Skip to content

Commit

Permalink
sort list of services by name
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmonsays committed Jan 21, 2016
1 parent 049fc76 commit d391965
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cmd/runitcmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package main
import (
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"

"github.com/codegangsta/cli"

"github.com/sigmonsays/runitcmd/runit"
)

func initList(app *Application) cli.Command {
Expand Down Expand Up @@ -53,6 +56,35 @@ func formatTime(seconds int64) string {
return strings.Join(ret, "")
}

type SortBy func(s1, s2 *runit.Service) bool

func (me SortBy) Sort(services []*runit.Service) {
ps := &ServiceSort{
services: services,
by: me,
}
sort.Sort(ps)
}

type ServiceSort struct {
services []*runit.Service
by func(s1, s2 *runit.Service) bool
}

func (s *ServiceSort) Len() int {
return len(s.services)
}
func (s *ServiceSort) Swap(i, j int) {
s.services[i], s.services[j] = s.services[j], s.services[i]
}
func (s *ServiceSort) Less(i, j int) bool {
return s.by(s.services[i], s.services[j])
}

func NameSort(s1, s2 *runit.Service) bool {
return s1.Name < s2.Name
}

func (app *Application) List(c *cli.Context) {
show_all := c.Bool("all")

Expand All @@ -64,8 +96,11 @@ func (app *Application) List(c *cli.Context) {
return
}

SortBy(NameSort).Sort(services)

tw := new(tabwriter.Writer)
tw.Init(os.Stdout, 0, 8, 0, '\t', 0)

for _, service := range services {
if service.Exists() == false {
continue
Expand Down

0 comments on commit d391965

Please sign in to comment.