Sort `docker stack ls` by name

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2017-02-16 14:30:39 +01:00
parent 1de08ac417
commit ddae8d967b
1 changed files with 10 additions and 2 deletions

View File

@ -3,17 +3,17 @@ package stack
import ( import (
"fmt" "fmt"
"io" "io"
"sort"
"strconv" "strconv"
"text/tabwriter" "text/tabwriter"
"golang.org/x/net/context"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/cli" "github.com/docker/docker/cli"
"github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command"
"github.com/docker/docker/cli/compose/convert" "github.com/docker/docker/cli/compose/convert"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/net/context"
) )
const ( const (
@ -53,12 +53,20 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error {
return nil return nil
} }
type byName []*stack
func (n byName) Len() int { return len(n) }
func (n byName) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
func (n byName) Less(i, j int) bool { return n[i].Name < n[j].Name }
func printTable(out io.Writer, stacks []*stack) { func printTable(out io.Writer, stacks []*stack) {
writer := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0) writer := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0)
// Ignore flushing errors // Ignore flushing errors
defer writer.Flush() defer writer.Flush()
sort.Sort(byName(stacks))
fmt.Fprintf(writer, listItemFmt, "NAME", "SERVICES") fmt.Fprintf(writer, listItemFmt, "NAME", "SERVICES")
for _, stack := range stacks { for _, stack := range stacks {
fmt.Fprintf( fmt.Fprintf(