Skip to content

Commit

Permalink
Merge pull request hyperledger-labs#127 from dongrie/path-config-update
Browse files Browse the repository at this point in the history
Update path config
  • Loading branch information
siburu authored Jan 30, 2024
2 parents 7178056 + be89032 commit adb402a
Show file tree
Hide file tree
Showing 19 changed files with 424 additions and 191 deletions.
36 changes: 1 addition & 35 deletions cmd/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"os"
"path"

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

func chainsCmd(ctx *config.Context) *cobra.Command {
Expand All @@ -37,7 +35,7 @@ func chainsAddDirCmd(ctx *config.Context) *cobra.Command {
if err := filesAdd(ctx, args[0]); err != nil {
return err
}
return overWriteConfig(ctx, cmd)
return ctx.Config.OverWriteConfig()
},
}

Expand Down Expand Up @@ -78,35 +76,3 @@ func filesAdd(ctx *config.Context, dir string) error {
}
return nil
}

func overWriteConfig(ctx *config.Context, cmd *cobra.Command) error {
home, err := cmd.Flags().GetString(flags.FlagHome)
if err != nil {
return err
}

cfgPath := path.Join(home, "config", "config.yaml")
if _, err = os.Stat(cfgPath); err == nil {
viper.SetConfigFile(cfgPath)
if err = viper.ReadInConfig(); err == nil {
// ensure validateConfig runs properly
err = config.InitChains(ctx, homePath, debug)
if err != nil {
return err
}

// marshal the new config
out, err := config.MarshalJSON(*ctx.Config)
if err != nil {
return err
}

// overwrite the config file
err = os.WriteFile(viper.ConfigFileUsed(), out, 0600)
if err != nil {
return err
}
}
}
return err
}
110 changes: 5 additions & 105 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"path"

"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,62 +18,23 @@ 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 {
if err := ctx.Config.CreateConfig(); err != nil {
return err
}

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

// 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
}
}

// Then create the file...
f, err := os.Create(cfgPath)
if err != nil {
return err
}
defer f.Close()

// And write the default config to that location...
if _, err = f.Write(defaultConfig()); err != nil {
return err
}

// And return no error...
return nil
}

// Otherwise, the config file exists, and an error is returned...
return fmt.Errorf("config already exists: %s", cfgPath)
return nil
},
}
return cmd
Expand All @@ -90,75 +47,18 @@ 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)
}

out, err := config.MarshalJSON(*ctx.Config)
if err != nil {
return err
}

fmt.Println(string(out))
return nil
},
}

return cmd
}

func defaultConfig() []byte {
bz, err := json.Marshal(config.DefaultConfig())
if err != nil {
panic(err)
}
return bz
}

// 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")
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)
}

// 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)
}

// 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)
}
}
} else {
defConfig := config.DefaultConfig()
ctx.Config = &defConfig
}
return nil
}
4 changes: 2 additions & 2 deletions cmd/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func pathsAddCmd(ctx *config.Context) *cobra.Command {
}
}

return overWriteConfig(ctx, cmd)
return ctx.Config.OverWriteConfig()
},
}
return fileFlag(cmd)
Expand Down Expand Up @@ -144,7 +144,7 @@ func pathsEditCmd(ctx *config.Context) *cobra.Command {
default:
return fmt.Errorf("invalid key: %s. Valid keys are: client-id, channel-id, connection-id, port-id", key)
}
return overWriteConfig(ctx, cmd)
return ctx.Config.OverWriteConfig()
},
}
return cmd
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 @@ -74,11 +75,11 @@ 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)
}
if err := initConfig(ctx, rootCmd); err != nil {
if err := ctx.Config.InitConfig(ctx, homePath, configPath, debug); err != nil {
return fmt.Errorf("failed to initialize the configuration: %v", err)
}
if err := initLogger(ctx); err != nil {
Expand Down
16 changes: 10 additions & 6 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func createClientsCmd(ctx *config.Context) *cobra.Command {
" path by querying headers from each chain and then sending the corresponding create-client messages",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
pathName := args[0]
c, src, dst, err := ctx.Config.ChainsFromPath(pathName)
if err != nil {
return err
}
Expand Down Expand Up @@ -93,7 +94,7 @@ func createClientsCmd(ctx *config.Context) *cobra.Command {
dstHeight = clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height)
}

return core.CreateClients(c[src], c[dst], srcHeight, dstHeight)
return core.CreateClients(pathName, c[src], c[dst], srcHeight, dstHeight)
},
}
cmd.Flags().Uint64(flagSrcHeight, defaultSrcHeight, "src header at this height is submitted to dst chain")
Expand Down Expand Up @@ -136,7 +137,8 @@ func createConnectionCmd(ctx *config.Context) *cobra.Command {
a connection between two chains with a configured path in the config file`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
pathName := args[0]
c, src, dst, err := ctx.Config.ChainsFromPath(pathName)
if err != nil {
return err
}
Expand All @@ -154,7 +156,7 @@ func createConnectionCmd(ctx *config.Context) *cobra.Command {
return err
}

return core.CreateConnection(c[src], c[dst], to)
return core.CreateConnection(pathName, c[src], c[dst], to)
},
}

Expand All @@ -169,7 +171,8 @@ func createChannelCmd(ctx *config.Context) *cobra.Command {
create a channel between two chains with a configured path in the config file`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
pathName := args[0]
c, src, dst, err := ctx.Config.ChainsFromPath(pathName)
if err != nil {
return err
}
Expand All @@ -186,7 +189,8 @@ func createChannelCmd(ctx *config.Context) *cobra.Command {
if _, err = c[dst].GetAddress(); err != nil {
return err
}
return core.CreateChannel(c[src], c[dst], to)

return core.CreateChannel(pathName, c[src], c[dst], to)
},
}

Expand Down
Loading

0 comments on commit adb402a

Please sign in to comment.