Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit badba35

Browse files
author
Dj Walker-Morgan
committed
Added sorting options -n,-d,-i
1 parent 3294815 commit badba35

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

cmd/list.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,40 @@ import (
1818
"fmt"
1919
"log"
2020
"regexp"
21+
"sort"
2122
"strings"
2223

24+
composeAPI "github.com/compose/gocomposeapi"
25+
2326
"github.com/spf13/cobra"
2427
)
2528

2629
var listdbtype string
2730
var listfilter string
31+
var bytype bool
32+
var byname bool
33+
var byid bool
34+
35+
//ByDBType sorts by type
36+
type ByDBType []composeAPI.Deployment
37+
38+
func (a ByDBType) Len() int { return len(a) }
39+
func (a ByDBType) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
40+
func (a ByDBType) Less(i, j int) bool { return a[i].Type < a[j].Type }
41+
42+
//ByName sorts by name
43+
type ByName []composeAPI.Deployment
44+
45+
func (a ByName) Len() int { return len(a) }
46+
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
47+
func (a ByName) Less(i, j int) bool { return strings.ToLower(a[i].Name) < strings.ToLower(a[j].Name) }
48+
49+
//ByID sorts by ID
50+
type ByID []composeAPI.Deployment
51+
52+
func (a ByID) Len() int { return len(a) }
53+
func (a ByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
54+
func (a ByID) Less(i, j int) bool { return a[i].ID < a[j].ID }
2855

2956
// listCmd represents the new list deployments command
3057
var listCmd = &cobra.Command{
@@ -50,6 +77,14 @@ var listCmd = &cobra.Command{
5077
fmt.Printf("%-24s %-14s %-40s\n", "ID", "Type", "Name")
5178
fmt.Printf("%-24s %-14s %-40s\n", strings.Repeat("-", 24), strings.Repeat("-", 14), strings.Repeat("-", 40))
5279

80+
if bytype {
81+
sort.Sort(ByDBType(*deployments))
82+
} else if byid {
83+
sort.Sort(ByID(*deployments))
84+
} else if byname {
85+
sort.Sort(ByName(*deployments))
86+
}
87+
5388
for _, v := range *deployments {
5489
if (listdbtype == "" || listdbtype == v.Type) && matcher.MatchString(v.Name) {
5590
fmt.Printf("%-24s %-14s %-40s\n", v.ID, v.Type, v.Name)
@@ -66,4 +101,7 @@ func init() {
66101
RootCmd.AddCommand(listCmd)
67102
listCmd.Flags().StringVarP(&listdbtype, "type", "t", "", "Only this database type")
68103
listCmd.Flags().StringVarP(&listfilter, "filter", "f", "", "Regular expression to filter names on")
104+
listCmd.Flags().BoolVarP(&byname, "sortname", "n", false, "Sort by name")
105+
listCmd.Flags().BoolVarP(&bytype, "sorttype", "d", false, "Sort by type")
106+
listCmd.Flags().BoolVarP(&byid, "sortid", "i", false, "Sort by id")
69107
}

0 commit comments

Comments
 (0)