Skip to content

Commit

Permalink
- sort goals by (name, env)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaabramov committed Nov 8, 2021
1 parent e349915 commit 65e9cdb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
23 changes: 15 additions & 8 deletions lib/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ func normalizeArgs(args []string) []string {
}
}

func parseEnvCommands(name string, envs map[string]YamlEnvGoal) (commands []Command) {
func parseEnvCommands(name string, envs map[string]YamlEnvGoal) []Command {
var commands []Command
for env, envCommand := range envs {
args := normalizeArgs(envCommand.Args)
commands = append(commands, Command{
Expand All @@ -212,8 +213,7 @@ func parseEnvCommands(name string, envs map[string]YamlEnvGoal) (commands []Comm
Env: env,
})
}
sortCommands(commands)
return
return sortCommands(commands)
}

// ParseCommands from byte input (YAML)
Expand All @@ -238,13 +238,20 @@ func ParseCommands(bytes []byte) (*Commands, error) {
})
}
}
sortCommands(res)

return &Commands{Commands: res}, nil
return &Commands{Commands: sortCommands(res)}, nil
}

func sortCommands(commands []Command) {
sort.Slice(commands, func(i, j int) bool {
return commands[i].Name < commands[j].Name
func sortCommands(commands []Command) (sorted []Command) {
sorted = append(sorted, commands...)
sort.Slice(sorted, func(i, j int) bool {
if sorted[i].Name < sorted[j].Name {
return true
}
if sorted[i].Name > sorted[j].Name {
return false
}
return sorted[i].Env < sorted[j].Env
})
return
}
4 changes: 2 additions & 2 deletions lib/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ workspace:
- stage.tfvars
`)},
want: &Commands{
Commands: []Command{
Commands: sortCommands([]Command{
{
Name: "workspace",
Cmd: "terraform",
Expand All @@ -79,7 +79,7 @@ workspace:
Env: "stage",
Desc: "tf apply stage",
},
},
}),
},
wantErr: false,
},
Expand Down

0 comments on commit 65e9cdb

Please sign in to comment.