80 lines
1.7 KiB
Go
80 lines
1.7 KiB
Go
package cmd
|
|
|
|
import (
|
|
"axolotl/store"
|
|
"fmt"
|
|
"os"
|
|
"slices"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var aliasDesc string
|
|
|
|
var aliasCmd = &cobra.Command{
|
|
Use: "alias [name] [command]", Short: "Manage aliases", Args: cobra.MaximumNArgs(2),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
w := cmd.OutOrStdout()
|
|
if len(args) == 0 {
|
|
PrintAliases(w, cfg.Aliases, jsonFlag)
|
|
return
|
|
}
|
|
if len(args) == 1 {
|
|
for _, a := range cfg.Aliases {
|
|
if a.Name == args[0] {
|
|
fmt.Println(a.Command)
|
|
return
|
|
}
|
|
}
|
|
fmt.Fprintln(os.Stderr, "alias not found:", args[0])
|
|
os.Exit(1)
|
|
}
|
|
alias := &store.Alias{Name: args[0], Command: args[1], Description: aliasDesc}
|
|
found := false
|
|
for i, a := range cfg.Aliases {
|
|
if a.Name == alias.Name {
|
|
cfg.Aliases[i] = alias
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
cfg.Aliases = append(cfg.Aliases, alias)
|
|
}
|
|
if err := cfg.Save(); err != nil {
|
|
fmt.Fprintln(os.Stderr, "failed to set alias:", err)
|
|
} else {
|
|
PrintAction(w, "Alias set", args[0], false)
|
|
}
|
|
},
|
|
}
|
|
|
|
var aliasDelCmd = &cobra.Command{
|
|
Use: "del <name>", Short: "Delete an alias", Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
found := false
|
|
for i, a := range cfg.Aliases {
|
|
if a.Name == args[0] {
|
|
cfg.Aliases = slices.Delete(cfg.Aliases, i, i+1)
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
fmt.Fprintln(os.Stderr, "alias not found")
|
|
os.Exit(1)
|
|
}
|
|
if err := cfg.Save(); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
PrintAction(cmd.OutOrStdout(), "Alias deleted", args[0], false)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(aliasCmd)
|
|
aliasCmd.AddCommand(aliasDelCmd)
|
|
aliasCmd.Flags().StringVar(&aliasDesc, "desc", "", "description for the alias")
|
|
}
|