Add shell completion

This commit is contained in:
Finn 2021-02-02 21:03:02 -08:00
parent 5c29236bc6
commit e892e19122
5 changed files with 96 additions and 6 deletions

3
.gitignore vendored
View file

@ -2,9 +2,10 @@
docs/public docs/public
docs/content/signaldctl docs/content/signaldctl
# man page files generated for the debian package # generated before building debian package
*.1 *.1
debian/manpages debian/manpages
debian/signaldctl.bash-completion
# Binaries for programs and plugins # Binaries for programs and plugins
*.exe *.exe

View file

@ -53,8 +53,8 @@ build:x86:deb:
- apt-get install -y git-buildpackage dh-golang golang-any golang-github-spf13-cobra-dev golang-github-spf13-viper-dev - apt-get install -y git-buildpackage dh-golang golang-any golang-github-spf13-cobra-dev golang-github-spf13-viper-dev
- apt-get install -y ./*.deb && rm -vf *.deb - apt-get install -y ./*.deb && rm -vf *.deb
script: script:
- echo "generating man pages" - go run ./cmd/signaldctl doc -o man
- SIGNALDCTL_PUBLIC_DOC_MODE=on go run ./cmd/signaldctl doc -o man - go run ./cmd/signaldctl completion bash > debian/package.bash-completion
- ls *.1 > debian/manpages - ls *.1 > debian/manpages
- gbp dch --ignore-branch --debian-tag="%(version)s" --git-author --new-version="$(git describe --abbrev=0)+git$(date +%Y-%m-%d)r$(git rev-parse --short=8 HEAD).$(git rev-list $(git describe --abbrev=0)..HEAD --count)" - gbp dch --ignore-branch --debian-tag="%(version)s" --git-author --new-version="$(git describe --abbrev=0)+git$(date +%Y-%m-%d)r$(git rev-parse --short=8 HEAD).$(git rev-list $(git describe --abbrev=0)..HEAD --count)"
- dpkg-buildpackage -us -uc -b - dpkg-buildpackage -us -uc -b
@ -68,6 +68,8 @@ build:x86:deb:
job: build job: build
ref: master ref: master
artifacts: true artifacts: true
variables:
SIGNALDCTL_PUBLIC_DOC_MODE: on
artifacts: artifacts:
paths: paths:
- "*.deb" - "*.deb"

View file

@ -0,0 +1,87 @@
// Copyright © 2021 Finn Herzfeld <finn@janky.solutions>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"os"
"github.com/spf13/cobra"
"gitlab.com/signald/signald-go/cmd/signaldctl/common"
)
var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Long: `To load completions:
Bash:
$ source <(signaldctl completion bash)
# To load completions for each session, execute once:
Linux:
$ signaldctl completion bash > /etc/bash_completion.d/signaldctl
MacOS:
$ signaldctl completion bash > /usr/local/etc/bash_completion.d/signaldctl
Zsh:
# If shell completion is not already enabled in your environment you will need
# to enable it. You can execute the following once:
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
# To load completions for each session, execute once:
$ signaldctl completion zsh > "${fpath[1]}/_signaldctl"
# You will need to start a new shell for this setup to take effect.
Fish:
$ signaldctl completion fish | source
# To load completions for each session, execute once:
$ signaldctl completion fish > ~/.config/fish/completions/signaldctl.fish
Powershell:
PS> signaldctl completion powershell | Out-String | Invoke-Expression
# To load completions for every new session, run:
PS> signaldctl completion powershell > signaldctl.ps1
# and source this file from your powershell profile.
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.ExactValidArgs(1),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
common.Must(cmd.Root().GenBashCompletion(os.Stdout))
case "zsh":
common.Must(cmd.Root().GenZshCompletion(os.Stdout))
case "fish":
common.Must(cmd.Root().GenFishCompletion(os.Stdout, true))
case "powershell":
common.Must(cmd.Root().GenPowerShellCompletion(os.Stdout))
}
},
}
func init() {
RootCmd.AddCommand(completionCmd)
}

View file

@ -27,7 +27,7 @@ import (
"gitlab.com/signald/signald-go/cmd/signaldctl/common" "gitlab.com/signald/signald-go/cmd/signaldctl/common"
) )
var DocCmd = &cobra.Command{ var docCmd = &cobra.Command{
Use: "doc", Use: "doc",
Aliases: []string{"docs"}, Aliases: []string{"docs"},
Hidden: true, Hidden: true,
@ -53,7 +53,7 @@ var DocCmd = &cobra.Command{
} }
func init() { func init() {
RootCmd.AddCommand(DocCmd) RootCmd.AddCommand(docCmd)
} }
const fmTemplate = `--- const fmTemplate = `---

2
debian/rules vendored
View file

@ -11,7 +11,7 @@ override_dh_auto_install:
dh_auto_install -- --no-source dh_auto_install -- --no-source
%: %:
dh $@ --buildsystem=golang --with=golang dh $@ --buildsystem=golang --with=golang --with bash-completion
override_dh_auto_build: override_dh_auto_build:
dh_auto_build -- -ldflags '$(GO_LDFLAGS)' dh_auto_build -- -ldflags '$(GO_LDFLAGS)'