Skip to content

Commit

Permalink
chore(feat/log): using cobra utils for args
Browse files Browse the repository at this point in the history
Adding this just to illustrate another way of validating the number of
arguments.

Anyway we should choose one way to do it and spread it across all of the
commands with arguments.
  • Loading branch information
leodido authored and fntlnz committed Dec 29, 2018
1 parent 8a59c03 commit c9df760
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pkg/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func NewLogCommand(factory factory.Factory, streams genericclioptions.IOStreams)
Short: logShort,
Long: logLong, // Wrap with templates.LongDesc()
Example: fmt.Sprintf(logExamples, "kubectl"), // Wrap with templates.Examples()
Args: cobra.ExactArgs(1),
PreRunE: func(c *cobra.Command, args []string) error {
return o.Validate(c, args)
},
Expand All @@ -76,17 +77,11 @@ func NewLogCommand(factory factory.Factory, streams genericclioptions.IOStreams)
}

func (o *LogOptions) Validate(cmd *cobra.Command, args []string) error {
switch len(args) {
case 1:
if meta.IsObjectName(args[0]) {
o.traceName = &args[0]
} else {
tid := types.UID(args[0])
o.traceID = &tid
}
break
default:
return fmt.Errorf("(TRACE_ID | TRACE_NAME) is a required argument for the log command")
if meta.IsObjectName(args[0]) {
o.traceName = &args[0]
} else {
tid := types.UID(args[0])
o.traceID = &tid
}

return nil
Expand Down

0 comments on commit c9df760

Please sign in to comment.