-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
81c12e3
commit b0a51da
Showing
6 changed files
with
297 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters