11package cmd
22
33import (
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
1511func 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- }
0 commit comments