Skip to content

Commit adb402a

Browse files
authored
Merge pull request #127 from dongrie/path-config-update
Update path config
2 parents 7178056 + be89032 commit adb402a

File tree

19 files changed

+424
-191
lines changed

19 files changed

+424
-191
lines changed

cmd/chains.go

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import (
66
"os"
77
"path"
88

9-
"github.com/cosmos/cosmos-sdk/client/flags"
109
"github.com/hyperledger-labs/yui-relayer/config"
1110
"github.com/hyperledger-labs/yui-relayer/core"
1211
"github.com/spf13/cobra"
13-
"github.com/spf13/viper"
1412
)
1513

1614
func chainsCmd(ctx *config.Context) *cobra.Command {
@@ -37,7 +35,7 @@ func chainsAddDirCmd(ctx *config.Context) *cobra.Command {
3735
if err := filesAdd(ctx, args[0]); err != nil {
3836
return err
3937
}
40-
return overWriteConfig(ctx, cmd)
38+
return ctx.Config.OverWriteConfig()
4139
},
4240
}
4341

@@ -78,35 +76,3 @@ func filesAdd(ctx *config.Context, dir string) error {
7876
}
7977
return nil
8078
}
81-
82-
func overWriteConfig(ctx *config.Context, cmd *cobra.Command) error {
83-
home, err := cmd.Flags().GetString(flags.FlagHome)
84-
if err != nil {
85-
return err
86-
}
87-
88-
cfgPath := path.Join(home, "config", "config.yaml")
89-
if _, err = os.Stat(cfgPath); err == nil {
90-
viper.SetConfigFile(cfgPath)
91-
if err = viper.ReadInConfig(); err == nil {
92-
// ensure validateConfig runs properly
93-
err = config.InitChains(ctx, homePath, debug)
94-
if err != nil {
95-
return err
96-
}
97-
98-
// marshal the new config
99-
out, err := config.MarshalJSON(*ctx.Config)
100-
if err != nil {
101-
return err
102-
}
103-
104-
// overwrite the config file
105-
err = os.WriteFile(viper.ConfigFileUsed(), out, 0600)
106-
if err != nil {
107-
return err
108-
}
109-
}
110-
}
111-
return err
112-
}

cmd/config.go

Lines changed: 5 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package cmd
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"os"
7-
"path"
86

9-
"github.com/cosmos/cosmos-sdk/client/flags"
107
"github.com/hyperledger-labs/yui-relayer/config"
118
"github.com/spf13/cobra"
12-
"github.com/spf13/viper"
139
)
1410

1511
func configCmd(ctx *config.Context) *cobra.Command {
@@ -22,62 +18,23 @@ func configCmd(ctx *config.Context) *cobra.Command {
2218

2319
cmd.AddCommand(
2420
configShowCmd(ctx),
25-
configInitCmd(),
21+
configInitCmd(ctx),
2622
)
2723

2824
return cmd
2925
}
3026

3127
// Command for inititalizing an empty config at the --home location
32-
func configInitCmd() *cobra.Command {
28+
func configInitCmd(ctx *config.Context) *cobra.Command {
3329
cmd := &cobra.Command{
3430
Use: "init",
3531
Aliases: []string{"i"},
3632
Short: "Creates a default home directory at path defined by --home",
3733
RunE: func(cmd *cobra.Command, args []string) error {
38-
home, err := cmd.Flags().GetString(flags.FlagHome)
39-
if err != nil {
34+
if err := ctx.Config.CreateConfig(); err != nil {
4035
return err
4136
}
42-
43-
cfgDir := path.Join(home, "config")
44-
cfgPath := path.Join(cfgDir, "config.yaml")
45-
46-
// If the config doesn't exist...
47-
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
48-
// And the config folder doesn't exist...
49-
if _, err := os.Stat(cfgDir); os.IsNotExist(err) {
50-
// And the home folder doesn't exist
51-
if _, err := os.Stat(home); os.IsNotExist(err) {
52-
// Create the home folder
53-
if err = os.Mkdir(home, os.ModePerm); err != nil {
54-
return err
55-
}
56-
}
57-
// Create the home config folder
58-
if err = os.Mkdir(cfgDir, os.ModePerm); err != nil {
59-
return err
60-
}
61-
}
62-
63-
// Then create the file...
64-
f, err := os.Create(cfgPath)
65-
if err != nil {
66-
return err
67-
}
68-
defer f.Close()
69-
70-
// And write the default config to that location...
71-
if _, err = f.Write(defaultConfig()); err != nil {
72-
return err
73-
}
74-
75-
// And return no error...
76-
return nil
77-
}
78-
79-
// Otherwise, the config file exists, and an error is returned...
80-
return fmt.Errorf("config already exists: %s", cfgPath)
37+
return nil
8138
},
8239
}
8340
return cmd
@@ -90,75 +47,18 @@ func configShowCmd(ctx *config.Context) *cobra.Command {
9047
Aliases: []string{"s", "list", "l"},
9148
Short: "Prints current configuration",
9249
RunE: func(cmd *cobra.Command, args []string) error {
93-
home, err := cmd.Flags().GetString(flags.FlagHome)
94-
if err != nil {
95-
return err
96-
}
97-
98-
cfgPath := path.Join(home, "config", "config.yaml")
50+
cfgPath := ctx.Config.ConfigPath
9951
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
100-
if _, err := os.Stat(home); os.IsNotExist(err) {
101-
return fmt.Errorf("home path does not exist: %s", home)
102-
}
10352
return fmt.Errorf("config does not exist: %s", cfgPath)
10453
}
105-
10654
out, err := config.MarshalJSON(*ctx.Config)
10755
if err != nil {
10856
return err
10957
}
110-
11158
fmt.Println(string(out))
11259
return nil
11360
},
11461
}
11562

11663
return cmd
11764
}
118-
119-
func defaultConfig() []byte {
120-
bz, err := json.Marshal(config.DefaultConfig())
121-
if err != nil {
122-
panic(err)
123-
}
124-
return bz
125-
}
126-
127-
// initConfig reads in config file and ENV variables if set.
128-
func initConfig(ctx *config.Context, cmd *cobra.Command) error {
129-
home, err := cmd.PersistentFlags().GetString(flags.FlagHome)
130-
if err != nil {
131-
return err
132-
}
133-
134-
cfgPath := path.Join(home, "config", "config.yaml")
135-
if _, err := os.Stat(cfgPath); err == nil {
136-
viper.SetConfigFile(cfgPath)
137-
if err := viper.ReadInConfig(); err == nil {
138-
// read the config file bytes
139-
file, err := os.ReadFile(viper.ConfigFileUsed())
140-
if err != nil {
141-
fmt.Println("Error reading file:", err)
142-
os.Exit(1)
143-
}
144-
145-
// unmarshall them into the struct
146-
err = config.UnmarshalJSON(ctx.Codec, file, ctx.Config)
147-
if err != nil {
148-
fmt.Println("Error unmarshalling config:", err)
149-
os.Exit(1)
150-
}
151-
152-
// ensure config has []*relayer.Chain used for all chain operations
153-
err = config.InitChains(ctx, homePath, debug)
154-
if err != nil {
155-
fmt.Println("Error parsing chain config:", err)
156-
os.Exit(1)
157-
}
158-
}
159-
} else {
160-
defConfig := config.DefaultConfig()
161-
ctx.Config = &defConfig
162-
}
163-
return nil
164-
}

cmd/paths.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func pathsAddCmd(ctx *config.Context) *cobra.Command {
103103
}
104104
}
105105

106-
return overWriteConfig(ctx, cmd)
106+
return ctx.Config.OverWriteConfig()
107107
},
108108
}
109109
return fileFlag(cmd)
@@ -144,7 +144,7 @@ func pathsEditCmd(ctx *config.Context) *cobra.Command {
144144
default:
145145
return fmt.Errorf("invalid key: %s. Valid keys are: client-id, channel-id, connection-id, port-id", key)
146146
}
147-
return overWriteConfig(ctx, cmd)
147+
return ctx.Config.OverWriteConfig()
148148
},
149149
}
150150
return cmd

cmd/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
homePath string
2222
debug bool
2323
defaultHome = os.ExpandEnv("$HOME/.yui-relayer")
24+
configPath = "config/config.json"
2425
)
2526

2627
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -74,11 +75,11 @@ func Execute(modules ...config.ModuleI) error {
7475
}
7576

7677
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
77-
// reads `homeDir/config/config.yaml` into `var config *Config` before each command
78+
// reads `homeDir/config/config.json` into `var config *Config` before each command
7879
if err := viper.BindPFlags(cmd.Flags()); err != nil {
7980
return fmt.Errorf("failed to bind the flag set to the configuration: %v", err)
8081
}
81-
if err := initConfig(ctx, rootCmd); err != nil {
82+
if err := ctx.Config.InitConfig(ctx, homePath, configPath, debug); err != nil {
8283
return fmt.Errorf("failed to initialize the configuration: %v", err)
8384
}
8485
if err := initLogger(ctx); err != nil {

cmd/tx.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func createClientsCmd(ctx *config.Context) *cobra.Command {
5656
" path by querying headers from each chain and then sending the corresponding create-client messages",
5757
Args: cobra.ExactArgs(1),
5858
RunE: func(cmd *cobra.Command, args []string) error {
59-
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
59+
pathName := args[0]
60+
c, src, dst, err := ctx.Config.ChainsFromPath(pathName)
6061
if err != nil {
6162
return err
6263
}
@@ -93,7 +94,7 @@ func createClientsCmd(ctx *config.Context) *cobra.Command {
9394
dstHeight = clienttypes.NewHeight(latestHeight.GetRevisionNumber(), height)
9495
}
9596

96-
return core.CreateClients(c[src], c[dst], srcHeight, dstHeight)
97+
return core.CreateClients(pathName, c[src], c[dst], srcHeight, dstHeight)
9798
},
9899
}
99100
cmd.Flags().Uint64(flagSrcHeight, defaultSrcHeight, "src header at this height is submitted to dst chain")
@@ -136,7 +137,8 @@ func createConnectionCmd(ctx *config.Context) *cobra.Command {
136137
a connection between two chains with a configured path in the config file`),
137138
Args: cobra.ExactArgs(1),
138139
RunE: func(cmd *cobra.Command, args []string) error {
139-
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
140+
pathName := args[0]
141+
c, src, dst, err := ctx.Config.ChainsFromPath(pathName)
140142
if err != nil {
141143
return err
142144
}
@@ -154,7 +156,7 @@ func createConnectionCmd(ctx *config.Context) *cobra.Command {
154156
return err
155157
}
156158

157-
return core.CreateConnection(c[src], c[dst], to)
159+
return core.CreateConnection(pathName, c[src], c[dst], to)
158160
},
159161
}
160162

@@ -169,7 +171,8 @@ func createChannelCmd(ctx *config.Context) *cobra.Command {
169171
create a channel between two chains with a configured path in the config file`),
170172
Args: cobra.ExactArgs(1),
171173
RunE: func(cmd *cobra.Command, args []string) error {
172-
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
174+
pathName := args[0]
175+
c, src, dst, err := ctx.Config.ChainsFromPath(pathName)
173176
if err != nil {
174177
return err
175178
}
@@ -186,7 +189,8 @@ func createChannelCmd(ctx *config.Context) *cobra.Command {
186189
if _, err = c[dst].GetAddress(); err != nil {
187190
return err
188191
}
189-
return core.CreateChannel(c[src], c[dst], to)
192+
193+
return core.CreateChannel(pathName, c[src], c[dst], to)
190194
},
191195
}
192196

0 commit comments

Comments
 (0)