From 6a07f5e4d9aaaaf471afd2b0589a1229fa5a4a71 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Wed, 13 Apr 2022 14:34:02 +0200 Subject: [PATCH] add --config-file flag to accounts service --- accounts/pkg/command/server.go | 10 +++ accounts/pkg/config/config.go | 3 + accounts/pkg/config/defaults/defaultconfig.go | 2 + accounts/pkg/config/parser/parse.go | 2 +- docs/helpers/configenvextractor.go | 1 + ocis-pkg/config/helpers.go | 67 +++---------------- 6 files changed, 26 insertions(+), 59 deletions(-) diff --git a/accounts/pkg/command/server.go b/accounts/pkg/command/server.go index 52fd784ee98..8cd7997d39d 100644 --- a/accounts/pkg/command/server.go +++ b/accounts/pkg/command/server.go @@ -20,10 +20,20 @@ import ( // Server is the entry point for the server command. func Server(cfg *config.Config) *cli.Command { + configFileFlag := cli.StringFlag{ + Name: "config-file", + Value: cfg.ConfigFile, + Usage: "config file to be loaded by the extension", + Destination: &cfg.ConfigFile, + } + return &cli.Command{ Name: "server", Usage: fmt.Sprintf("start %s extension without runtime (unsupervised mode)", cfg.Service.Name), Category: "server", + Flags: []cli.Flag{ + &configFileFlag, + }, Before: func(c *cli.Context) error { return parser.ParseConfig(cfg) }, diff --git a/accounts/pkg/config/config.go b/accounts/pkg/config/config.go index 9b46d2dbf17..454f7b29cc8 100644 --- a/accounts/pkg/config/config.go +++ b/accounts/pkg/config/config.go @@ -10,6 +10,9 @@ import ( type Config struct { *shared.Commons `yaml:"-"` + ConfigFile string `yaml:"-" env:"ACCOUNTS_CONFIG_FILE" desc:"config file to be used by the accounts extension"` + ConfigFileHasBeenSet bool `yaml:"-"` + Service Service `yaml:"-"` Tracing *Tracing `yaml:"tracing"` diff --git a/accounts/pkg/config/defaults/defaultconfig.go b/accounts/pkg/config/defaults/defaultconfig.go index 25912cfa648..261a6cb1a84 100644 --- a/accounts/pkg/config/defaults/defaultconfig.go +++ b/accounts/pkg/config/defaults/defaultconfig.go @@ -19,6 +19,8 @@ func FullDefaultConfig() *config.Config { func DefaultConfig() *config.Config { return &config.Config{ + ConfigFile: path.Join(defaults.BaseConfigPath(), "accounts.yaml"), + Debug: config.Debug{ Addr: "127.0.0.1:9182", Token: "", diff --git a/accounts/pkg/config/parser/parse.go b/accounts/pkg/config/parser/parse.go index f689ee528c1..ff43a695a30 100644 --- a/accounts/pkg/config/parser/parse.go +++ b/accounts/pkg/config/parser/parse.go @@ -12,7 +12,7 @@ import ( // ParseConfig loads accounts configuration from known paths. func ParseConfig(cfg *config.Config) error { - _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg) + _, err := ociscfg.BindSourcesToStructs(cfg.Service.Name, cfg.ConfigFile, cfg.ConfigFileHasBeenSet, cfg) if err != nil { return err } diff --git a/docs/helpers/configenvextractor.go b/docs/helpers/configenvextractor.go index 1d5b9f6f81d..7abb62ec60d 100644 --- a/docs/helpers/configenvextractor.go +++ b/docs/helpers/configenvextractor.go @@ -57,6 +57,7 @@ func GenerateIntermediateCode(templatePath string, intermediateCodePath string, func RunIntermediateCode(intermediateCodePath string) { fmt.Println("Running intermediate go code for " + intermediateCodePath) os.Setenv("OCIS_BASE_DATA_PATH", "~/.ocis") + os.Setenv("OCIS_CONFIG_DIR", "~/.ocis") out, err := exec.Command("go", "run", intermediateCodePath).Output() if err != nil { log.Fatal(err) diff --git a/ocis-pkg/config/helpers.go b/ocis-pkg/config/helpers.go index ee005500fb2..693ed3a6bf1 100644 --- a/ocis-pkg/config/helpers.go +++ b/ocis-pkg/config/helpers.go @@ -1,83 +1,34 @@ package config import ( - "io/fs" - "os" - "path/filepath" - "strings" - gofig "github.com/gookit/config/v2" gooyaml "github.com/gookit/config/v2/yaml" - "github.com/owncloud/ocis/ocis-pkg/config/defaults" ) var ( - // supportedExtensions is determined by gookit/config. - // we only support the official yaml file ending (http://yaml.org/faq.html) to - // mitigate the loading order problem. - // It would raise this question: does yaml win over yml or vice versa!? - supportedExtensions = []string{ - "yaml", - } // decoderConfigTagname sets the tag name to be used from the config structs // currently we only support "yaml" because we only support config loading // from yaml files and the yaml parser has no simple way to set a custom tag name to use decoderConfigTagName = "yaml" ) -// configSources returns a slice with matched expected config files. -// It uses globbing to match a config file by name, and retrieve any supported extension supported by our drivers. -// It sanitizes the output depending on the list of drivers provided. -func configSources(filename string, drivers []string) []string { - var sources []string - - locations := []string{ - defaults.BaseConfigPath(), - } - - for i := range locations { - dirFS := os.DirFS(locations[i]) - pattern := filename + ".*" - matched, _ := fs.Glob(dirFS, pattern) - if len(matched) > 0 { - // prepend path to results - for j := 0; j < len(matched); j++ { - matched[j] = filepath.Join(locations[i], matched[j]) - } - } - sources = append(sources, matched...) - } - - return sanitizeExtensions(sources, drivers, func(a, b string) bool { - return strings.HasSuffix(filepath.Base(a), b) - }) -} - -// sanitizeExtensions removes elements from "set" which extensions are not in "ext". -func sanitizeExtensions(set []string, ext []string, f func(a, b string) bool) []string { - var r []string - for i := 0; i < len(set); i++ { - for j := 0; j < len(ext); j++ { - if f(filepath.Base(set[i]), ext[j]) { - r = append(r, set[i]) - } - } - } - return r -} - // BindSourcesToStructs assigns any config value from a config file / env variable to struct `dst`. Its only purpose // is to solely modify `dst`, not dealing with the config structs; and do so in a thread safe manner. -func BindSourcesToStructs(extension string, dst interface{}) (*gofig.Config, error) { - sources := configSources(extension, supportedExtensions) +func BindSourcesToStructs(extension, ConfigFile string, failOnLoadErr bool, dst interface{}) (*gofig.Config, error) { + sources := []string{ConfigFile} cnf := gofig.NewWithOptions(extension) cnf.WithOptions(func(options *gofig.Options) { options.DecoderConfig.TagName = decoderConfigTagName }) cnf.AddDriver(gooyaml.Driver) - _ = cnf.LoadFiles(sources...) - err := cnf.BindStruct("", &dst) + err := cnf.LoadFiles(sources...) + if err != nil && failOnLoadErr { + // fail only if config file was explicitly set + return nil, err + } + + err = cnf.BindStruct("", &dst) if err != nil { return nil, err }