diff --git a/cmd/replace/replace.go b/cmd/replace/replace.go new file mode 100644 index 0000000..fc3de90 --- /dev/null +++ b/cmd/replace/replace.go @@ -0,0 +1,44 @@ +package replace + +import ( + "git.denysoft.de/CubeBit/ssh-hub/util" + "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "os" + "strings" +) + +var replaceCmd = &cobra.Command{ + Use: "replace", + Short: "Replace in string to new string in all commands", + Args: func(cmd *cobra.Command, args []string) error { + if err := cobra.ExactArgs(2)(cmd, args); err != nil { + return err + } + return nil + }, + Run: func(cmd *cobra.Command, args []string) { + cfg := viper.GetStringMapString("data") + + for alias, command := range cfg { + cfg[alias] = strings.Replace(command, args[0], args[1], -1) + } + + err := util.SaveConfig(&cfg) + cobra.CheckErr(err) + + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.AppendHeader(table.Row{"Alias", "Command"}) + + for alias, command := range cfg { + t.AppendRow(table.Row{alias, command}) + } + t.Render() + }, +} + +func Cmd() *cobra.Command { + return replaceCmd +} diff --git a/cmd/root.go b/cmd/root.go index fea731b..465e190 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,6 +8,7 @@ import ( cobraDelete "git.denysoft.de/CubeBit/ssh-hub/cmd/delete" "git.denysoft.de/CubeBit/ssh-hub/cmd/exec" "git.denysoft.de/CubeBit/ssh-hub/cmd/list" + "git.denysoft.de/CubeBit/ssh-hub/cmd/replace" "github.com/spf13/cobra" "github.com/spf13/viper" "os" @@ -37,6 +38,7 @@ func init() { rootCmd.AddCommand(exec.Cmd()) rootCmd.AddCommand(add.Cmd()) rootCmd.AddCommand(cobraDelete.Cmd()) + rootCmd.AddCommand(replace.Cmd()) } func initCfg() {