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"
|
2026-03-27 02:11:46 +01:00
|
|
|
"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
|
2026-03-27 02:11:46 +01:00
|
|
|
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
|
2026-03-29 21:24:09 +02:00
|
|
|
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()
|
2026-03-26 12:48:47 +00:00
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 23:16:44 +02:00
|
|
|
func init() {
|
|
|
|
|
rootCmd.PersistentFlags().BoolVar(&jsonFlag, "json", false, "")
|
|
|
|
|
}
|
2026-03-29 21:24:09 +02:00
|
|
|
|
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{
|
2026-03-29 19:09:06 +02:00
|
|
|
Use: a.Name,
|
|
|
|
|
Short: a.Description,
|
|
|
|
|
GroupID: "aliases",
|
|
|
|
|
DisableFlagParsing: true,
|
2026-03-29 23:16:44 +02:00
|
|
|
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-31 15:55:47 +02:00
|
|
|
rootCmd.SetArgs(expanded)
|
2026-03-29 18:58:34 +02:00
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|