Skip to content

Commit

Permalink
Merge pull request #91 from jakefhyde/90-global-vars-marshal
Browse files Browse the repository at this point in the history
  • Loading branch information
jakefhyde authored Sep 26, 2022
2 parents 88bbecd + 6f0c989 commit c5a0b23
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 20 deletions.
24 changes: 14 additions & 10 deletions cmd/config/vars/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package vars

import (
"github.com/rancherlabs/corral/pkg/config"
"github.com/sirupsen/logrus"
"github.com/rancherlabs/corral/pkg/vars"
"github.com/spf13/cobra"
)

Expand All @@ -12,21 +12,25 @@ func NewCommandSet() *cobra.Command {
Short: "Create or update global variable.",
Long: "Create or update global variable.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
createVar(args[0], args[1])
RunE: func(cmd *cobra.Command, args []string) error {
cfg := config.MustLoad()
err := CreateVar(&cfg, args[0], args[1])
if err != nil {
return err
}
return cfg.Save()
},
}

return cmd
}

func createVar(key, value string) {
cfg := config.MustLoad()

cfg.Vars[key] = value

err := cfg.Save()
func CreateVar(cfg *config.Config, key, value string) error {
v, err := vars.FromJson(value)
if err != nil {
logrus.Fatalf("%e", err)
return err
}

cfg.Vars[key] = v
return nil
}
51 changes: 51 additions & 0 deletions cmd/config/vars/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package vars_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rancherlabs/corral/cmd/config/vars"
"github.com/rancherlabs/corral/pkg/config"
)

var _ = Describe("Set", func() {
When("a string is passed", func() {
It("returns that string", func() {
cfg := &config.Config{Vars: map[string]any{}}
err := vars.CreateVar(cfg, "test", "test")
Expect(err).To(BeNil())
Expect(cfg.Vars["test"]).To(Equal("test"))
})
})
When("a number is passed", func() {
It("returns a number", func() {
cfg := &config.Config{Vars: map[string]any{}}
err := vars.CreateVar(cfg, "test", "1")
Expect(err).To(BeNil())
Expect(cfg.Vars["test"]).To(Equal(1.))
})
})
When("a number is passed in quotes", func() {
It("returns a string", func() {
cfg := &config.Config{Vars: map[string]any{}}
err := vars.CreateVar(cfg, "test", `"1"`)
Expect(err).To(BeNil())
Expect(cfg.Vars["test"]).To(Equal("1"))
})
})
When("a json list is passed", func() {
It("returns a slice", func() {
cfg := &config.Config{Vars: map[string]any{}}
err := vars.CreateVar(cfg, "test", "[1,2,3]")
Expect(err).To(BeNil())
Expect(cfg.Vars["test"]).To(Equal([]any{1., 2., 3.}))
})
})
When("a json object is passed", func() {
It("returns a map", func() {
cfg := &config.Config{Vars: map[string]any{}}
err := vars.CreateVar(cfg, "test", `{"a":"1","b":2,"c":[1,2,3]}`)
Expect(err).To(BeNil())
Expect(cfg.Vars["test"]).To(Equal(map[string]any{"a": "1", "b": 2., "c": []any{1., 2., 3.}}))
})
})
})
4 changes: 2 additions & 2 deletions cmd/config/vars/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func NewVarsCommand() *cobra.Command {

func ListVars(cfg config.Config, output pkgcmd.OutputFormat, args ...string) (string, error) {
if len(args) == 1 {
return cfg.Vars[args[0]], nil
return fmt.Sprintf("%v", cfg.Vars[args[0]]), nil
}

vars := map[string]string{}
vars := map[string]any{}
if len(args) > 1 {
for _, k := range args {
vars[k] = cfg.Vars[k]
Expand Down
14 changes: 7 additions & 7 deletions cmd/config/vars/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,50 @@ var _ = Describe("Vars", func() {
When("only one variable is passed", func() {
When("the output format is table", func() {
It("returns only one variable", func() {
v, err := vars.ListVars(config.Config{Vars: map[string]string{"test": "test1"}}, pkgcmd.OutputFormatTable, "test")
v, err := vars.ListVars(config.Config{Vars: map[string]any{"test": "test1"}}, pkgcmd.OutputFormatTable, "test")
Expect(err).To(BeNil())
Expect(v).To(Equal("test1"))
})
})
When("the output format is json", func() {
It("returns only one variable", func() {
v, err := vars.ListVars(config.Config{Vars: map[string]string{"test": "test1"}}, pkgcmd.OutputFormatJSON, "test")
v, err := vars.ListVars(config.Config{Vars: map[string]any{"test": "test1"}}, pkgcmd.OutputFormatJSON, "test")
Expect(err).To(BeNil())
Expect(v).To(Equal("test1"))
})
})
When("the output format is yaml", func() {
It("returns only one variable", func() {
v, err := vars.ListVars(config.Config{Vars: map[string]string{"test": "test1"}}, pkgcmd.OutputFormatYAML, "test")
v, err := vars.ListVars(config.Config{Vars: map[string]any{"test": "test1"}}, pkgcmd.OutputFormatYAML, "test")
Expect(err).To(BeNil())
Expect(v).To(Equal("test1"))
})
})
})
When("an unsupported output format is passed", func() {
It("returns an error", func() {
_, err := vars.ListVars(config.Config{Vars: map[string]string{}}, "a")
_, err := vars.ListVars(config.Config{Vars: map[string]any{}}, "a")
Expect(err).Should(MatchError(pkgcmd.ErrUnknownOutputFormat))
})
})
When("no variables are passed", func() {
When("the output format is table", func() {
It("has the expected output", func() {
v, err := vars.ListVars(config.Config{Vars: map[string]string{"test": "test1"}}, pkgcmd.OutputFormatTable)
v, err := vars.ListVars(config.Config{Vars: map[string]any{"test": "test1"}}, pkgcmd.OutputFormatTable)
Expect(err).To(BeNil())
Expect(v).To(Equal("+------+-------+\n| NAME | VALUE |\n+------+-------+\n| test | test1 |\n+------+-------+"))
})
})
When("the output format is json", func() {
It("has the expected output", func() {
v, err := vars.ListVars(config.Config{Vars: map[string]string{"test": "test1"}}, pkgcmd.OutputFormatJSON)
v, err := vars.ListVars(config.Config{Vars: map[string]any{"test": "test1"}}, pkgcmd.OutputFormatJSON)
Expect(err).To(BeNil())
Expect(v).To(Equal(`{"test":"test1"}`))
})
})
When("the output format is yaml", func() {
It("has the expected output", func() {
v, err := vars.ListVars(config.Config{Vars: map[string]string{"test": "test1"}}, pkgcmd.OutputFormatYAML)
v, err := vars.ListVars(config.Config{Vars: map[string]any{"test": "test1"}}, pkgcmd.OutputFormatYAML)
Expect(err).To(BeNil())
Expect(v).To(Equal("test: test1"))
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Config struct {

Version string `yaml:"version"`

Vars map[string]string `yaml:"vars"`
Vars map[string]any `yaml:"vars"`
}

func MustLoad() Config {
Expand Down

0 comments on commit c5a0b23

Please sign in to comment.