Skip to content
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

Add path config edit command #97

Merged
merged 4 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
siburu marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
Expand Down
49 changes: 47 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,59 @@ func pathsAddCmd(ctx *config.Context) *cobra.Command {
return fileFlag(cmd)
}

func pathsEditCmd(ctx *config.Context) *cobra.Command {
siburu marked this conversation as resolved.
Show resolved Hide resolved
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)
}
if err := overWriteConfig(ctx, cmd); err != nil {
return err
}
fmt.Println("config file updated")
Copy link
Contributor

@siburu siburu Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dongrie
This message is unnecessary and please delete it.
return overWriteConfig(ctx, cmd)

The other part looks perfect. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8923490
Fixed

return nil
},
}
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
Loading