-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix config add-dir regression #406
Changes from 3 commits
c84e1e3
33d674c
ece2a00
074e1b8
a53a0ab
c9d0dce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ import ( | |
"time" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/cosmos/relayer/relayer" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
@@ -51,7 +52,8 @@ func configCmd() *cobra.Command { | |
cmd.AddCommand( | ||
configShowCmd(), | ||
configInitCmd(), | ||
configAddDirCmd(), | ||
configAddChainsCmd(), | ||
configAddPathsCmd(), | ||
) | ||
|
||
return cmd | ||
|
@@ -170,19 +172,17 @@ $ %s cfg i`, appName, defaultHome, appName)), | |
return cmd | ||
} | ||
|
||
func configAddDirCmd() *cobra.Command { | ||
func configAddChainsCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "add-dir [dir]", | ||
Aliases: []string{"ad"}, | ||
Args: cobra.ExactArgs(1), | ||
Short: `Add new chains and paths to the configuration file from a | ||
directory full of chain and path configuration, useful for adding testnet configurations`, | ||
Use: "add-chains [/path/to/chains/]", | ||
Args: cobra.ExactArgs(1), | ||
Short: `Add new chains to the configuration file from a | ||
directory full of chain configurations, useful for adding testnet configurations`, | ||
Example: strings.TrimSpace(fmt.Sprintf(` | ||
$ %s config add-dir configs/ | ||
$ %s cfg ad configs/`, appName, appName)), | ||
$ %s config add-chains configs/chains`, appName)), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
var out *Config | ||
if out, err = cfgFilesAdd(args[0]); err != nil { | ||
if out, err = cfgFilesAddChains(args[0]); err != nil { | ||
return err | ||
} | ||
return overWriteConfig(cmd, out) | ||
|
@@ -192,7 +192,27 @@ $ %s cfg ad configs/`, appName, appName)), | |
return cmd | ||
} | ||
|
||
func cfgFilesAdd(dir string) (cfg *Config, err error) { | ||
func configAddPathsCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "add-paths [/path/to/paths/]", | ||
Args: cobra.ExactArgs(1), | ||
Short: `Add new paths to the configuration file from a directory full of path configurations, useful for adding testnet configurations. | ||
Chain configuration files must be added before calling this command.`, | ||
Example: strings.TrimSpace(fmt.Sprintf(` | ||
$ %s config add-paths configs/paths`, appName)), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
var out *Config | ||
if out, err = cfgFilesAddPaths(args[0]); err != nil { | ||
return err | ||
} | ||
return overWriteConfig(cmd, out) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func cfgFilesAddChains(dir string) (cfg *Config, err error) { | ||
dir = path.Clean(dir) | ||
files, err := ioutil.ReadDir(dir) | ||
if err != nil { | ||
|
@@ -218,51 +238,69 @@ func cfgFilesAdd(dir string) (cfg *Config, err error) { | |
continue | ||
} | ||
|
||
if c.ChainID == "" && c.Key == "" && c.RPCAddr == "" { | ||
p := &relayer.Path{} | ||
if err = json.Unmarshal(byt, p); err != nil { | ||
fmt.Printf("failed to unmarshal file %s, skipping...\n", pth) | ||
continue | ||
} | ||
if err = cfg.AddChain(c); err != nil { | ||
fmt.Printf("%s: %s\n", pth, err.Error()) | ||
continue | ||
} | ||
fmt.Printf("added chain %s...\n", c.ChainID) | ||
} | ||
return cfg, nil | ||
} | ||
|
||
// In the case that order isn't added to the path, add it manually | ||
if p.Src.Order == "" || p.Dst.Order == "" { | ||
p.Src.Order = defaultOrder | ||
p.Dst.Order = defaultOrder | ||
} | ||
func cfgFilesAddPaths(dir string) (cfg *Config, err error) { | ||
dir = path.Clean(dir) | ||
files, err := ioutil.ReadDir(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg = config | ||
for _, f := range files { | ||
pth := fmt.Sprintf("%s/%s", dir, f.Name()) | ||
if f.IsDir() { | ||
fmt.Printf("directory at %s, skipping...\n", pth) | ||
continue | ||
} | ||
|
||
// If the version isn't added to the path, add it manually | ||
if p.Src.Version == "" { | ||
p.Src.Version = defaultVersion | ||
} | ||
if p.Dst.Version == "" { | ||
p.Dst.Version = defaultVersion | ||
} | ||
byt, err := ioutil.ReadFile(pth) | ||
if err != nil { | ||
fmt.Printf("failed to read file %s, skipping...\n", pth) | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
|
||
pthName := strings.Split(f.Name(), ".")[0] | ||
if err = config.ValidatePath(p); err != nil { | ||
fmt.Printf("%s: %s\n", pth, err.Error()) | ||
continue | ||
} | ||
if err = cfg.AddPath(pthName, p); err != nil { | ||
fmt.Printf("%s: %s\n", pth, err.Error()) | ||
continue | ||
} | ||
p := &relayer.Path{} | ||
if err = json.Unmarshal(byt, p); err != nil { | ||
fmt.Printf("failed to unmarshal file %s, skipping...\n", pth) | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
|
||
// For now, we assume that all chain files must have same filename as chain-id | ||
// this is to ensure non-chain files (global config) does not get parsed into chain struct. | ||
if c.ChainID != pthName { | ||
fmt.Printf("Skipping non chain file: %s\n", f.Name()) | ||
continue | ||
} | ||
// In the case that order isn't added to the path, add it manually | ||
if p.Src.Order == "" || p.Dst.Order == "" { | ||
p.Src.Order = defaultOrder | ||
p.Dst.Order = defaultOrder | ||
} | ||
|
||
if err = cfg.AddChain(c); err != nil { | ||
// If the version isn't added to the path, add it manually | ||
if p.Src.Version == "" { | ||
p.Src.Version = defaultVersion | ||
} | ||
if p.Dst.Version == "" { | ||
p.Dst.Version = defaultVersion | ||
} | ||
|
||
pthName := strings.Split(f.Name(), ".")[0] | ||
if err = config.ValidatePath(p); err != nil { | ||
fmt.Printf("%s: failed to validate path: %s\n", pth, err.Error()) | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
|
||
if err = cfg.AddPath(pthName, p); err != nil { | ||
fmt.Printf("%s: %s\n", pth, err.Error()) | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
} | ||
fmt.Printf("added chain %s...\n", c.ChainID) | ||
|
||
fmt.Printf("added path %s...\n", pthName) | ||
} | ||
|
||
return cfg, nil | ||
} | ||
|
||
|
@@ -448,10 +486,10 @@ func (c *Config) ValidatePath(p *relayer.Path) (err error) { | |
return fmt.Errorf("source must specify a version") | ||
} | ||
if err = c.ValidatePathEnd(p.Src); err != nil { | ||
return err | ||
return sdkerrors.Wrapf(err, "chain %s failed path validation", p.Src.ChainID) | ||
} | ||
if err = c.ValidatePathEnd(p.Dst); err != nil { | ||
return err | ||
return sdkerrors.Wrapf(err, "chain %s failed path validation", p.Dst.ChainID) | ||
} | ||
if _, err = p.GetStrategy(); err != nil { | ||
return err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should return the error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will update