|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/fntlnz/kubectl-trace/factory" |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 9 | + // "k8s.io/kubernetes/pkg/kubectl/util/templates" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + getCommand = "get" |
| 14 | + getShort = `Get the running traces` // Wrap with i18n.T() |
| 15 | + getLong = getShort + ` |
| 16 | + |
| 17 | +...` |
| 18 | + |
| 19 | + getExamples = ` |
| 20 | + # Get all traces in a namespace |
| 21 | + %[1]s trace get -n myns |
| 22 | +
|
| 23 | + # Get only a specific trace |
| 24 | + %[1]s trace get 656ee75a-ee3c-11e8-9e7a-8c164500a77e |
| 25 | +
|
| 26 | + # Get only a specific trace in a specific namespace |
| 27 | + %[1]s trace get 656ee75a-ee3c-11e8-9e7a-8c164500a77e -n myns |
| 28 | +
|
| 29 | + # Get all traces in all namespaces |
| 30 | + %[1]s trace get --all-namespaces` |
| 31 | + |
| 32 | + argumentsErr = fmt.Sprintf("at most one argument for %s command", getCommand) |
| 33 | + missingTargetErr = fmt.Sprintf("specify either a TRACE_ID or a namespace or all namespaces") |
| 34 | +) |
| 35 | + |
| 36 | +// GetOptions ... |
| 37 | +type GetOptions struct { |
| 38 | + genericclioptions.IOStreams |
| 39 | + ResourceBuilderFlags *genericclioptions.ResourceBuilderFlags |
| 40 | + |
| 41 | + namespace string |
| 42 | + explicitNamespace bool |
| 43 | + |
| 44 | + // Local to this command |
| 45 | + allNamespaces bool |
| 46 | + traceArg string |
| 47 | +} |
| 48 | + |
| 49 | +// NewGetOptions provides an instance of GetOptions with default values. |
| 50 | +func NewGetOptions(streams genericclioptions.IOStreams) *GetOptions { |
| 51 | + rbFlags := &genericclioptions.ResourceBuilderFlags{} |
| 52 | + rbFlags.WithAllNamespaces(false) |
| 53 | + |
| 54 | + return &GetOptions{ |
| 55 | + ResourceBuilderFlags: rbFlags, |
| 56 | + IOStreams: streams, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// NewGetCommand provides the get command wrapping GetOptions. |
| 61 | +func NewGetCommand(factory factory.Factory, streams genericclioptions.IOStreams) *cobra.Command { |
| 62 | + o := NewGetOptions(streams) |
| 63 | + |
| 64 | + cmd := &cobra.Command{ |
| 65 | + Use: fmt.Sprintf("%s [TRACE_ID]", getCommand), |
| 66 | + Short: getShort, |
| 67 | + Long: getLong, // wrap with templates.LongDesc() |
| 68 | + Example: fmt.Sprintf(getExamples, "kubectl"), // wrap with templates.Examples() |
| 69 | + SilenceUsage: true, |
| 70 | + PreRunE: func(c *cobra.Command, args []string) error { |
| 71 | + return o.Validate(c, args) |
| 72 | + }, |
| 73 | + RunE: func(c *cobra.Command, args []string) error { |
| 74 | + if err := o.Complete(factory, c, args); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + return nil |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + o.ResourceBuilderFlags.AddFlags(cmd.Flags()) |
| 82 | + |
| 83 | + return cmd |
| 84 | +} |
| 85 | + |
| 86 | +// Validate validates the arguments and flags populating GetOptions accordingly. |
| 87 | +func (o *GetOptions) Validate(cmd *cobra.Command, args []string) error { |
| 88 | + switch len(args) { |
| 89 | + case 0: |
| 90 | + break |
| 91 | + case 1: |
| 92 | + o.traceArg = args[0] |
| 93 | + break |
| 94 | + default: |
| 95 | + return fmt.Errorf(argumentsErr) |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// Complete completes the setup of the command. |
| 102 | +func (o *GetOptions) Complete(factory factory.Factory, cmd *cobra.Command, args []string) error { |
| 103 | + o.namespace, o.explicitNamespace, _ = factory.ToRawKubeConfigLoader().Namespace() |
| 104 | + |
| 105 | + if cmd.Flag("all-namespaces").Changed { |
| 106 | + o.allNamespaces = *o.ResourceBuilderFlags.AllNamespaces |
| 107 | + o.explicitNamespace = false |
| 108 | + o.namespace = "" |
| 109 | + } |
| 110 | + if o.traceArg == "" && !o.allNamespaces && !o.explicitNamespace { |
| 111 | + return fmt.Errorf(missingTargetErr) |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
0 commit comments