Files
ax/cmd/root.go

126 lines
3.0 KiB
Go
Raw Normal View History

2026-03-26 12:48:47 +00:00
package cmd
import (
2026-03-29 18:58:34 +02:00
"axolotl/service"
"fmt"
2026-03-26 12:48:47 +00:00
"os"
"strings"
2026-03-26 12:48:47 +00:00
"github.com/spf13/cobra"
)
var jsonFlag bool
2026-03-29 18:58:34 +02:00
var cfg service.Config
var rootCmd = &cobra.Command{Use: "ax", Short: "The axolotl issue tracker"}
2026-03-26 12:48:47 +00:00
func Execute() {
2026-03-29 18:58:34 +02:00
var err error
cfg, err = service.LoadConfigFile()
2026-03-29 18:58:34 +02:00
if err != nil {
fmt.Fprintln(os.Stderr, "failed to load config:", err)
os.Exit(1)
}
registerAliasCommands()
rootCmd.SetArgs(transformArgs(os.Args[1:]))
2026-03-26 12:48:47 +00:00
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.PersistentFlags().BoolVar(&jsonFlag, "json", false, "")
}
func addPropertyFlags(cmd *cobra.Command) {
cmd.Flags().String("type", "", "node type")
cmd.Flags().String("status", "", "node status")
cmd.Flags().String("prio", "", "node priority")
cmd.Flags().String("namespace", "", "node namespace")
cmd.Flags().String("assignee", "", "node assignee")
cmd.Flags().String("mention", "", "node mention")
}
2026-03-29 18:58:34 +02:00
func registerAliasCommands() {
rootCmd.AddGroup(&cobra.Group{ID: "aliases", Title: "Aliases:"})
aliases, _ := cfg.ListAliases()
for _, a := range aliases {
rootCmd.AddCommand(&cobra.Command{
Use: a.Name,
Short: a.Description,
GroupID: "aliases",
DisableFlagParsing: true,
Run: func(ccmd *cobra.Command, args []string) {
acmd := a.Command
acmd = strings.ReplaceAll(acmd, "$me", cfg.GetUser())
parts := strings.Fields(acmd)
var expanded []string
for _, part := range parts {
if part == "$@" {
expanded = append(expanded, args...)
continue
}
hasCatchAll := strings.Contains(part, "$@")
replaced := part
if hasCatchAll {
replaced = strings.ReplaceAll(replaced, "$@", strings.Join(args, " "))
}
for i := len(args) - 1; i >= 0; i-- {
placeholder := fmt.Sprintf("$%d", i+1)
replaced = strings.ReplaceAll(replaced, placeholder, args[i])
}
expanded = append(expanded, replaced)
}
2026-03-29 18:58:34 +02:00
rootCmd.SetArgs(transformArgs(expanded))
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
},
})
}
}
func transformArgs(args []string) []string {
tagAliases := map[string]string{
"--status": "_status",
"--prio": "_prio",
"--type": "_type",
}
relAliases := map[string]string{
"--namespace": "in_namespace",
"--assignee": "assignee",
"--mention": "mentions",
}
result := []string{}
for i := 0; i < len(args); i++ {
if idx := strings.Index(args[i], "="); idx != -1 {
flag := args[i][:idx]
val := args[i][idx+1:]
if prop, ok := tagAliases[flag]; ok {
result = append(result, "--tag", prop+"::"+val)
continue
}
if prop, ok := relAliases[flag]; ok {
result = append(result, "--rel", prop+":"+val)
continue
}
}
flag := args[i]
if i+1 < len(args) {
if prop, ok := tagAliases[flag]; ok {
result = append(result, "--tag", prop+"::"+args[i+1])
i++
continue
}
if prop, ok := relAliases[flag]; ok {
result = append(result, "--rel", prop+":"+args[i+1])
i++
continue
}
}
result = append(result, args[i])
}
return result
}