Skip to content

Commit

Permalink
added viper
Browse files Browse the repository at this point in the history
  • Loading branch information
Mads Schou-Andreasen committed Apr 16, 2021
1 parent 8a01803 commit d016cff
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 4 deletions.
87 changes: 84 additions & 3 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"github.com/joho/godotenv"
"github.com/panjf2000/ants/v2"
"github.com/pkg/errors"
"github.com/spf13/viper"
secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
"gopkg.in/yaml.v2"
"log"
"os"

"reflect"
"sync"
)
Expand All @@ -28,8 +30,9 @@ func (svc *secretClient) accessSecretVersion(name string) ([]byte, error) {
}

type params struct {
v *reflect.Value
name string
v *reflect.Value
name string
viper *viper.Viper
}

// InitializeConfig initializes a config struct by getting the secrets from GCP Secret Manager
Expand Down Expand Up @@ -70,6 +73,80 @@ func InitializeConfigYaml(cfg interface{}, project string, secret string) error
return nil
}

func InitializeConfigViper(cfg interface{}, project string, viperP *viper.Viper) (error, []error) {

grabber, err := newClient(project)
if err != nil {
return err, nil
}
defer grabber.client.Close()

t := reflect.TypeOf(cfg)

if t.Kind() != reflect.Ptr {
return errors.New("cfg argument must be a pointer to a struct"), nil
}

s := reflect.ValueOf(cfg).Elem()
if s.Kind() != reflect.Struct {
return errors.New("cfg argument must be a pointer to a struct"), nil
}

var wg sync.WaitGroup

type errorStruct struct {
err error
name string
}

c := make(chan errorStruct, s.NumField())

p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
defer wg.Done()
p := i.(params)

err := grabber.setValueFromGcp(p)
if err == nil {
return
}

c <- errorStruct{err, p.name}

})
defer ants.Release()

for i := 0; i < s.NumField(); i++ {

f := s.Field(i)
name := s.Type().Field(i).Name

if !f.IsValid() || !f.CanSet() {
return errors.New(fmt.Sprintf("field %s is not valid - check if field is value and that it is exported from struct", name)), nil
}

if f.Kind() != reflect.String {
return errors.New(fmt.Sprintf("pointer struct can only contain string fields - field '%s' is of type '%s'", name, f.Type().Name())), nil
}
wg.Add(1)
err := p.Invoke(params{v: &f, name: name, viper: viperP})
if err != nil {
return err, nil
}

}

wg.Wait()

close(c)

var errs []error
for err := range c {
errs = append(errs, err.err)
}

return err, errs
}

func InitializeConfig(cfg interface{}, project string, envFileAction EnvFileAction) error {

grabber, err := newClient(project)
Expand Down Expand Up @@ -145,7 +222,7 @@ func InitializeConfig(cfg interface{}, project string, envFileAction EnvFileActi
return errors.New(fmt.Sprintf("pointer struct can only contain string fields - field '%s' is of type '%s'", name, f.Type().Name()))
}
wg.Add(1)
err := p.Invoke(params{&f, name})
err := p.Invoke(params{v: &f, name: name})
if err != nil {
return err
}
Expand All @@ -171,6 +248,10 @@ func (svc *secretClient) setValueFromGcp(p params) error {
return err
}

if p.viper != nil {
p.viper.Set(p.name, secretString)
}

p.v.SetString(string(secretString))
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.15

require (
cloud.google.com/go v0.70.0
github.com/davecgh/go-spew v1.1.1
github.com/joho/godotenv v1.3.0
github.com/panjf2000/ants/v2 v2.4.3
github.com/pkg/errors v0.9.1
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20201026091529-146b70c837a4 // indirect
google.golang.org/genproto v0.0.0-20201026171402-d4b8fe4fd877
Expand Down
Loading

0 comments on commit d016cff

Please sign in to comment.