Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add best practice on usage strings #528

Merged
merged 2 commits into from
Mar 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions site/content/docs/developer-guide/develop/best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ If you are planning to write a plugin with Go, check out:
- [cli-runtime]: Provides packages to share code with `kubectl` for printing output or [sharing command-line options][cli-opts]
- [sample-cli-plugin]: An example plugin implementation in Go

### Consistency with kubectl
### Consistency with kubectl {#kubectl-options}

Krew does not try to impose any rules in terms of the shape of your plugin.

Expand All @@ -39,7 +39,7 @@ Furthermore, by using the [genericclioptions][cli-opts] package (Go), you can
support the global command-line flags listed in `kubectl options` (e.g.
`--kubeconfig`, `--context` and many others) in your plugins.

### Import authentication plugins (Go)
### Import authentication plugins (Go) {#auth-plugins}

By default, plugins that use [client-go]
cannot authenticate to Kubernetes clusters on many cloud providers. To overcome
Expand All @@ -53,3 +53,34 @@ import _ "k8s.io/client-go/plugin/pkg/client/auth"
[client-go]: https://godoc.org/k8s.io/client-go
[cli-opts]: https://godoc.org/k8s.io/cli-runtime/pkg/genericclioptions
[sample-cli-plugin]: https://github.com/kubernetes/sample-cli-plugin

## Revise usage/help messages with `kubectl` prefix {#help-messages}

Users discover how to use your plugin by calling it without any arguments (which
might trigger a help message), or `-h`/`--help` options.

Therefore, change your usage strings to show `kubectl ` prefix before the plugin
name. For example

```text
Usage:
kubectl popeye [flags]
```

To determine whether an executable is running as a plugin or not, you can look
at argv[0], which would have the `kubectl-` prefix. To determine whether your
program is running as a kubectl plugin or not:

- **Go:**

```go
if strings.HasPrefix(filepath.Base(os.Args[0]), "kubectl-") { }
```

- **Bash:**

```bash
if [[ "$(basename "$0")" == kubectl-* ]]; then # invoked as plugin
# ...
fi
```