diff --git a/pkg/command/root.go b/pkg/command/root.go index bd12a6e5d..9e48b23b0 100644 --- a/pkg/command/root.go +++ b/pkg/command/root.go @@ -32,45 +32,7 @@ func Execute() error { Flags: flagset.RootWithConfig(cfg), Before: func(c *cli.Context) error { - logger := NewLogger(cfg) - - viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - viper.SetEnvPrefix("GRAPH") - viper.AutomaticEnv() - - if c.IsSet("config-file") { - viper.SetConfigFile(c.String("config-file")) - } else { - viper.SetConfigName("graph") - - viper.AddConfigPath("/etc/ocis") - viper.AddConfigPath("$HOME/.ocis") - viper.AddConfigPath("./config") - } - - if err := viper.ReadInConfig(); err != nil { - switch err.(type) { - case viper.ConfigFileNotFoundError: - logger.Info(). - Msg("Continue without config") - case viper.UnsupportedConfigError: - logger.Fatal(). - Err(err). - Msg("Unsupported config type") - default: - logger.Fatal(). - Err(err). - Msg("Failed to read config") - } - } - - if err := viper.Unmarshal(&cfg); err != nil { - logger.Fatal(). - Err(err). - Msg("Failed to parse config") - } - - return nil + return ParseConfig(c, cfg) }, Commands: []*cli.Command{ @@ -101,3 +63,46 @@ func NewLogger(cfg *config.Config) log.Logger { log.Color(cfg.Log.Color), ) } + +// ParseConfig reads graph configuration from fs. +func ParseConfig(c *cli.Context, cfg *config.Config) error { + logger := NewLogger(cfg) + + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viper.SetEnvPrefix("GRAPH") + viper.AutomaticEnv() + + if c.IsSet("config-file") { + viper.SetConfigFile(c.String("config-file")) + } else { + viper.SetConfigName("graph") + + viper.AddConfigPath("/etc/ocis") + viper.AddConfigPath("$HOME/.ocis") + viper.AddConfigPath("./config") + } + + if err := viper.ReadInConfig(); err != nil { + switch err.(type) { + case viper.ConfigFileNotFoundError: + logger.Info(). + Msg("Continue without config") + case viper.UnsupportedConfigError: + logger.Fatal(). + Err(err). + Msg("Unsupported config type") + default: + logger.Fatal(). + Err(err). + Msg("Failed to read config") + } + } + + if err := viper.Unmarshal(&cfg); err != nil { + logger.Fatal(). + Err(err). + Msg("Failed to parse config") + } + + return nil +}