refactor: remove db package and move database logic to service layer
This commit is contained in:
36
cmd/root.go
36
cmd/root.go
@@ -15,7 +15,7 @@ var rootCmd = &cobra.Command{Use: "ax", Short: "The axolotl issue tracker"}
|
||||
|
||||
func Execute() {
|
||||
var err error
|
||||
cfg, err = service.LoadConfig()
|
||||
cfg, err = service.LoadConfigFile()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "failed to load config:", err)
|
||||
os.Exit(1)
|
||||
@@ -27,18 +27,48 @@ func Execute() {
|
||||
}
|
||||
}
|
||||
|
||||
func expandAlias(alias *service.Alias, args []string, currentUser string) []string {
|
||||
cmd := alias.Command
|
||||
cmd = strings.ReplaceAll(cmd, "$me", currentUser)
|
||||
|
||||
parts := strings.Fields(cmd)
|
||||
var result []string
|
||||
|
||||
for _, part := range parts {
|
||||
if part == "$@" {
|
||||
result = append(result, 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])
|
||||
}
|
||||
|
||||
result = append(result, replaced)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
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())
|
||||
expanded := expandAlias(a, args, cfg.GetUser())
|
||||
rootCmd.SetArgs(transformArgs(expanded))
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
|
||||
Reference in New Issue
Block a user