Skip to content

Commit

Permalink
fix: Print usage string & examples according to execution context
Browse files Browse the repository at this point in the history
If the program is executed as a kubectl plugin, adjust command usage &
example strings to show the "kubectl" prefix before the plugin's name.

Signed-off-by: Justin Toh <tohjustin@hotmail.com>
  • Loading branch information
tohjustin committed Oct 2, 2021
1 parent 54402b4 commit 0ab822f
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions pkg/cmd/lineage/lineage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package lineage
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"

Expand All @@ -26,22 +28,32 @@ import (
)

var (
cmdLong = templates.LongDesc(`
Display all dependents of a Kubernetes object.
TYPE is a Kubernetes resource. Shortcuts and groups will be resolved.
NAME is the name of a particular Kubernetes resource.`)
cmdName = "kubectl-lineage"
cmdUse = "COMMAND (TYPE[.VERSION][.GROUP] [NAME] | TYPE[.VERSION][.GROUP]/NAME) [flags]"
cmdExample = templates.Examples(`
# List all dependents of the deployment named "bar" in the current namespace
kubectl lineage deployments bar
COMMAND deployments bar
# List all dependents of the cronjob named "bar" in namespace "foo"
kubectl lineage cronjobs.batch/bar -n foo
COMMAND cronjobs.batch/bar -n foo
# List all dependents of the node named "k3d-dev-server" & the corresponding relationship type(s)
kubectl lineage node/k3d-dev-server -o wide`)
COMMAND node/k3d-dev-server -o wide`)
cmdShort = "Display all dependents of a Kubernetes object"
cmdLong = templates.LongDesc(`
Display all dependents of a Kubernetes object.
TYPE is a Kubernetes resource. Shortcuts and groups will be resolved.
NAME is the name of a particular Kubernetes resource.`)
)

//nolint:gochecknoinits
func init() {
if strings.HasPrefix(filepath.Base(os.Args[0]), "kubectl-") {
cmdName = "kubectl lineage"
}
}

// CmdOptions contains all the options for running the lineage command.
type CmdOptions struct {
// RequestObject represents the requested object.
Expand Down Expand Up @@ -117,10 +129,10 @@ func New(streams genericclioptions.IOStreams) *cobra.Command {
}

cmd := &cobra.Command{
Use: "lineage (TYPE[.VERSION][.GROUP] [NAME] | TYPE[.VERSION][.GROUP]/NAME) [flags]",
Short: "Display all dependents of a Kubernetes object",
Use: strings.ReplaceAll(cmdUse, "COMMAND", cmdName),
Example: strings.ReplaceAll(cmdExample, "COMMAND", cmdName),
Short: cmdShort,
Long: cmdLong,
Example: cmdExample,
DisableFlagsInUseLine: true,
DisableSuggestions: true,
SilenceUsage: true,
Expand Down

0 comments on commit 0ab822f

Please sign in to comment.