Git

Freckles embeds Git as subcommand to manage the repository.

By passing -C <dir> it acts as if called from within the repository at ~/.local/share/freckles. And with DisableFlagParsing: true every argument is seen as positional and passed along.

var gitCmd = &cobra.Command{
	Use:                "git",
	Short:              "invoke git on freckles directory",
	DisableFlagParsing: true,
	Run: func(cmd *cobra.Command, args []string) {
		c := exec.Command("git", append([]string{"-C", freckles.Dir()}, args...)...)
		c.Stdin = os.Stdin
		c.Stdout = os.Stdout
		c.Stderr = os.Stderr
		c.Run()
	},
}

Completion is provided with ActionCarapaceBin.

	carapace.Gen(gitCmd).PositionalAnyCompletion(
		carapace.ActionCallback(func(c carapace.Context) carapace.Action {
			return bridge.ActionCarapaceBin("git", "-C", freckles.Dir())
		}),
	)

Without arguments, ActionCarapaceBin completes all registered commands.

Here, we limit it to the Git command and further specify it with arguments.


Just as above we change the directory with -C <dir> to the repository.

This works because the Git completer in Carapace implemented generic folder modifications with PreInvoke. Nonetheless, Chdir should generally be used for this.


When fully embedding a command DisableFlagParsing: true is the right approach.

But for sudo-like behavior where the local command has flags as well, there is SetInterspersed. Disabling it stops flag parsing after the first positional argument.