Skip to content

Commit

Permalink
docs: Add best practice on usage strings
Browse files Browse the repository at this point in the history
Tell developers to have "kubectl " prefix ahead of their plugins' usage/help
messages.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
  • Loading branch information
ahmetb committed Mar 2, 2020
1 parent 4a1f75c commit b5c9c5e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions site/content/docs/developer-guide/develop/best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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
```

0 comments on commit b5c9c5e

Please sign in to comment.