Skip to content

Commit

Permalink
Fix config.yaml to config.json
Browse files Browse the repository at this point in the history
Refactoring config path

Signed-off-by: Dongri Jin <[email protected]>
  • Loading branch information
dongrie committed Dec 12, 2023
1 parent 6cd8ddf commit 4a6607c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 75 deletions.
89 changes: 25 additions & 64 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/hyperledger-labs/yui-relayer/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func configCmd(ctx *config.Context) *cobra.Command {
Expand All @@ -22,44 +20,26 @@ func configCmd(ctx *config.Context) *cobra.Command {

cmd.AddCommand(
configShowCmd(ctx),
configInitCmd(),
configInitCmd(ctx),
)

return cmd
}

// Command for inititalizing an empty config at the --home location
func configInitCmd() *cobra.Command {
func configInitCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Aliases: []string{"i"},
Short: "Creates a default home directory at path defined by --home",
RunE: func(cmd *cobra.Command, args []string) error {
home, err := cmd.Flags().GetString(flags.FlagHome)
if err != nil {
return err
}

cfgDir := path.Join(home, "config")
cfgPath := path.Join(cfgDir, "config.yaml")

cfgPath := ctx.Config.ConfigPath
// If the config doesn't exist...
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
// And the config folder doesn't exist...
if _, err := os.Stat(cfgDir); os.IsNotExist(err) {
// And the home folder doesn't exist
if _, err := os.Stat(home); os.IsNotExist(err) {
// Create the home folder
if err = os.Mkdir(home, os.ModePerm); err != nil {
return err
}
}
// Create the home config folder
if err = os.Mkdir(cfgDir, os.ModePerm); err != nil {
return err
}
dirPath := filepath.Dir(cfgPath)
if err := os.MkdirAll(dirPath, os.ModePerm); err != nil {
return err
}

// Then create the file...
f, err := os.Create(cfgPath)
if err != nil {
Expand Down Expand Up @@ -90,16 +70,8 @@ func configShowCmd(ctx *config.Context) *cobra.Command {
Aliases: []string{"s", "list", "l"},
Short: "Prints current configuration",
RunE: func(cmd *cobra.Command, args []string) error {
home, err := cmd.Flags().GetString(flags.FlagHome)
if err != nil {
return err
}

cfgPath := path.Join(home, "config", "config.yaml")
cfgPath := ctx.Config.ConfigPath
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
if _, err := os.Stat(home); os.IsNotExist(err) {
return fmt.Errorf("home path does not exist: %s", home)
}
return fmt.Errorf("config does not exist: %s", cfgPath)
}

Expand All @@ -117,7 +89,7 @@ func configShowCmd(ctx *config.Context) *cobra.Command {
}

func defaultConfig() []byte {
bz, err := json.Marshal(config.DefaultConfig(homePath))
bz, err := json.Marshal(config.DefaultConfig(fmt.Sprintf("%s/%s", homePath, configPath)))
if err != nil {
panic(err)
}
Expand All @@ -126,38 +98,27 @@ func defaultConfig() []byte {

// initConfig reads in config file and ENV variables if set.
func initConfig(ctx *config.Context, cmd *cobra.Command) error {
home, err := cmd.PersistentFlags().GetString(flags.FlagHome)
if err != nil {
return err
}

cfgPath := path.Join(home, "config", "config.yaml")
cfgPath := ctx.Config.ConfigPath
if _, err := os.Stat(cfgPath); err == nil {
viper.SetConfigFile(cfgPath)
if err := viper.ReadInConfig(); err == nil {
// read the config file bytes
file, err := os.ReadFile(viper.ConfigFileUsed())
if err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
}
file, err := os.ReadFile(cfgPath)
if err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
}

// unmarshall them into the struct
err = config.UnmarshalJSON(ctx.Codec, file, ctx.Config)
if err != nil {
fmt.Println("Error unmarshalling config:", err)
os.Exit(1)
}
// unmarshall them into the struct
if err = config.UnmarshalJSON(ctx.Codec, file, ctx.Config); err != nil {
fmt.Println("Error unmarshalling config:", err)
os.Exit(1)
}

// ensure config has []*relayer.Chain used for all chain operations
err = config.InitChains(ctx, homePath, debug)
if err != nil {
fmt.Println("Error parsing chain config:", err)
os.Exit(1)
}
// ensure config has []*relayer.Chain used for all chain operations
if err = config.InitChains(ctx, homePath, debug); err != nil {
fmt.Println("Error parsing chain config:", err)
os.Exit(1)
}
} else {
defConfig := config.DefaultConfig(homePath)
defConfig := config.DefaultConfig(fmt.Sprintf("%s/%s", homePath, configPath))
ctx.Config = &defConfig
}
ctx.Config.InitCoreConfig()
Expand Down
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
homePath string
debug bool
defaultHome = os.ExpandEnv("$HOME/.yui-relayer")
configPath = "config/config.json"
)

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down Expand Up @@ -53,7 +54,7 @@ func Execute(modules ...config.ModuleI) error {
for _, module := range modules {
module.RegisterInterfaces(codec.InterfaceRegistry())
}
ctx := &config.Context{Modules: modules, Config: &config.Config{HomePath: homePath}, Codec: codec}
ctx := &config.Context{Modules: modules, Config: &config.Config{ConfigPath: fmt.Sprintf("%s/%s", homePath, configPath)}, Codec: codec}

// Register subcommands

Expand All @@ -74,7 +75,7 @@ func Execute(modules ...config.ModuleI) error {
}

rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
// reads `homeDir/config/config.yaml` into `var config *Config` before each command
// reads `homeDir/config/config.json` into `var config *Config` before each command
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return fmt.Errorf("failed to bind the flag set to the configuration: %v", err)
}
Expand Down
16 changes: 7 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"time"

"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -19,15 +18,15 @@ type Config struct {
// cache
chains Chains `yaml:"-" json:"-"`

HomePath string `yaml:"-" json:"-"`
ConfigPath string `yaml:"-" json:"-"`
}

func DefaultConfig(homePath string) Config {
func DefaultConfig(configPath string) Config {
return Config{
Global: newDefaultGlobalConfig(),
Chains: []core.ChainProverConfig{},
Paths: core.Paths{},
HomePath: homePath,
Global: newDefaultGlobalConfig(),
Chains: []core.ChainProverConfig{},
Paths: core.Paths{},
ConfigPath: configPath,
}
}

Expand Down Expand Up @@ -148,8 +147,7 @@ func (c *Config) OverWriteConfig() error {
if err != nil {
return err
}
cfgPath := path.Join(c.HomePath, "config", "config.yaml")
if err := os.WriteFile(cfgPath, configData, 0600); err != nil {
if err := os.WriteFile(c.ConfigPath, configData, 0600); err != nil {
return err
}
return nil
Expand Down

0 comments on commit 4a6607c

Please sign in to comment.