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
Changes from 1 commit
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
60 changes: 58 additions & 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 All @@ -23,6 +22,7 @@ func configCmd(ctx *config.Context) *cobra.Command {
cmd.AddCommand(
configShowCmd(ctx),
configInitCmd(),
configEditCmd(ctx),
Copy link
Contributor

@siburu siburu Aug 22, 2023

Choose a reason for hiding this comment

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

Should you add this feature to the paths subcommand instead of the config subcommand?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

move to the paths subcommand
e39fd4c

)

return cmd
Expand Down Expand Up @@ -116,6 +116,62 @@ func configShowCmd(ctx *config.Context) *cobra.Command {
return cmd
}

func configEditCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "edit [path-name] [key] [value]",
Aliases: []string{"e"},
Short: "Edit the config file",
RunE: func(cmd *cobra.Command, args []string) error {
home, err := cmd.Flags().GetString(flags.FlagHome)
if err != nil {
return err
}
cfgPath := path.Join(home, "config", "config.yaml")
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
if _, err := os.Stat(home); os.IsNotExist(err) {
return fmt.Errorf("home path does not exist: %s", home)
}
return fmt.Errorf("config does not exist: %s", cfgPath)
}
pathName := args[0]
key := args[1]
value := args[2]
configPath, err := ctx.Config.Paths.Get(pathName)
if err != nil {
return err
}
switch key {
case "client-id":
configPath.Src.ClientID = value
configPath.Dst.ClientID = value
Copy link
Contributor

Choose a reason for hiding this comment

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

In general, src client id can be different from dst client id.
The same can be said about connection id, channel id, and port id.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add src/dst args

$RLY paths edit $PATH_NAME client-id src clinet-id-src
$RLY paths edit $PATH_NAME client-id dst clinet-id-dst

case "channel-id":
configPath.Src.ChannelID = value
configPath.Dst.ChannelID = value
case "connection-id":
configPath.Src.ConnectionID = value
configPath.Dst.ConnectionID = value
case "port-id":
configPath.Src.PortID = value
configPath.Dst.PortID = value
default:
return fmt.Errorf("invalid key: %s. Valid keys are: client-id, channel-id, connection-id, port-id", key)
}
ctx.Config.Paths[pathName] = configPath
out, err := config.MarshalJSON(*ctx.Config)
if err != nil {
return err
}
err = os.WriteFile(cfgPath, out, 0600)
if err != nil {
return err
}
fmt.Println("config file updated")
return nil
},
}
return cmd
}

func defaultConfig() []byte {
bz, err := json.Marshal(config.DefaultConfig())
if err != nil {
Expand All @@ -136,7 +192,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
Loading