diff --git a/cmd/add/add.go b/cmd/add/add.go new file mode 100644 index 0000000..a255dbd --- /dev/null +++ b/cmd/add/add.go @@ -0,0 +1,53 @@ +package add + +import ( + "errors" + "git.denysoft.de/CubeBit/ssh-hub/util" + "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "os" + "regexp" +) + +var addCmd = &cobra.Command{ + Use: "add", + Short: "Add new command with alias", + Args: func(cmd *cobra.Command, args []string) error { + if err := cobra.ExactArgs(2)(cmd, args); err != nil { + return err + } + + conf := viper.GetStringMapString("data") + if _, ok := conf[args[0]]; ok { + return errors.New("such alias already exists") + } + + if matched, err := regexp.MatchString("/[a-zA-Z|1-9-]{1,}", args[0]); !matched { + cobra.CheckErr(err) + return errors.New("alias is in false format. Example of correct one: /work/dev. Regex: /[a-zA-Z|1-9-]{1,}") + } + + return nil + }, + Run: func(cmd *cobra.Command, args []string) { + cfg := viper.GetStringMapString("data") + cfg[args[0]] = args[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 addCmd +} diff --git a/cmd/delete/delete.go b/cmd/delete/delete.go new file mode 100644 index 0000000..1995799 --- /dev/null +++ b/cmd/delete/delete.go @@ -0,0 +1,47 @@ +package delete + +import ( + "errors" + "git.denysoft.de/CubeBit/ssh-hub/util" + "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "os" +) + +var deleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete alias and it's command", + Args: func(cmd *cobra.Command, args []string) error { + if err := cobra.ExactArgs(1)(cmd, args); err != nil { + return err + } + + conf := viper.GetStringMapString("data") + if _, ok := conf[args[0]]; !ok { + return errors.New("such alias doesn't exist") + } + + return nil + }, + Run: func(cmd *cobra.Command, args []string) { + cfg := viper.GetStringMapString("data") + delete(cfg, args[0]) + + 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 deleteCmd +} diff --git a/cmd/root.go b/cmd/root.go index eb85ddc..fea731b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,8 @@ import ( "encoding/json" "errors" "fmt" + "git.denysoft.de/CubeBit/ssh-hub/cmd/add" + 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" "github.com/spf13/cobra" @@ -33,6 +35,8 @@ func init() { cobra.OnInitialize(initCfg) rootCmd.AddCommand(list.Cmd()) rootCmd.AddCommand(exec.Cmd()) + rootCmd.AddCommand(add.Cmd()) + rootCmd.AddCommand(cobraDelete.Cmd()) } func initCfg() { diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..5250ab9 --- /dev/null +++ b/util/util.go @@ -0,0 +1,21 @@ +package util + +import ( + "encoding/json" + "github.com/spf13/cobra" + "os" +) + +func SaveConfig(data *map[string]string) error { + jsonData, err := json.Marshal(data) + cobra.CheckErr(err) + + homeDir, err := os.UserHomeDir() + cobra.CheckErr(err) + f, err := os.Create(homeDir + "/.ssh-hub-config.json") + cobra.CheckErr(err) + _, err = f.Write(jsonData) + cobra.CheckErr(err) + + return nil +}