Files
ax/cmd/alias.go

57 lines
1.4 KiB
Go
Raw Normal View History

2026-03-26 12:48:47 +00:00
package cmd
import (
"axolotl/output"
2026-03-29 18:58:34 +02:00
"axolotl/service"
2026-03-26 12:48:47 +00:00
"fmt"
"os"
"github.com/spf13/cobra"
)
2026-03-29 18:58:34 +02:00
var aliasDesc string
2026-03-26 12:48:47 +00:00
var aliasCmd = &cobra.Command{
Use: "alias [name] [command]", Short: "Manage aliases", Args: cobra.MaximumNArgs(2),
2026-03-26 12:48:47 +00:00
Run: func(cmd *cobra.Command, args []string) {
w := cmd.OutOrStdout()
2026-03-29 18:58:34 +02:00
if len(args) == 0 {
if aliases, err := cfg.ListAliases(); err == nil {
output.PrintAliases(w, aliases, jsonFlag)
2026-03-26 12:48:47 +00:00
}
return
}
if len(args) == 1 {
a, err := cfg.GetAlias(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, "alias not found:", args[0])
os.Exit(1)
2026-03-26 12:48:47 +00:00
}
fmt.Println(a.Command)
2026-03-26 12:48:47 +00:00
return
}
2026-03-29 18:58:34 +02:00
if err := cfg.SetAlias(&service.Alias{Name: args[0], Command: args[1], Description: aliasDesc}); err != nil {
fmt.Fprintln(os.Stderr, "failed to set alias:", err)
} else {
output.PrintAction(w, "Alias set", args[0], false)
2026-03-26 12:48:47 +00:00
}
},
}
var aliasDelCmd = &cobra.Command{
Use: "del <name>", Short: "Delete an alias", Args: cobra.ExactArgs(1),
2026-03-29 18:58:34 +02:00
Run: func(cmd *cobra.Command, args []string) {
if err := cfg.DeleteAlias(args[0]); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
2026-03-29 18:58:34 +02:00
}
output.PrintAction(cmd.OutOrStdout(), "Alias deleted", args[0], false)
2026-03-29 18:58:34 +02:00
},
}
2026-03-26 12:48:47 +00:00
func init() {
rootCmd.AddCommand(aliasCmd)
aliasCmd.AddCommand(aliasDelCmd)
2026-03-29 18:58:34 +02:00
aliasCmd.Flags().StringVar(&aliasDesc, "desc", "", "description for the alias")
2026-03-26 12:48:47 +00:00
}