Skip to content

Commit

Permalink
Aliases Feature made (#17)
Browse files Browse the repository at this point in the history
* Aliases Feature made

Signed-off-by: Jyotin Goel <b22ai063@iitj.ac.in>

* Updates the alias commands

Signed-off-by: Saahil Bhavsar <saahil_bhavsar@outlook.com>

---------

Signed-off-by: Jyotin Goel <b22ai063@iitj.ac.in>
Signed-off-by: Saahil Bhavsar <saahil_bhavsar@outlook.com>
Co-authored-by: Saahil Bhavsar <saahil_bhavsar@outlook.com>
  • Loading branch information
gjyotin305 and SaahilNotSahil authored Jul 23, 2024
1 parent 81c12e3 commit b0a51da
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 25 deletions.
51 changes: 51 additions & 0 deletions cli/cmd/alias/add.go
Original file line number Diff line number Diff line change
@@ -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)
}
98 changes: 96 additions & 2 deletions cli/cmd/alias/alias.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 8 additions & 17 deletions cli/cmd/alias/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
},
Expand Down
78 changes: 78 additions & 0 deletions cli/cmd/alias/remove.go
Original file line number Diff line number Diff line change
@@ -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)
}
58 changes: 58 additions & 0 deletions cli/cmd/alias/update.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 6 additions & 6 deletions internal/pkg/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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")
}
}

0 comments on commit b0a51da

Please sign in to comment.