|
| 1 | +package qbcli |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + |
| 8 | + "github.com/QuickBase/quickbase-cli/qbclient" |
| 9 | + "github.com/QuickBase/quickbase-cli/qberrors" |
| 10 | + "github.com/go-playground/validator/v10" |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +type QuickbaseFile struct { |
| 15 | + Deploy *QuickbaseFileDeploy `yaml:"deploy"` |
| 16 | +} |
| 17 | + |
| 18 | +type QuickbaseFileDeploy struct { |
| 19 | + Formulas []*QuickbaseFileDeployFormula `yaml:"formulas"` |
| 20 | +} |
| 21 | + |
| 22 | +type QuickbaseFileDeployFormula struct { |
| 23 | + File string `validate:"required" yaml:"file"` |
| 24 | + TableID string `validate:"required" yaml:"table_id"` |
| 25 | + FieldID int `validate:"required" yaml:"field_id"` |
| 26 | +} |
| 27 | + |
| 28 | +func ParseQuickbaseFile(file string) (f *QuickbaseFile, err error) { |
| 29 | + f = &QuickbaseFile{} |
| 30 | + |
| 31 | + b, err := ioutil.ReadFile(file) |
| 32 | + if err != nil { |
| 33 | + err = fmt.Errorf("error reading quickbase file: %w", err) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + dec := yaml.NewDecoder(bytes.NewBuffer(b)) |
| 38 | + dec.KnownFields(true) |
| 39 | + err = dec.Decode(f) |
| 40 | + |
| 41 | + if verr := validator.New().Struct(f); err != nil { |
| 42 | + err = qberrors.HandleErrorValidation(verr) |
| 43 | + } |
| 44 | + |
| 45 | + return |
| 46 | +} |
| 47 | + |
| 48 | +type DeployFormulaInput struct { |
| 49 | + File string `cliutil:"option=file default=quickbase.yml"` |
| 50 | +} |
| 51 | + |
| 52 | +type DeployFormulaOutput struct { |
| 53 | + Deployed map[string][]int `json:"deployed"` |
| 54 | + Errors map[string]map[int]string `json:"errors"` |
| 55 | +} |
| 56 | + |
| 57 | +func DeployFormula(qb *qbclient.Client, in *DeployFormulaInput) (out *DeployFormulaOutput, err error) { |
| 58 | + out = &DeployFormulaOutput{ |
| 59 | + Deployed: map[string][]int{}, |
| 60 | + Errors: map[string]map[int]string{}, |
| 61 | + } |
| 62 | + |
| 63 | + var file *QuickbaseFile |
| 64 | + file, err = ParseQuickbaseFile(in.File) |
| 65 | + if err != nil { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + for _, f := range file.Deploy.Formulas { |
| 70 | + |
| 71 | + b, ferr := ioutil.ReadFile(f.File) |
| 72 | + if ferr != nil { |
| 73 | + err = fmt.Errorf("error reading formula file: %w", ferr) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + ufi := &qbclient.UpdateFieldInput{ |
| 78 | + TableID: f.TableID, |
| 79 | + FieldID: f.FieldID, |
| 80 | + Properties: &qbclient.UpdateFieldInputProperties{}, |
| 81 | + } |
| 82 | + ufi.Properties.Formula = string(b) |
| 83 | + |
| 84 | + // TODO See https://github.com/QuickBase/quickbase-cli/issues/36 |
| 85 | + ufi.AddToNewReports = true |
| 86 | + ufi.Searchable = true |
| 87 | + |
| 88 | + _, uerr := qb.UpdateField(ufi) |
| 89 | + if uerr == nil { |
| 90 | + if _, ok := out.Deployed[f.TableID]; !ok { |
| 91 | + out.Deployed[f.TableID] = []int{} |
| 92 | + } |
| 93 | + out.Deployed[f.TableID] = append(out.Deployed[f.TableID], f.FieldID) |
| 94 | + } else { |
| 95 | + if _, ok := out.Errors[f.TableID]; !ok { |
| 96 | + out.Errors[f.TableID] = make(map[int]string) |
| 97 | + } |
| 98 | + out.Errors[f.TableID][f.FieldID] = uerr.Error() |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return |
| 103 | +} |
0 commit comments