Skip to content

Commit

Permalink
Fixed #37: Adddedd command to deploy formulas given a configuraion file
Browse files Browse the repository at this point in the history
  • Loading branch information
cpliakas committed May 22, 2021
1 parent c592fd0 commit 9c8be0b
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cmd/formula_deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"github.com/QuickBase/quickbase-cli/qbcli"
"github.com/QuickBase/quickbase-cli/qbclient"
"github.com/cpliakas/cliutil"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var formulaDeployCfg *viper.Viper

var formulaDeployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy formulas to an app",

Args: func(cmd *cobra.Command, args []string) (err error) {
err = globalCfg.Validate()
return
},

Run: func(cmd *cobra.Command, args []string) {
ctx, logger, qb := qbcli.NewClient(cmd, globalCfg)

input := &qbcli.DeployFormulaInput{}
qbcli.GetOptions(ctx, logger, input, formulaDeployCfg)

output, err := qbcli.DeployFormula(qb, input)
qbcli.Render(ctx, logger, cmd, globalCfg, output, err)
},
}

func init() {
var flags *cliutil.Flagger
formulaDeployCfg, flags = cliutil.AddCommand(formulaCmd, formulaDeployCmd, qbclient.EnvPrefix)
flags.SetOptions(&qbcli.DeployFormulaInput{})
}
103 changes: 103 additions & 0 deletions qbcli/qbfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package qbcli

import (
"bytes"
"fmt"
"io/ioutil"

"github.com/QuickBase/quickbase-cli/qbclient"
"github.com/QuickBase/quickbase-cli/qberrors"
"github.com/go-playground/validator/v10"
"gopkg.in/yaml.v3"
)

type QuickbaseFile struct {
Deploy *QuickbaseFileDeploy `yaml:"deploy"`
}

type QuickbaseFileDeploy struct {
Formulas []*QuickbaseFileDeployFormula `yaml:"formulas"`
}

type QuickbaseFileDeployFormula struct {
File string `validate:"required" yaml:"file"`
TableID string `validate:"required" yaml:"table_id"`
FieldID int `validate:"required" yaml:"field_id"`
}

func ParseQuickbaseFile(file string) (f *QuickbaseFile, err error) {
f = &QuickbaseFile{}

b, err := ioutil.ReadFile(file)
if err != nil {
err = fmt.Errorf("error reading quickbase file: %w", err)
return
}

dec := yaml.NewDecoder(bytes.NewBuffer(b))
dec.KnownFields(true)
err = dec.Decode(f)

if verr := validator.New().Struct(f); err != nil {
err = qberrors.HandleErrorValidation(verr)
}

return
}

type DeployFormulaInput struct {
File string `cliutil:"option=file default=quickbase.yml"`
}

type DeployFormulaOutput struct {
Deployed map[string][]int `json:"deployed"`
Errors map[string]map[int]string `json:"errors"`
}

func DeployFormula(qb *qbclient.Client, in *DeployFormulaInput) (out *DeployFormulaOutput, err error) {
out = &DeployFormulaOutput{
Deployed: map[string][]int{},
Errors: map[string]map[int]string{},
}

var file *QuickbaseFile
file, err = ParseQuickbaseFile(in.File)
if err != nil {
return
}

for _, f := range file.Deploy.Formulas {

b, ferr := ioutil.ReadFile(f.File)
if ferr != nil {
err = fmt.Errorf("error reading formula file: %w", ferr)
return
}

ufi := &qbclient.UpdateFieldInput{
TableID: f.TableID,
FieldID: f.FieldID,
Properties: &qbclient.UpdateFieldInputProperties{},
}
ufi.Properties.Formula = string(b)

// TODO See https://github.com/QuickBase/quickbase-cli/issues/36
ufi.AddToNewReports = true
ufi.Searchable = true

_, uerr := qb.UpdateField(ufi)
if uerr == nil {
if _, ok := out.Deployed[f.TableID]; !ok {
out.Deployed[f.TableID] = []int{}
}
out.Deployed[f.TableID] = append(out.Deployed[f.TableID], f.FieldID)
} else {
if _, ok := out.Errors[f.TableID]; !ok {
out.Errors[f.TableID] = make(map[int]string)
}
out.Errors[f.TableID][f.FieldID] = uerr.Error()
}
}

return
}

0 comments on commit 9c8be0b

Please sign in to comment.