Skip to content

Commit

Permalink
feat: allow configuration of config loader via cmdx (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-suhas authored Aug 1, 2023
1 parent 02e9c21 commit dff8984
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cmdx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (
GOTOCOMPANY_CONFIG_DIR = "GOTOCOMPANY_CONFIG_DIR"
XDG_CONFIG_HOME = "XDG_CONFIG_HOME"
APP_DATA = "AppData"
LOCAL_APP_DATA = "LocalAppData"
)

type ConfigLoaderOpt func(c *Config)
Expand All @@ -28,6 +27,12 @@ func WithFlags(pfs *pflag.FlagSet) ConfigLoaderOpt {
}
}

func WithLoaderOptions(opts ...config.LoaderOption) ConfigLoaderOpt {
return func(c *Config) {
c.loaderOpts = append(c.loaderOpts, opts...)
}
}

// SetConfig allows to set a client config file. It is used to
// load and save a config file for command line clients.
func SetConfig(app string) *Config {
Expand All @@ -39,6 +44,7 @@ func SetConfig(app string) *Config {
type Config struct {
filename string
boundedPFlags *pflag.FlagSet
loaderOpts []config.LoaderOption
}

func (c *Config) File() string {
Expand Down Expand Up @@ -86,10 +92,10 @@ func (c *Config) Load(cfg interface{}, opts ...ConfigLoaderOpt) error {
}

loaderOpts := []config.LoaderOption{config.WithFile(c.filename)}

if c.boundedPFlags != nil {
loaderOpts = append(loaderOpts, config.WithBindPFlags(c.boundedPFlags, cfg))
}
loaderOpts = append(loaderOpts, c.loaderOpts...)

loader := config.NewLoader(loaderOpts...)

Expand Down

0 comments on commit dff8984

Please sign in to comment.