diff --git a/cmd/root.go b/cmd/root.go index d92fd823..fb55958e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "strings" + "time" "github.com/hyperledger-labs/yui-relayer/config" "github.com/hyperledger-labs/yui-relayer/core" @@ -18,10 +19,18 @@ import ( ) var ( - homePath string - debug bool - defaultHome = os.ExpandEnv("$HOME/.yui-relayer") - configPath = "config/config.json" + homePath string + debug bool + timeout time.Duration + logLevel string + logFormat string + logOutput string + defaultHome = os.ExpandEnv("$HOME/.yui-relayer") + defaultTimeout = 10 * time.Second + defaultLogLevel = "DEBUG" + defaultLogFormat = "json" + defaultLogOutput = "stderr" + configPath = "config/config.json" ) // Execute adds all child commands to the root command and sets flags appropriately. @@ -41,6 +50,10 @@ func Execute(modules ...config.ModuleI) error { // Register top level flags --home and --debug rootCmd.PersistentFlags().StringVar(&homePath, flags.FlagHome, defaultHome, "set home directory") rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "debug output") + rootCmd.PersistentFlags().DurationVar(&timeout, "timeout", defaultTimeout, "rpc timeout duration") + rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", defaultLogLevel, "set the log level") + rootCmd.PersistentFlags().StringVar(&logFormat, "log-format", defaultLogFormat, "set the log format") + rootCmd.PersistentFlags().StringVar(&logOutput, "log-output", defaultLogOutput, "set the log output") if err := viper.BindPFlag(flags.FlagHome, rootCmd.PersistentFlags().Lookup(flags.FlagHome)); err != nil { return err } @@ -79,10 +92,10 @@ func Execute(modules ...config.ModuleI) error { if err := viper.BindPFlags(cmd.Flags()); err != nil { return fmt.Errorf("failed to bind the flag set to the configuration: %v", err) } - if err := ctx.Config.InitConfig(ctx, homePath, configPath, debug); err != nil { + if err := ctx.Config.InitConfig(ctx, homePath, configPath, debug, timeout); err != nil { return fmt.Errorf("failed to initialize the configuration: %v", err) } - if err := initLogger(ctx); err != nil { + if err := log.InitLogger(logLevel, logFormat, logOutput); err != nil { return err } if err := metrics.InitializeMetrics(metrics.ExporterNull{}); err != nil { @@ -106,11 +119,6 @@ func readStdin() (string, error) { return strings.TrimSpace(str), err } -func initLogger(ctx *config.Context) error { - c := ctx.Config.Global.LoggerConfig - return log.InitLogger(c.Level, c.Format, c.Output) -} - func noCommand(cmd *cobra.Command, args []string) error { cmd.Help() return errors.New("specified command does not exist") diff --git a/config/config.go b/config/config.go index 30aa67b6..c62f9a24 100644 --- a/config/config.go +++ b/config/config.go @@ -12,7 +12,6 @@ import ( ) type Config struct { - Global GlobalConfig `yaml:"global" json:"global"` Chains []core.ChainProverConfig `yaml:"chains" json:"chains"` Paths core.Paths `yaml:"paths" json:"paths"` @@ -24,39 +23,12 @@ type Config struct { func defaultConfig(configPath string) Config { return Config{ - Global: newDefaultGlobalConfig(), Chains: []core.ChainProverConfig{}, Paths: core.Paths{}, ConfigPath: configPath, } } -// GlobalConfig describes any global relayer settings -type GlobalConfig struct { - Timeout string `yaml:"timeout" json:"timeout"` - LightCacheSize int `yaml:"light-cache-size" json:"light-cache-size"` - LoggerConfig LoggerConfig `yaml:"logger" json:"logger"` -} - -type LoggerConfig struct { - Level string `yaml:"level" json:"level"` - Format string `yaml:"format" json:"format"` - Output string `yaml:"output" json:"output"` -} - -// newDefaultGlobalConfig returns a global config with defaults set -func newDefaultGlobalConfig() GlobalConfig { - return GlobalConfig{ - Timeout: "10s", - LightCacheSize: 20, - LoggerConfig: LoggerConfig{ - Level: "DEBUG", - Format: "json", - Output: "stderr", - }, - } -} - func (c *Config) InitCoreConfig() { initCoreConfig(c) } @@ -128,14 +100,9 @@ func (c *Config) ChainsFromPath(path string) (map[string]*core.ProvableChain, st } // Called to initialize the relayer.Chain types on Config -func InitChains(ctx *Context, homePath string, debug bool) error { - to, err := time.ParseDuration(ctx.Config.Global.Timeout) - if err != nil { - return fmt.Errorf("did you remember to run 'rly config init' error:%w", err) - } - +func InitChains(ctx *Context, homePath string, debug bool, timeout time.Duration) error { for _, chain := range ctx.Config.chains { - if err := chain.Init(homePath, to, ctx.Codec, debug); err != nil { + if err := chain.Init(homePath, timeout, ctx.Codec, debug); err != nil { return fmt.Errorf("did you remember to run 'rly config init' error:%w", err) } } @@ -143,7 +110,7 @@ func InitChains(ctx *Context, homePath string, debug bool) error { return nil } -func (c *Config) InitConfig(ctx *Context, homePath, configPath string, debug bool) error { +func (c *Config) InitConfig(ctx *Context, homePath, configPath string, debug bool, timeout time.Duration) error { cfgPath := fmt.Sprintf("%s/%s", homePath, configPath) c.ConfigPath = cfgPath if _, err := os.Stat(cfgPath); err == nil { @@ -156,7 +123,7 @@ func (c *Config) InitConfig(ctx *Context, homePath, configPath string, debug boo return err } // ensure config has []*relayer.Chain used for all chain operations - if err = InitChains(ctx, homePath, debug); err != nil { + if err = InitChains(ctx, homePath, debug, timeout); err != nil { return err } } else {