Skip to content

Commit

Permalink
Sort target list (#212)
Browse files Browse the repository at this point in the history
When using the mage:import feature the target list (`mage -l`) will now be sorted.
  • Loading branch information
andrewkroh authored and natefinch committed Dec 29, 2018
1 parent 4d83845 commit 85b33f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
6 changes: 3 additions & 3 deletions mage/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func TestMageImportsList(t *testing.T) {
actual := stdout.String()
expected := `
Targets:
buildSubdir Builds stuff.
nS:deploy deploys stuff.
root
zz:nS:deploy2* deploys stuff.
zz:buildSubdir2 Builds stuff.
nS:deploy deploys stuff.
buildSubdir Builds stuff.
zz:nS:deploy2* deploys stuff.
* default target
`[1:]
Expand Down
23 changes: 18 additions & 5 deletions mage/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"text/tabwriter"
Expand Down Expand Up @@ -96,16 +97,28 @@ Options:
{{with .Description}}fmt.Println(` + "`{{.}}\n`" + `)
{{- end}}
{{- $default := .DefaultFunc}}
w := tabwriter.NewWriter(os.Stdout, 0, 4, 4, ' ', 0)
fmt.Println("Targets:")
targets := map[string]string{
{{- range .Funcs}}
fmt.Fprintln(w, " {{lowerFirst .TargetName}}{{if and (eq .Name $default.Name) (eq .Receiver $default.Receiver)}}*{{end}}\t" + {{printf "%q" .Synopsis}})
"{{lowerFirst .TargetName}}{{if and (eq .Name $default.Name) (eq .Receiver $default.Receiver)}}*{{end}}": {{printf "%q" .Synopsis}},
{{- end}}
{{- range .Imports}}{{$imp := .}}
{{- range .Info.Funcs}}
fmt.Fprintln(w, " {{lowerFirst .TargetName}}{{if and (eq .Name $default.Name) (eq .Receiver $default.Receiver)}}*{{end}}\t" + {{printf "%q" .Synopsis}})
{{end}}
"{{lowerFirst .TargetName}}{{if and (eq .Name $default.Name) (eq .Receiver $default.Receiver)}}*{{end}}": {{printf "%q" .Synopsis}},
{{- end}}
{{- end}}
}
keys := make([]string, 0, len(targets))
for name := range targets {
keys = append(keys, name)
}
sort.Strings(keys)
fmt.Println("Targets:")
w := tabwriter.NewWriter(os.Stdout, 0, 4, 4, ' ', 0)
for _, name := range keys {
fmt.Fprintf(w, " %v\t%v\n", name, targets[name])
}
err := w.Flush()
{{- if .DefaultFunc.Name}}
if err == nil {
Expand Down

0 comments on commit 85b33f7

Please sign in to comment.