added replace command
All checks were successful
Go / build-test-publish (1.23.4) (push) Successful in 52s

This commit is contained in:
Denys Seredenko
2025-01-15 11:42:32 +01:00
parent ca2d6bbc67
commit afffe6e9e5
2 changed files with 46 additions and 0 deletions

44
cmd/replace/replace.go Normal file
View File

@@ -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
}

View File

@@ -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() {