Files
ax/cmd/root.go
Elias Kohout 05261522a0 fix: alias argument expansion with flags and spaces
Fixes an issue where alias arguments containing spaces or flags were being split incorrectly by using strings.Fields on the substituted command string. Instead, the command string is tokenized first and then substitution happens on each token.

Also disables Cobra flag parsing for aliases dynamically so that arguments and flags correctly cascade down to the target command instead of throwing unknown flag errors.
2026-03-29 19:09:06 +02:00

79 lines
1.7 KiB
Go

package cmd
import (
"axolotl/service"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
)
var jsonFlag bool
var cfg service.Config
var rootCmd = &cobra.Command{Use: "ax", Short: "The axolotl issue tracker"}
func Execute() {
var err error
cfg, err = service.LoadConfig()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to load config:", err)
os.Exit(1)
}
registerAliasCommands()
rootCmd.SetArgs(transformArgs(os.Args[1:]))
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func registerAliasCommands() {
rootCmd.AddGroup(&cobra.Group{ID: "aliases", Title: "Aliases:"})
aliases, _ := cfg.ListAliases()
for _, a := range aliases {
a := a
rootCmd.AddCommand(&cobra.Command{
Use: a.Name,
Short: a.Description,
GroupID: "aliases",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
expanded := service.ExpandAlias(a, args, cfg.GetUser())
rootCmd.SetArgs(transformArgs(expanded))
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
},
})
}
}
func transformArgs(args []string) []string {
aliases := map[string]string{
"--status": "_status",
"--prio": "_prio",
"--type": "_type",
"--namespace": "_namespace",
}
result := []string{}
for i := 0; i < len(args); i++ {
if idx := strings.Index(args[i], "="); idx != -1 {
if prop, ok := aliases[args[i][:idx]]; ok {
result = append(result, "--tag", prop+"::"+args[i][idx+1:])
continue
}
}
if prop, ok := aliases[args[i]]; ok && i+1 < len(args) {
result, i = append(result, "--tag", prop+"::"+args[i+1]), i+1
continue
}
result = append(result, args[i])
}
return result
}
func init() {
rootCmd.PersistentFlags().BoolVar(&jsonFlag, "json", false, "")
}