Skip to content

Commit

Permalink
Merge pull request #146 from dai1975/features/init-logger
Browse files Browse the repository at this point in the history
restruct config initialization
  • Loading branch information
siburu authored Aug 15, 2024
2 parents a10aff4 + 810fd56 commit 4ecddcb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 42 deletions.
3 changes: 2 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"encoding/json"

"github.com/hyperledger-labs/yui-relayer/config"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -51,7 +52,7 @@ func configShowCmd(ctx *config.Context) *cobra.Command {
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
return fmt.Errorf("config does not exist: %s", cfgPath)
}
out, err := config.MarshalJSON(*ctx.Config)
out, err := json.Marshal(*ctx.Config)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,15 @@ 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.UnmarshalConfig(homePath, configPath); err != nil {
return fmt.Errorf("failed to initialize the configuration: %v", err)
}
if err := initLogger(ctx); err != nil {
return err
}
if err := ctx.InitConfig(homePath, debug); err != nil {
return fmt.Errorf("failed to initialize the configuration: %v", err)
}
if err := metrics.InitializeMetrics(metrics.ExporterNull{}); err != nil {
return fmt.Errorf("failed to initialize the metrics: %v", err)
}
Expand Down
36 changes: 25 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ 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 {
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)
Expand All @@ -143,28 +143,42 @@ 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) UnmarshalConfig(homePath, configPath string) error {
cfgPath := fmt.Sprintf("%s/%s", homePath, configPath)
c.ConfigPath = cfgPath
if _, err := os.Stat(cfgPath); err == nil {
file, err := os.ReadFile(cfgPath)
if err != nil {
return err
}
// unmarshall them into the struct
if err = UnmarshalJSON(ctx.Codec, file, c); err != nil {
return err
}
// ensure config has []*relayer.Chain used for all chain operations
if err = InitChains(ctx, homePath, debug); err != nil {
if err = json.Unmarshal(file, c); err != nil {
return err
}
} else {
defConfig := defaultConfig(cfgPath)
c = &defConfig
*c = defConfig
}
c.ConfigPath = cfgPath
return nil
}

func (ctx *Context) InitConfig(homePath string, debug bool) error {
for _, c := range ctx.Config.Chains {
if err := c.Init(ctx.Codec); err != nil {
return err
}
chain, err := c.Build()
if err != nil {
return err
}
ctx.Config.chains = append(ctx.Config.chains, chain)
}

// ensure config has []*relayer.Chain used for all chain operations
if err := initChains(ctx, homePath, debug); err != nil {
return err
}
c.InitCoreConfig()
ctx.Config = c
ctx.Config.InitCoreConfig()
return nil
}

Expand Down
28 changes: 0 additions & 28 deletions config/marshaler.go

This file was deleted.

2 changes: 1 addition & 1 deletion log/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func InitLogger(logLevel, format, output string) error {
case "stderr":
return InitLoggerWithWriter(logLevel, format, os.Stderr)
default:
return errors.New("invalid log output")
return errors.New(fmt.Sprintf("invalid log output: '%s'", output))
}
}

Expand Down
3 changes: 3 additions & 0 deletions provers/mock/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

mocktypes "github.com/datachainlab/ibc-mock-client/modules/light-clients/xx-mock/types"
"github.com/hyperledger-labs/yui-relayer/core"
"github.com/hyperledger-labs/yui-relayer/log"
)

type Prover struct {
Expand All @@ -27,6 +28,8 @@ func NewProver(chain core.Chain, config ProverConfig) *Prover {
}

func (pr *Prover) Init(homePath string, timeout time.Duration, codec codec.ProtoCodecMarshaler, debug bool) error {
logger := log.GetLogger()
logger.Info("mock prover is initialized.")
return nil
}

Expand Down

0 comments on commit 4ecddcb

Please sign in to comment.