Skip to content

Commit

Permalink
Add completions (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz authored Feb 28, 2024
1 parent 32234e1 commit 0eb5adf
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ bin

dist/
.github_token
completions
14 changes: 10 additions & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}_
Expand All @@ -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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 12 additions & 0 deletions internal/stack/stack_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Bhacaz/gostacking/internal/git"
"log"
"slices"
"strings"
)

type StacksManager struct {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions scripts/completions.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/zsh

set -e
rm -rf completions
mkdir completions

go run . completion zsh > "completions/gostacking.zsh"
File renamed without changes.

0 comments on commit 0eb5adf

Please sign in to comment.