Skip to content

Commit 70238c2

Browse files
author
Martin Simango
committed
Initial commit with working code and release flow
0 parents  commit 70238c2

30 files changed

Lines changed: 1481 additions & 0 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
releases/
2+
dist/
3+
.token
4+
.DS_STORE

.goreleaser.yaml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
before:
2+
hooks:
3+
- go mod tidy
4+
- go generate ./...
5+
builds:
6+
- id: kubectl-plugin_completion
7+
binary: kubectl-plugin_completion
8+
env:
9+
- CGO_ENABLED=0
10+
goos:
11+
- linux
12+
- windows
13+
- darwin
14+
goarch:
15+
- amd64
16+
- arm64
17+
ignore:
18+
- goos: windows
19+
goarch: arm64
20+
archives:
21+
- builds:
22+
- kubectl-plugin_completion
23+
name_template: "{{ .ProjectName }}_{{ .Tag }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
24+
wrap_in_directory: false
25+
format: tar.gz
26+
files:
27+
- LICENSE
28+
format_overrides:
29+
- goos: windows
30+
format: zip
31+
id: kubectl-plugin_completion
32+
checksum:
33+
name_template: 'checksums.txt'
34+
snapshot:
35+
name_template: "{{ .Version }}"
36+
changelog:
37+
sort: asc
38+
filters:
39+
exclude:
40+
- '^docs:'
41+
- '^test:'
42+
krews:
43+
-
44+
name: plugin_completion
45+
ids:
46+
- kubectl-plugin_completion
47+
homepage: "https://github.com/MartinSimango/kubectl-plugin_completion"
48+
description: "This plugin allows for the creation of completion scripts"
49+
short_description: "Allow shell completion for kubectl plugins"
50+
51+
url_template: "https://github.com/MartinSimango/kubectl-plugin_completion/releases/download/{{ .Tag }}/{{ .ArtifactName }}"
52+
skip_upload: true
53+
54+
release:
55+
extra_files:
56+
- glob: ./dist/plugin_completion.yaml

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Martin Simango
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ifeq (,$(wildcard /.token))
2+
include .token
3+
endif
4+
5+
install:
6+
go build && go install
7+
8+
release:
9+
goreleaser release --rm-dist
10+
11+
release-local:
12+
goreleaser release --snapshot --rm-dist

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# kubectl-plugin_completion
2+
3+
Auto completion saves a lot of time when typing in commands. The kubectl tool has a way to allow for completion for it's sub-commands,
4+
however it doesn't not accomodate for when these sub-commands are plugin commands. This repo contains the code for a kubectl plugin that generates scripts for extending the kubectl tool in order to accomodate for completions for kubectl plugin sub-commands.
5+
6+
## Sections
7+
* [Install plugin](https://github.com/MartinSimango/kubectl-plugin-autocompletion#install-plugin)
8+
* [How it works](https://github.com/MartinSimango/kubectl-plugin-autocompletion#how-it-works)
9+
* [How to use](https://github.com/MartinSimango/kubectl-plugin-autocompletion#how-to-use)
10+
* [Example](https://github.com/MartinSimango/kubectl-plugin-autocompletion#example)
11+
12+
13+
## Install plugin
14+
15+
Plugin can be installed in 3 ways:
16+
17+
1. Via krew
18+
```sh
19+
$ kubectl krew install --manifest-url https://github.com/MartinSimango/kubectl-plugin_completion/releases/download/v0.1.0/plugin_completion.yaml
20+
```
21+
2. Zip file containing binary can be download on the [release page](https://github.com/MartinSimango/kubectl-plugin-autocompletion/releases)
22+
23+
3. Manually by cloning the repo and running make install - (This requires you having go installed)
24+
25+
26+
## How it works
27+
28+
The plugin works by reading config files for specfic shells (so far only bash and zsh). These config files in a folder located at `$HOME/.kube/plugin-completion-config` and are named `$SHELL_NAME.yaml`. So for example the zsh config file will be located at `$HOME/.kube/plugin-completion-config/zsh.yaml`.
29+
30+
The config files contain information about the available kubectl plugins along with some additional information such as plugin's description and the plugin's completion function name. Below is an example of what a config file for a zsh terminal will look like:
31+
32+
```yaml
33+
shell: zsh
34+
shellLocation: /bin/zsh
35+
plugins:
36+
- name: krew
37+
completionFunctionName: _krew
38+
description: A kubectl plugin called krew
39+
- name: hello
40+
completionFunctionName: ""
41+
description: A kubectl plugin called hello
42+
- name: plugin_completion
43+
completionFunctionName: _plugin_completion
44+
description: A kubectl plugin called plugin_completion
45+
```
46+
47+
The above file can be generated by running:
48+
``` sh
49+
$ kubectl plugin_completion config generate
50+
```
51+
This will generate config files for all supported shells (zsh and bash). For plugins that were created using [cobra](https://github.com/spf13/cobra) the `completionFunctionName` field will automatically be field in. The description for each plugin will default to: "A kubectl plugin called $plugin_name".
52+
53+
If a plugin was not created by using [cobra](https://github.com/spf13/cobra) it will be up to you to manually edit the config file and provide the name of the plugin's completion function name. This can also be done by running the `kubectl plugin_completion config edit` command as shown below:
54+
55+
```sh
56+
$ kubectl plugin_completion config edit zsh --plugin-name=custom_plugin --completion-function="_completion_function_name"
57+
```
58+
59+
The plugin_completion uses config files to generate completions specific shells in order to allow for completions for kubectl plugins. The generated completion scripts overwrite the behaviour of the completion function for the kubectl tool and extend it to allow for kubectl plugin completions.
60+
61+
## How to use
62+
63+
To start of you will need to generate your shell plugin config files. This can be done by running the below command:
64+
``` sh
65+
$ kubectl plugin_completion config generate
66+
```
67+
68+
Once the config files are generated you can source the plugin completion script by running:
69+
70+
```sh
71+
$ source <(kubectl plugin_completion plugin-completion $SHELL)
72+
```
73+
74+
where `$SHELL` is the shell you want to generate the completion script for.
75+
76+
To make the changes permanent you can add `source <(kubectl plugin_completion plugin-completion zsh)` to your `~/.zshrc` file or `source <(kubectl plugin_completion plugin-completion bash)` to your `~/.bashrc (or ~/.bash_profile)` file.
77+
78+
Please note if you change the config files for a shell you will need to rerun the `source <(kubectl plugin_completion plugin-completion $SHELL)` command as the plugin completion script is generated from using information from the config files.
79+
80+
Once the above is done completions for your plugins should work as displayed in the below Example section.
81+
82+
83+
## Example
84+
<p align="center">
85+
<img src="https://github.com/MartinSimango/kubectl-plugin-autocompletion/blob/main/kub-plugin.gif" style="width:600px;height=1000px"/>
86+
</p>
87+
<!-- ![Alt Text](https://github.com/MartinSimango/kubectl-plugin-autocompletion/blob/main/kub-plugin.gif) -->
88+
89+

cmd/clean.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright © 2022 Martin Simango <shukomango@gmail.com>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"os"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// cleanCmd represents the clean command
14+
var cleanCmd = &cobra.Command{
15+
Use: "clean",
16+
Short: "Clear config files for specified shell (bash or zsh) or for all shells (by specifying all instead of a shell name)",
17+
Long: `Clear config files for specified shell (bash or zsh) or for all shells (by specifying all instead of a shell name).
18+
19+
To clean the config file for the zsh terminal, you can enter the following command:
20+
"kubectl plugin_completion clean zsh"`,
21+
Run: func(cmd *cobra.Command, args []string) {
22+
cmd.Help()
23+
os.Exit(1)
24+
},
25+
}
26+
27+
func init() {
28+
configCmd.AddCommand(cleanCmd)
29+
30+
}

cmd/cleanAll.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright © 2022 Martin Simango <shukomango@gmail.com>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// cleanAllCmd represents the cleanAll command
12+
var cleanAllCmd = &cobra.Command{
13+
Use: "all",
14+
Short: "Clear plugin config file for all shells",
15+
Long: `Clear plugin config file for all shells`,
16+
Run: func(cmd *cobra.Command, args []string) {
17+
NewBashPluginConfigImpl().WriteCleanConfig()
18+
NewZshPluginConfigImpl().WriteCleanConfig()
19+
},
20+
}
21+
22+
func init() {
23+
cleanCmd.AddCommand(cleanAllCmd)
24+
}

cmd/cleanBash.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright © 2022 Martin Simango <shukomango@gmail.com>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// cleanBashCmd represents the cleanBash command
12+
var cleanBashCmd = &cobra.Command{
13+
Use: "bash",
14+
Short: "Clear the bash config plugin file",
15+
Long: `Clear the bash config plugin file`,
16+
Run: func(cmd *cobra.Command, args []string) {
17+
NewBashPluginConfigImpl().WriteCleanConfig()
18+
},
19+
}
20+
21+
func init() {
22+
cleanCmd.AddCommand(cleanBashCmd)
23+
}

cmd/cleanZsh.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright © 2022 Martin Simango <shukomango@gmail.com>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// cleanZshCmd represents the cleanZsh command
12+
var cleanZshCmd = &cobra.Command{
13+
Use: "zsh",
14+
Short: "Clear the zsh config plugin file",
15+
Long: `Clear the zsh config plugin file`,
16+
Run: func(cmd *cobra.Command, args []string) {
17+
NewZshPluginConfigImpl().WriteCleanConfig()
18+
},
19+
}
20+
21+
func init() {
22+
cleanCmd.AddCommand(cleanZshCmd)
23+
24+
}

cmd/completion.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var CompletionCmd = &cobra.Command{
10+
Use: "completion",
11+
Short: "Generate completion script",
12+
Long: "To load completions",
13+
DisableFlagsInUseLine: true,
14+
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
15+
Args: cobra.ExactValidArgs(1),
16+
Run: func(cmd *cobra.Command, args []string) {
17+
switch args[0] {
18+
case "bash":
19+
cmd.Root().GenBashCompletion(os.Stdout)
20+
case "zsh":
21+
cmd.Root().GenZshCompletion(os.Stdout)
22+
case "fish":
23+
cmd.Root().GenFishCompletion(os.Stdout, true)
24+
case "powershell":
25+
cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
26+
}
27+
},
28+
}
29+
30+
func init() {
31+
rootCmd.AddCommand(CompletionCmd)
32+
}

0 commit comments

Comments
 (0)