Skip to content

Commit 9c8be0b

Browse files
committed
Fixed #37: Adddedd command to deploy formulas given a configuraion file
1 parent c592fd0 commit 9c8be0b

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

cmd/formula_deploy.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cmd
2+
3+
import (
4+
"github.com/QuickBase/quickbase-cli/qbcli"
5+
"github.com/QuickBase/quickbase-cli/qbclient"
6+
"github.com/cpliakas/cliutil"
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/viper"
9+
)
10+
11+
var formulaDeployCfg *viper.Viper
12+
13+
var formulaDeployCmd = &cobra.Command{
14+
Use: "deploy",
15+
Short: "Deploy formulas to an app",
16+
17+
Args: func(cmd *cobra.Command, args []string) (err error) {
18+
err = globalCfg.Validate()
19+
return
20+
},
21+
22+
Run: func(cmd *cobra.Command, args []string) {
23+
ctx, logger, qb := qbcli.NewClient(cmd, globalCfg)
24+
25+
input := &qbcli.DeployFormulaInput{}
26+
qbcli.GetOptions(ctx, logger, input, formulaDeployCfg)
27+
28+
output, err := qbcli.DeployFormula(qb, input)
29+
qbcli.Render(ctx, logger, cmd, globalCfg, output, err)
30+
},
31+
}
32+
33+
func init() {
34+
var flags *cliutil.Flagger
35+
formulaDeployCfg, flags = cliutil.AddCommand(formulaCmd, formulaDeployCmd, qbclient.EnvPrefix)
36+
flags.SetOptions(&qbcli.DeployFormulaInput{})
37+
}

qbcli/qbfile.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)