Skip to content

Commit

Permalink
Merge pull request #97 from dongrie/edit-path-config-id
Browse files Browse the repository at this point in the history
Add path config edit command
  • Loading branch information
siburu authored Aug 24, 2023
2 parents 0fbda53 + 8923490 commit 43e8592
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
3 changes: 1 addition & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"

Expand Down Expand Up @@ -136,7 +135,7 @@ func initConfig(ctx *config.Context, cmd *cobra.Command) error {
viper.SetConfigFile(cfgPath)
if err := viper.ReadInConfig(); err == nil {
// read the config file bytes
file, err := ioutil.ReadFile(viper.ConfigFileUsed())
file, err := os.ReadFile(viper.ConfigFileUsed())
if err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
Expand Down
45 changes: 43 additions & 2 deletions cmd/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/hyperledger-labs/yui-relayer/config"
Expand All @@ -25,6 +24,7 @@ connection, and channel ids from both the source and destination chains as well
cmd.AddCommand(
pathsListCmd(ctx),
pathsAddCmd(ctx),
pathsEditCmd(ctx),
)

return cmd
Expand Down Expand Up @@ -108,14 +108,55 @@ func pathsAddCmd(ctx *config.Context) *cobra.Command {
return fileFlag(cmd)
}

func pathsEditCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "edit [path-name] [src or dst] [key] [value]",
Aliases: []string{"e"},
Short: "Edit the config file",
RunE: func(cmd *cobra.Command, args []string) error {
pathName := args[0]
srcDst := args[1]
key := args[2]
value := args[3]
configPath, err := ctx.Config.Paths.Get(pathName)
if err != nil {
return err
}
var pathEnd *core.PathEnd
switch srcDst {
case "src":
pathEnd = configPath.Src
case "dst":
pathEnd = configPath.Dst
default:
return fmt.Errorf("invalid src or dst: %s. Valid values are: src, dst", srcDst)
}
switch key {
case "client-id":
pathEnd.ClientID = value
case "channel-id":
pathEnd.ChannelID = value
case "connection-id":
pathEnd.ConnectionID = value
case "port-id":
pathEnd.PortID = value
default:
return fmt.Errorf("invalid key: %s. Valid keys are: client-id, channel-id, connection-id, port-id", key)
}
return overWriteConfig(ctx, cmd)
},
}
return cmd
}

func fileInputPathAdd(config *config.Config, file, name string) error {
// If the user passes in a file, attempt to read the chain config from that file
p := &core.Path{}
if _, err := os.Stat(file); err != nil {
return err
}

byt, err := ioutil.ReadFile(file)
byt, err := os.ReadFile(file)
if err != nil {
return err
}
Expand Down

0 comments on commit 43e8592

Please sign in to comment.