Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove global config #136

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/hyperledger-labs/yui-relayer/config"
"github.com/hyperledger-labs/yui-relayer/core"
Expand All @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
41 changes: 4 additions & 37 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand All @@ -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)
}
Expand Down Expand Up @@ -128,22 +100,17 @@ 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)
}
}

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 {
Expand All @@ -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 {
Expand Down
Loading