Remove unused BindEnv code

We dropped this in favour of envdecode a while ago.
This commit is contained in:
Ralf Haferkamp
2022-07-14 16:34:25 +02:00
parent 930ccbffb3
commit 946f3e8feb
32 changed files with 57 additions and 153 deletions
-61
View File
@@ -1,61 +0,0 @@
package config
import (
"fmt"
"reflect"
"strings"
gofig "github.com/gookit/config/v2"
"github.com/owncloud/ocis/v2/ocis-pkg/shared"
)
// BindEnv takes a config `c` and a EnvBinding and binds the values from the environment to the address location in cfg.
func BindEnv(c *gofig.Config, bindings []shared.EnvBinding) error {
return bindEnv(c, bindings)
}
func bindEnv(c *gofig.Config, bindings []shared.EnvBinding) error {
for i := range bindings {
for j := range bindings[i].EnvVars {
// we need to guard against v != "" because this is the condition that checks that the value is set from the environment.
// the `ok` guard is not enough, apparently.
if v, ok := c.GetValue(bindings[i].EnvVars[j]); ok && v != "" {
// get the destination type from destination
switch reflect.ValueOf(bindings[i].Destination).Type().String() {
case "*bool":
r := c.Bool(bindings[i].EnvVars[j])
*bindings[i].Destination.(*bool) = r
case "*string":
r := c.String(bindings[i].EnvVars[j])
*bindings[i].Destination.(*string) = r
case "*int":
r := c.Int(bindings[i].EnvVars[j])
*bindings[i].Destination.(*int) = r
case "*float64":
// 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])
}
}
}
}
return nil
}
func envStringToSlice(value string) []string {
vals := strings.Split(value, ",")
for i := range vals {
vals[i] = strings.TrimSpace(vals[i])
}
return vals
}
-35
View File
@@ -1,35 +0,0 @@
package config_test
import (
gofig "github.com/gookit/config/v2"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
ociscfg "github.com/owncloud/ocis/v2/ocis-pkg/config"
"github.com/owncloud/ocis/v2/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"}))
})
})