From b0fb996b82b27171c8999a9e8bd673af0e8ddde2 Mon Sep 17 00:00:00 2001 From: Ralf Haferkamp Date: Thu, 7 Apr 2022 17:07:53 +0200 Subject: [PATCH] Allow to pass string list via Enviroment Allow to pass comma-separated strings via Enviroment variables and store them in a string slice. --- go.mod | 1 + ocis-pkg/config/config_suite_test.go | 13 +++++++++++ ocis-pkg/config/config_test.go | 23 +++++++++--------- ocis-pkg/config/environment.go | 14 +++++++++++ ocis-pkg/config/environment_test.go | 35 ++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 ocis-pkg/config/config_suite_test.go create mode 100644 ocis-pkg/config/environment_test.go diff --git a/go.mod b/go.mod index 64adb1d12..dbdd16875 100644 --- a/go.mod +++ b/go.mod @@ -52,6 +52,7 @@ require ( github.com/oklog/run v1.1.0 github.com/olekukonko/tablewriter v0.0.5 github.com/onsi/ginkgo v1.16.5 + github.com/onsi/ginkgo/v2 v2.1.3 github.com/onsi/gomega v1.19.0 github.com/owncloud/libre-graph-api-go v0.13.3 github.com/pkg/errors v0.9.1 diff --git a/ocis-pkg/config/config_suite_test.go b/ocis-pkg/config/config_suite_test.go new file mode 100644 index 000000000..c6e29ba71 --- /dev/null +++ b/ocis-pkg/config/config_suite_test.go @@ -0,0 +1,13 @@ +package config_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestConfig(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Config Suite") +} diff --git a/ocis-pkg/config/config_test.go b/ocis-pkg/config/config_test.go index 6a77d3cda..bfbc7b408 100644 --- a/ocis-pkg/config/config_test.go +++ b/ocis-pkg/config/config_test.go @@ -1,17 +1,16 @@ -package config +package config_test import ( - "fmt" - "testing" - + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/owncloud/ocis/ocis-pkg/config" "gopkg.in/yaml.v2" ) -func TestDefaultConfig(t *testing.T) { - cfg := DefaultConfig() - yBytes, err := yaml.Marshal(cfg) - if err != nil { - panic(err) - } - fmt.Println(string(yBytes)) -} +var _ = Describe("Config", func() { + It("Success generating the default config", func() { + cfg := config.DefaultConfig() + _, err := yaml.Marshal(cfg) + Expect(err).To(BeNil()) + }) +}) diff --git a/ocis-pkg/config/environment.go b/ocis-pkg/config/environment.go index c713d92e4..d30c26422 100644 --- a/ocis-pkg/config/environment.go +++ b/ocis-pkg/config/environment.go @@ -3,6 +3,7 @@ package config import ( "fmt" "reflect" + "strings" gofig "github.com/gookit/config/v2" "github.com/owncloud/ocis/ocis-pkg/shared" @@ -35,6 +36,11 @@ func bindEnv(c *gofig.Config, bindings []shared.EnvBinding) error { // defaults to float64 r := c.Float(bindings[i].EnvVars[j]) *bindings[i].Destination.(*float64) = r + case "*[]string": + // Treat values a comma-separated list + r := c.String(bindings[i].EnvVars[j]) + vals := envStringToSlice(r) + *bindings[i].Destination.(*[]string) = vals default: // it is unlikely we will ever get here. Let this serve more as a runtime check for when debugging. return fmt.Errorf("invalid type for env var: `%v`", bindings[i].EnvVars[j]) @@ -45,3 +51,11 @@ func bindEnv(c *gofig.Config, bindings []shared.EnvBinding) error { return nil } + +func envStringToSlice(value string) []string { + vals := strings.Split(value, ",") + for i := range vals { + vals[i] = strings.TrimSpace(vals[i]) + } + return vals +} diff --git a/ocis-pkg/config/environment_test.go b/ocis-pkg/config/environment_test.go new file mode 100644 index 000000000..32a22ef1f --- /dev/null +++ b/ocis-pkg/config/environment_test.go @@ -0,0 +1,35 @@ +package config_test + +import ( + gofig "github.com/gookit/config/v2" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + ociscfg "github.com/owncloud/ocis/ocis-pkg/config" + "github.com/owncloud/ocis/ocis-pkg/shared" +) + +var _ = Describe("Environment", func() { + It("Succeed to parse a comma separated list in to a sting slice", func() { + cfg := gofig.NewEmpty("test") + err := cfg.Set("stringlist", "one,two,three") + Expect(err).To(Not(HaveOccurred())) + err = cfg.Set("stringlist2", "one ,two , t h r e e") + Expect(err).To(Not(HaveOccurred())) + var stringTest, stringTest2 []string + eb := []shared.EnvBinding{ + { + EnvVars: []string{"stringlist"}, + Destination: &stringTest, + }, + { + EnvVars: []string{"stringlist2"}, + Destination: &stringTest2, + }, + } + err = ociscfg.BindEnv(cfg, eb) + Expect(err).To(Not(HaveOccurred())) + Expect(stringTest).To(Equal([]string{"one", "two", "three"})) + Expect(stringTest2).To(Equal([]string{"one", "two", "t h r e e"})) + + }) +})