Skip to content

Commit

Permalink
fix(cdsctl): bad sort of arg when mixed with ctx arg, remove weight (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored May 28, 2020
1 parent 5ce1251 commit 96a798c
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 50 deletions.
14 changes: 7 additions & 7 deletions cli/cdsctl/application_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ var applicationVariableCreateCmd = cli.Command{
{Name: _ApplicationName},
},
Args: []cli.Arg{
{Name: "variable-name", Weight: 1},
{Name: "variable-type", Weight: 2},
{Name: "variable-value", Weight: 3},
{Name: "variable-name"},
{Name: "variable-type"},
{Name: "variable-value"},
},
}

Expand Down Expand Up @@ -102,10 +102,10 @@ var applicationVariableUpdateCmd = cli.Command{
{Name: _ApplicationName},
},
Args: []cli.Arg{
{Name: "variable-oldname", Weight: 1},
{Name: "variable-name", Weight: 2},
{Name: "variable-type", Weight: 3},
{Name: "variable-value", Weight: 4},
{Name: "variable-oldname"},
{Name: "variable-name"},
{Name: "variable-type"},
{Name: "variable-value"},
},
}

Expand Down
2 changes: 0 additions & 2 deletions cli/cdsctl/workflow_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ var workflowLogListCmd = cli.Command{
match, _ := regexp.MatchString(`[0-9]?`, s)
return match
},
Weight: 1,
},
},
}
Expand Down Expand Up @@ -196,7 +195,6 @@ var workflowLogDownloadCmd = cli.Command{
match, _ := regexp.MatchString(`[0-9]?`, s)
return match
},
Weight: 1,
},
},
Flags: []cli.Flag{
Expand Down
2 changes: 0 additions & 2 deletions cli/cdsctl/workflow_run_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ var workflowHistoryCmd = cli.Command{
match, _ := regexp.MatchString(`[0-9]?`, s)
return match
},
Weight: 1,
},
{
Name: "limit",
IsValid: func(s string) bool {
match, _ := regexp.MatchString(`[0-9]?`, s)
return match
},
Weight: 2,
},
},
}
Expand Down
6 changes: 2 additions & 4 deletions cli/cdsctl/workflow_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ cdsctl workflow stop MYPROJECT myworkflow 5 compile # To stop a workflow node ru
},
OptionalArgs: []cli.Arg{
{
Name: "run-number",
Weight: 1,
Name: "run-number",
},
{
Name: "node-name",
Weight: 2,
Name: "node-name",
},
},
}
Expand Down
4 changes: 0 additions & 4 deletions cli/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ func newCommand(c Command, run interface{}, subCommands SubCommands, mods ...Com
cmd.SetOutput(os.Stdout)
cmd.Use = c.Name

sort.Sort(orderArgs(c.Args...))
sort.Sort(orderArgs(c.OptionalArgs...))

if len(c.Ctx) > 0 {
cmd.Use = cmd.Use + " ["
}
Expand Down Expand Up @@ -138,7 +135,6 @@ func newCommand(c Command, run interface{}, subCommands SubCommands, mods ...Com

definedArgs := append(c.Ctx, c.Args...)
definedArgs = append(definedArgs, c.OptionalArgs...)
sort.Sort(orderArgs(definedArgs...))
definedArgs = append(definedArgs, c.VariadicArgs)

cmd.Short = c.Short
Expand Down
31 changes: 0 additions & 31 deletions cli/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -83,39 +82,9 @@ func (v *Values) GetStringArray(s string) []string {
type Arg struct {
Name string
IsValid func(string) bool
Weight int
AllowEmpty bool
}

func orderArgs(a ...Arg) args {
for i := range a {
if a[i].Weight == 0 {
a[i].Weight = i
}
}
res := args(a)
sort.Sort(res)
return res
}

type args []Arg

// Len is the number of elements in the collection.
func (s args) Len() int {
return len(s)
}

// Less reports whether the element with
// index i should sort before the element with index j.
func (s args) Less(i, j int) bool {
return s[i].Weight < s[j].Weight
}

// Swap swaps the elements with indexes i and j.
func (s args) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

// Command represents the way to instantiate a cobra.Command
type Command struct {
Name string
Expand Down

0 comments on commit 96a798c

Please sign in to comment.