diff --git a/.gitignore b/.gitignore index 464c17d..b23bc47 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ bin dist/ .github_token +completions diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 5cd435b..45443e5 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -10,10 +10,8 @@ version: 1 before: hooks: - # You may remove this if you don't use go modules. - go mod tidy - # you may remove this if you don't need go generate - - go generate ./... + - ./scripts/completions.zsh builds: - env: @@ -23,6 +21,10 @@ builds: archives: - format: tar.gz + files: + - README.md + - LICENSE + - completions/* # this name template makes the OS and Arch compatible with the results of `uname`. name_template: >- {{ .ProjectName }}_ @@ -43,6 +45,10 @@ env_files: github_token: .github_token brews: - - repository: + - extra_install: |- + zsh_completion.install "completions/{{ .ProjectName }}.zsh" => "{{ .ProjectName }}" + homepage: "https://github.com/Bhacaz/gostacking" + description: "Git stacking using merge" + repository: owner: Bhacaz name: homebrew-tap diff --git a/README.md b/README.md index bac683c..5af7ab0 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,8 @@ goreleaser release ## Release -1. Update `.version` -2. `zsh release.zsh` +1. Update the version in file `.version` +2. `zsh scripts/release.zsh` ## TODOs diff --git a/cmd/root.go b/cmd/root.go index 06a6233..4e13229 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -40,4 +40,5 @@ func init() { // Cobra also supports local flags, which will only run // when this action is called directly. // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + rootCmd.CompletionOptions.HiddenDefaultCmd = true } diff --git a/cmd/switch.go b/cmd/switch.go index e50c3af..c320723 100644 --- a/cmd/switch.go +++ b/cmd/switch.go @@ -21,6 +21,9 @@ var switchCmd = &cobra.Command{ stack.Manager().SwitchByName(args[0]) } }, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return stack.Manager().ListStacksForCompletion(toComplete), cobra.ShellCompDirectiveNoFileComp + }, } func init() { diff --git a/internal/stack/stack_manager.go b/internal/stack/stack_manager.go index f1f080f..8da1bdd 100644 --- a/internal/stack/stack_manager.go +++ b/internal/stack/stack_manager.go @@ -6,6 +6,7 @@ import ( "github.com/Bhacaz/gostacking/internal/git" "log" "slices" + "strings" ) type StacksManager struct { @@ -95,6 +96,17 @@ func (sm StacksManager) List() { } } +func (sm StacksManager) ListStacksForCompletion(toComplete string) []string { + data := sm.load() + var stacks []string + for _, stack := range data.Stacks { + if toComplete == "" || strings.HasPrefix(stack.Name, toComplete) { + stacks = append(stacks, stack.Name) + } + } + return stacks +} + func (sm StacksManager) SwitchByName(stackName string) { data := sm.load() data.CurrentStack = stackName diff --git a/scripts/completions.zsh b/scripts/completions.zsh new file mode 100755 index 0000000..e734cb0 --- /dev/null +++ b/scripts/completions.zsh @@ -0,0 +1,7 @@ +#!/bin/zsh + +set -e +rm -rf completions +mkdir completions + +go run . completion zsh > "completions/gostacking.zsh" diff --git a/release.zsh b/scripts/release.zsh similarity index 100% rename from release.zsh rename to scripts/release.zsh