Skip to content

Commit

Permalink
Install shell completions (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbialon authored Jan 8, 2024
1 parent 5d9039b commit 10a716e
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ build
dist
key.*
*.asc

completions
12 changes: 12 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
before:
hooks:
- ./scripts/completions.sh

builds:
- env:
- CGO_ENABLED=0
Expand All @@ -13,6 +17,10 @@ archives:
# https://github.com/spacelift-io/setup-spacectl
- format: zip
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
files:
- LICENSE
- README.md
- completions/*

dockers:
- use: buildx
Expand Down Expand Up @@ -99,6 +107,10 @@ brews:
homepage: https://github.com/spacelift-io/spacectl
description: "Spacelift client and CLI"
license: "MIT"
extra_install: |
bash_completion.install "completions/{{ .ProjectName }}.bash" => "{{ .ProjectName }}"
zsh_completion.install "completions/{{ .ProjectName }}.zsh" => "_{{ .ProjectName }}"
fish_completion.install "completions/{{ .ProjectName }}.fish"
winget:
- name: "{{ .ProjectName }}"
Expand Down
32 changes: 32 additions & 0 deletions internal/cmd/completion/bash_autocomplete
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#! /bin/bash

# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
_cli_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}

_cli_bash_autocomplete() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
_cli_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
requestComp="${words[*]} ${cur} --generate-bash-completion"
else
requestComp="${words[*]} --generate-bash-completion"
fi
opts=$(eval "${requestComp}" 2>/dev/null)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
}

complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete spacectl
56 changes: 56 additions & 0 deletions internal/cmd/completion/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package completion

import (
"bytes"
_ "embed"
"io"
"os"
"strings"

"github.com/urfave/cli/v2"
)

var (
//go:embed bash_autocomplete
bashAutocomplete []byte

//go:embed zsh_autocomplete
zshAutocomplete []byte
)

func Command() *cli.Command {
return &cli.Command{
Name: "completion",
Usage: "Print out shell completion script",
Subcommands: []*cli.Command{
{
Name: "bash",
Usage: "Print out bash shell completion script",
Action: func(cliCtx *cli.Context) error {
_, err := io.Copy(os.Stdout, bytes.NewReader(bashAutocomplete))
return err
},
},
{
Name: "zsh",
Usage: "Print out zsh shell completion script",
Action: func(cliCtx *cli.Context) error {
_, err := io.Copy(os.Stdout, bytes.NewReader(zshAutocomplete))
return err
},
},
{
Name: "fish",
Usage: "Print out fish shell completion script",
Action: func(cliCtx *cli.Context) error {
s, err := cliCtx.App.ToFishCompletion()
if err != nil {
return err
}
_, err = io.Copy(os.Stdout, strings.NewReader(s))
return err
},
},
},
}
}
20 changes: 20 additions & 0 deletions internal/cmd/completion/zsh_autocomplete
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#compdef spacectl

_cli_zsh_autocomplete() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
fi

if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
}

compdef _cli_zsh_autocomplete spacectl
11 changes: 7 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/urfave/cli/v2"

"github.com/spacelift-io/spacectl/internal/cmd/completion"
"github.com/spacelift-io/spacectl/internal/cmd/module"
"github.com/spacelift-io/spacectl/internal/cmd/profile"
"github.com/spacelift-io/spacectl/internal/cmd/provider"
Expand All @@ -26,10 +27,11 @@ func main() {
log.Fatalf("Could not parse compilation date: %v", err)
}
app := &cli.App{
Name: "spacectl",
Version: version,
Compiled: compileTime,
Usage: "Programmatic access to Spacelift GraphQL API.",
Name: "spacectl",
Version: version,
Compiled: compileTime,
Usage: "Programmatic access to Spacelift GraphQL API.",
EnableBashCompletion: true,
Commands: []*cli.Command{
module.Command(),
profile.Command(),
Expand All @@ -39,6 +41,7 @@ func main() {
whoami.Command(),
versioncmd.Command(version),
workerpools.Command(),
completion.Command(),
},
}

Expand Down
10 changes: 10 additions & 0 deletions scripts/completions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

set -e

rm -rf completions
mkdir completions

for sh in bash zsh fish; do
go run main.go completion "$sh" >"completions/spacectl.$sh"
done

0 comments on commit 10a716e

Please sign in to comment.