From b0a51da086a39b2ea1c3cc0678226e9d1d1057e8 Mon Sep 17 00:00:00 2001 From: Jyotin Goel <120490013+gjyotin305@users.noreply.github.com> Date: Wed, 24 Jul 2024 01:47:47 +0530 Subject: [PATCH] Aliases Feature made (#17) * Aliases Feature made Signed-off-by: Jyotin Goel * Updates the alias commands Signed-off-by: Saahil Bhavsar --------- Signed-off-by: Jyotin Goel Signed-off-by: Saahil Bhavsar Co-authored-by: Saahil Bhavsar --- cli/cmd/alias/add.go | 51 ++++++++++++++++ cli/cmd/alias/alias.go | 98 ++++++++++++++++++++++++++++++- cli/cmd/alias/list.go | 25 +++----- cli/cmd/alias/remove.go | 78 ++++++++++++++++++++++++ cli/cmd/alias/update.go | 58 ++++++++++++++++++ internal/pkg/selector/selector.go | 12 ++-- 6 files changed, 297 insertions(+), 25 deletions(-) create mode 100644 cli/cmd/alias/add.go create mode 100644 cli/cmd/alias/remove.go create mode 100644 cli/cmd/alias/update.go diff --git a/cli/cmd/alias/add.go b/cli/cmd/alias/add.go new file mode 100644 index 0000000..ba6b5e0 --- /dev/null +++ b/cli/cmd/alias/add.go @@ -0,0 +1,51 @@ +package alias + +import ( + "bufio" + "fmt" + "os" + + "github.com/spf13/cobra" +) + +// addCmd represents the alias add command +var addCmd = &cobra.Command{ + Use: "add", + Short: "Add a new alias", + Long: "Add a new alias to the aliases file", + Run: func(cmd *cobra.Command, args []string) { + var alias string + var value string + var description string + + if len(os.Args) < 4 { + fmt.Println("Invalid number of arguments") + + os.Exit(1) + } + + value = os.Args[3] + + fmt.Print("Please provide a short, memorable name for the alias: ") + _, err := fmt.Scanln(&alias) + cobra.CheckErr(err) + + fmt.Print("Provide a short description for the alias (Press ENTER to leave blank): ") + reader := bufio.NewReader(os.Stdin) + description, err = reader.ReadString('\n') + cobra.CheckErr(err) + description = description[:len(description)-1] + + aliases := new(Aliases) + cobra.CheckErr(aliases.ReadFromFile()) + + aliases.Add(alias, value, description) + cobra.CheckErr(aliases.WriteToFile()) + + fmt.Printf("Successfully added the alias to the file: %s\n", AliasFilePath) + }, +} + +func init() { + AliasCmd.AddCommand(addCmd) +} diff --git a/cli/cmd/alias/alias.go b/cli/cmd/alias/alias.go index 6294c15..cb1c8d6 100644 --- a/cli/cmd/alias/alias.go +++ b/cli/cmd/alias/alias.go @@ -1,19 +1,113 @@ package alias import ( + "fmt" "os" "github.com/spf13/cobra" + "gopkg.in/yaml.v2" ) type Alias struct { - Name string `yaml:"name"` Description string `yaml:"description"` Value string `yaml:"value"` } type Aliases struct { - Aliases []Alias `yaml:"aliases"` + Aliases map[string]Alias `yaml:"aliases"` +} + +func (aliases *Aliases) ReadFromFile() error { + data, err := os.ReadFile(AliasFilePath) + if err != nil { + if os.IsNotExist(err) { + return nil + } + + fmt.Println("Error reading the aliases file") + + return err + } + + err = yaml.Unmarshal(data, aliases) + if err != nil { + fmt.Println("Invalid yaml file") + + return err + } + + return nil +} + +func (aliases *Aliases) Add(name, value, description string) { + if _, ok := aliases.Aliases[name]; ok { + fmt.Printf( + "An alias with the name \"%s\" already exists. Please try with a different name", + name, + ) + + return + } + + aliases.Aliases[name] = Alias{ + Value: value, + Description: description, + } +} + +func (aliases *Aliases) Update(name, fieldToUpdate, fieldNewValue string) bool { + if fieldNewValue == "" { + return false + } + + switch fieldToUpdate { + case "name": + aliases.Aliases[fieldNewValue] = aliases.Aliases[name] + + delete(aliases.Aliases, name) + case "value": + description := aliases.Aliases[name].Description + + aliases.Aliases[name] = Alias{ + Value: fieldNewValue, + Description: description, + } + case "description": + value := aliases.Aliases[name].Value + + aliases.Aliases[name] = Alias{ + Value: value, + Description: fieldNewValue, + } + } + + return true +} + +func (aliases *Aliases) Remove(name string) { + delete(aliases.Aliases, name) +} + +func (aliases *Aliases) RemoveAll() { + aliases.Aliases = map[string]Alias{} +} + +func (aliases *Aliases) WriteToFile() error { + data, err := yaml.Marshal(aliases) + if err != nil { + fmt.Println("Error marshalling the aliases") + + return err + } + + err = os.WriteFile(AliasFilePath, data, 0644) + if err != nil { + fmt.Println("Error writing the aliases file") + + return err + } + + return nil } var AliasFilePath string diff --git a/cli/cmd/alias/list.go b/cli/cmd/alias/list.go index f99a22c..b7ec19a 100644 --- a/cli/cmd/alias/list.go +++ b/cli/cmd/alias/list.go @@ -2,12 +2,10 @@ package alias import ( "fmt" - "log" "os" "github.com/devlup-labs/spok/internal/pkg/selector" "github.com/spf13/cobra" - "gopkg.in/yaml.v2" ) // listCmd represents the alias list command @@ -16,30 +14,23 @@ var listCmd = &cobra.Command{ Short: "List all aliases", Long: `List all aliases`, Run: func(cmd *cobra.Command, args []string) { - data, err := os.ReadFile(AliasFilePath) - if err != nil { - return - } - aliases := new(Aliases) + cobra.CheckErr(aliases.ReadFromFile()) - err = yaml.Unmarshal(data, aliases) - if err != nil { - log.Println("Error reading the aliases file") + if len(aliases.Aliases) == 0 { + fmt.Printf("No aliases found in the file: %s\n", AliasFilePath) - return + os.Exit(0) } - menu := selector.NewMenu("Choose your alias:") + menu := selector.NewMenu("List of aliases:") - for _, alias := range aliases.Aliases { - menu.AddItem(alias.Name, alias.Value) + for alias, value := range aliases.Aliases { + menu.AddItem(alias, value.Value) } choice := menu.Display() - - numLinesToClear := len(menu.MenuItems) + 1 - selector.ClearMenu(numLinesToClear) + menu.Clear() fmt.Println(choice) }, diff --git a/cli/cmd/alias/remove.go b/cli/cmd/alias/remove.go new file mode 100644 index 0000000..64a3f1f --- /dev/null +++ b/cli/cmd/alias/remove.go @@ -0,0 +1,78 @@ +package alias + +import ( + "fmt" + "os" + "strings" + + "github.com/devlup-labs/spok/internal/pkg/selector" + "github.com/spf13/cobra" +) + +// removeCmd represents the alias remove command +var removeCmd = &cobra.Command{ + Use: "remove", + Short: "Remove an/all alias(es)", + Long: "Remove an/all alias(es)", + Run: func(cmd *cobra.Command, args []string) { + aliases := new(Aliases) + cobra.CheckErr(aliases.ReadFromFile()) + + all, _ := cmd.Flags().GetBool("all") + + if all { + userInput := "n" + + fmt.Print("Are you sure you want to remove all the aliases? [y/N]: ") + fmt.Scanln(&userInput) + + if strings.ToLower(userInput) == "y" { + aliases.RemoveAll() + cobra.CheckErr(aliases.WriteToFile()) + + fmt.Printf("Removed all the aliases from the file: %s\n", AliasFilePath) + + os.Exit(0) + } else { + os.Exit(0) + } + } + + menu := selector.NewMenu("Choose alias to remove:") + + for alias, _ := range aliases.Aliases { + menu.AddItem(alias, alias) + } + + choice := menu.Display() + menu.Clear() + + userInput := "n" + + fmt.Printf("Are you sure you want to remove the alias \"%s\"? [y/N]: ", choice) + fmt.Scanln(&userInput) + + if strings.ToLower(userInput) == "y" { + aliases.Remove(choice) + cobra.CheckErr(aliases.WriteToFile()) + + fmt.Printf("Removed the alias \"%s\" from the file: %s\n", choice, AliasFilePath) + + os.Exit(0) + } else { + os.Exit(0) + } + cobra.CheckErr(aliases.WriteToFile()) + + fmt.Printf( + "Successfully removed the alias \"%s\" from the file: %s\n", + choice, + AliasFilePath, + ) + }, +} + +func init() { + removeCmd.Flags().BoolP("all", "a", false, "To remove all the aliases") + AliasCmd.AddCommand(removeCmd) +} diff --git a/cli/cmd/alias/update.go b/cli/cmd/alias/update.go new file mode 100644 index 0000000..0e8464a --- /dev/null +++ b/cli/cmd/alias/update.go @@ -0,0 +1,58 @@ +package alias + +import ( + "bufio" + "fmt" + "os" + + "github.com/devlup-labs/spok/internal/pkg/selector" + "github.com/spf13/cobra" +) + +// updateCmd represents the alias update command +var updateCmd = &cobra.Command{ + Use: "update", + Short: "Update an alias", + Long: "Update an alias", + Run: func(cmd *cobra.Command, args []string) { + aliases := new(Aliases) + cobra.CheckErr(aliases.ReadFromFile()) + + menu := selector.NewMenu("Choose your alias:") + + for alias, _ := range aliases.Aliases { + menu.AddItem(alias, alias) + } + + aliasToUpdate := menu.Display() + menu.Clear() + + menu = selector.NewMenu("What do you want to update?") + + menu.AddItem("Name", "name") + menu.AddItem("Value", "value") + menu.AddItem("Description", "description") + + fieldToUpdate := menu.Display() + menu.Clear() + + var fieldNewValue string + + fmt.Printf("Enter a new %s for the alias (Leave blank to keep unchanged): ", fieldToUpdate) + reader := bufio.NewReader(os.Stdin) + fieldNewValue, err := reader.ReadString('\n') + cobra.CheckErr(err) + fieldNewValue = fieldNewValue[:len(fieldNewValue)-1] + + if ok := aliases.Update(aliasToUpdate, fieldToUpdate, fieldNewValue); !ok { + os.Exit(0) + } + cobra.CheckErr(aliases.WriteToFile()) + + fmt.Printf("Successfully updated the alias in the file: %s\n", AliasFilePath) + }, +} + +func init() { + AliasCmd.AddCommand(updateCmd) +} diff --git a/internal/pkg/selector/selector.go b/internal/pkg/selector/selector.go index 9a2e692..4968a17 100644 --- a/internal/pkg/selector/selector.go +++ b/internal/pkg/selector/selector.go @@ -92,6 +92,12 @@ func (m *Menu) Display() string { } } +func (m *Menu) Clear() { + for i := 0; i < (len(m.MenuItems) + 1); i++ { + fmt.Print("\033[F\033[K") + } +} + func getInput() byte { t, _ := term.Open("/dev/tty") @@ -124,9 +130,3 @@ func NewMenu(prompt string) *Menu { MenuItems: make([]*MenuItem, 0), } } - -func ClearMenu(lines int) { - for i := 0; i < lines; i++ { - fmt.Print("\033[F\033[K") - } -}