ref(cmd): refactor out globals and init()

pull/921/head
Adam Reese 9 years ago
parent d32c20fd5c
commit 8cb39ce5cc

@ -18,6 +18,8 @@ package main
import ( import (
"fmt" "fmt"
"io"
"os"
"strings" "strings"
"github.com/gosuri/uitable" "github.com/gosuri/uitable"
@ -52,51 +54,59 @@ server's default, which may be much higher than 256. Pairing the '--max'
flag with the '--offset' flag allows you to page through results. flag with the '--offset' flag allows you to page through results.
` `
var listCommand = &cobra.Command{ type lister struct {
Use: "list [flags] [FILTER]", long bool
Short: "list releases", max int
Long: listHelp, offset string
RunE: listCmd, byDate bool
Aliases: []string{"ls"}, sortDesc bool
PersistentPreRunE: setupConnection, out io.Writer
} }
var ( func newListCmd(out io.Writer) *cobra.Command {
listLong bool list := &lister{
listMax int out: out,
listOffset string }
listByDate bool cmd := &cobra.Command{
listSortDesc bool Use: "list [flags] [FILTER]",
) Short: "list releases",
Long: listHelp,
Aliases: []string{"ls"},
PersistentPreRunE: setupConnection,
RunE: func(cmd *cobra.Command, args []string) error {
return list.run(args)
},
}
f := cmd.Flags()
f.BoolVarP(&list.long, "long", "l", false, "output long listing format")
f.BoolVarP(&list.byDate, "date", "d", false, "sort by release date")
f.BoolVarP(&list.sortDesc, "reverse", "r", false, "reverse the sort order")
f.IntVarP(&list.max, "max", "m", 256, "maximum number of releases to fetch")
f.StringVarP(&list.offset, "offset", "o", "", "the next release name in the list, used to offset from start value")
return cmd
}
func init() { func init() {
f := listCommand.Flags() RootCommand.AddCommand(newListCmd(os.Stdout))
f.BoolVarP(&listLong, "long", "l", false, "output long listing format")
f.BoolVarP(&listByDate, "date", "d", false, "sort by release date")
f.BoolVarP(&listSortDesc, "reverse", "r", false, "reverse the sort order")
f.IntVarP(&listMax, "max", "m", 256, "maximum number of releases to fetch")
f.StringVarP(&listOffset, "offset", "o", "", "the next release name in the list, used to offset from start value")
RootCommand.AddCommand(listCommand)
} }
func listCmd(cmd *cobra.Command, args []string) error { func (l *lister) run(args []string) error {
var filter string var filter string
if len(args) > 0 { if len(args) > 0 {
filter = strings.Join(args, " ") filter = strings.Join(args, " ")
} }
sortBy := services.ListSort_NAME sortBy := services.ListSort_NAME
if listByDate { if l.byDate {
sortBy = services.ListSort_LAST_RELEASED sortBy = services.ListSort_LAST_RELEASED
} }
sortOrder := services.ListSort_ASC sortOrder := services.ListSort_ASC
if listSortDesc { if l.sortDesc {
sortOrder = services.ListSort_DESC sortOrder = services.ListSort_DESC
} }
res, err := helm.ListReleases(listMax, listOffset, sortBy, sortOrder, filter) res, err := helm.ListReleases(l.max, l.offset, sortBy, sortOrder, filter)
if err != nil { if err != nil {
return prettyError(err) return prettyError(err)
} }
@ -106,21 +116,23 @@ func listCmd(cmd *cobra.Command, args []string) error {
} }
if res.Next != "" { if res.Next != "" {
fmt.Printf("\tnext: %s", res.Next) fmt.Fprintf(l.out, "\tnext: %s", res.Next)
} }
rels := res.Releases rels := res.Releases
if listLong {
return formatList(rels) if l.long {
fmt.Fprintln(l.out, formatList(rels))
return nil
} }
for _, r := range rels { for _, r := range rels {
fmt.Println(r.Name) fmt.Fprintln(l.out, r.Name)
} }
return nil return nil
} }
func formatList(rels []*release.Release) error { func formatList(rels []*release.Release) string {
table := uitable.New() table := uitable.New()
table.MaxColWidth = 30 table.MaxColWidth = 30
table.AddRow("NAME", "UPDATED", "STATUS", "CHART") table.AddRow("NAME", "UPDATED", "STATUS", "CHART")
@ -130,7 +142,5 @@ func formatList(rels []*release.Release) error {
s := r.Info.Status.Code.String() s := r.Info.Status.Code.String()
table.AddRow(r.Name, t, s, c) table.AddRow(r.Name, t, s, c)
} }
fmt.Println(table) return table.String()
return nil
} }

Loading…
Cancel
Save