Skip to content

Commit

Permalink
add --config-file flag to accounts service
Browse files Browse the repository at this point in the history
  • Loading branch information
wkloucek committed Apr 13, 2022
1 parent 608c5d0 commit 6a07f5e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 59 deletions.
10 changes: 10 additions & 0 deletions accounts/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
3 changes: 3 additions & 0 deletions accounts/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 2 additions & 0 deletions accounts/pkg/config/defaults/defaultconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down
2 changes: 1 addition & 1 deletion accounts/pkg/config/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions docs/helpers/configenvextractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
67 changes: 9 additions & 58 deletions ocis-pkg/config/helpers.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down

0 comments on commit 6a07f5e

Please sign in to comment.