|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + |
4 | 7 | "github.com/carapace-sh/carapace" |
5 | 8 | "github.com/carapace-sh/carapace-bin/pkg/actions/tools/jj" |
| 9 | + "github.com/carapace-sh/carapace-bridge/pkg/actions/bridge" |
| 10 | + shlex "github.com/carapace-sh/carapace-shlex" |
6 | 11 | "github.com/carapace-sh/carapace/pkg/style" |
7 | 12 | "github.com/carapace-sh/carapace/pkg/traverse" |
| 13 | + "github.com/carapace-sh/carapace/third_party/golang.org/x/sys/execabs" |
8 | 14 | "github.com/spf13/cobra" |
9 | 15 | "github.com/spf13/pflag" |
10 | 16 | ) |
@@ -48,4 +54,40 @@ func init() { |
48 | 54 | carapace.Gen(rootCmd).PreInvoke(func(cmd *cobra.Command, flag *pflag.Flag, action carapace.Action) carapace.Action { |
49 | 55 | return action.ChdirF(traverse.Flag(rootCmd.Flag("repository"))) |
50 | 56 | }) |
| 57 | + |
| 58 | + carapace.Gen(rootCmd).PreRun(func(cmd *cobra.Command, args []string) { |
| 59 | + output, err := execabs.Command("jj", "config", "get", "aliases").Output() |
| 60 | + if err != nil { |
| 61 | + carapace.LOG.Println(err.Error()) |
| 62 | + return |
| 63 | + } |
| 64 | + s := string(output) |
| 65 | + s = strings.TrimLeft(s, "{ ") |
| 66 | + s = strings.TrimRight(s, " }\n") |
| 67 | + for _, alias := range strings.Split(s, ", ") { |
| 68 | + if name, value, ok := strings.Cut(alias, " = "); ok { |
| 69 | + var args []string |
| 70 | + if err := json.Unmarshal([]byte(value), &args); err != nil { |
| 71 | + carapace.LOG.Println(err.Error()) |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + aliasCmd := &cobra.Command{ |
| 76 | + Use: name, |
| 77 | + Short: shlex.Join(args), |
| 78 | + GroupID: "alias", |
| 79 | + DisableFlagParsing: true, |
| 80 | + Run: func(cmd *cobra.Command, args []string) {}, |
| 81 | + } |
| 82 | + cmd.Root().AddCommand(aliasCmd) |
| 83 | + |
| 84 | + carapace.Gen(aliasCmd).PositionalAnyCompletion( |
| 85 | + carapace.ActionCallback(func(c carapace.Context) carapace.Action { |
| 86 | + c.Args = append(args, c.Args...) |
| 87 | + return bridge.ActionCarapaceBin("jj").Invoke(c).ToA() |
| 88 | + }), |
| 89 | + ) |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
51 | 93 | } |
0 commit comments