1
0
mirror of https://github.com/drone/drone-cli.git synced 2024-11-26 06:07:05 +01:00

Add a filter for server state

This commit is contained in:
Thomas Boerger 2019-10-18 10:06:12 +02:00
parent cb187f05cb
commit b4012c3539
No known key found for this signature in database
GPG Key ID: 09745AFF9D63C79B

@ -20,6 +20,10 @@ var serverListCmd = cli.Command{
ArgsUsage: " ", ArgsUsage: " ",
Action: serverList, Action: serverList,
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.StringFlag{
Name: "s, state",
Usage: "filter by state",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "a, all", Name: "a, all",
Usage: "include stopped servers", Usage: "include stopped servers",
@ -49,6 +53,7 @@ func serverList(c *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
s := c.String("s")
a := c.Bool("a") a := c.Bool("a")
l := c.Bool("l") l := c.Bool("l")
h := c.BoolT("H") h := c.BoolT("H")
@ -64,7 +69,7 @@ func serverList(c *cli.Context) error {
} }
if l && h { if l && h {
printLong(servers, a, h) printLong(servers, s, a, h)
return nil return nil
} }
@ -77,12 +82,17 @@ func serverList(c *cli.Context) error {
if !a && server.State == "stopped" { if !a && server.State == "stopped" {
continue continue
} }
if s != "" && s != server.State {
continue
}
tmpl.Execute(os.Stdout, server) tmpl.Execute(os.Stdout, server)
} }
return nil return nil
} }
func printLong(servers []*drone.Server, a, h bool) { func printLong(servers []*drone.Server, s string, a, h bool) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
if h { if h {
fmt.Fprintln(w, "Name\tAddress\tState\tCreated") fmt.Fprintln(w, "Name\tAddress\tState\tCreated")
@ -91,6 +101,9 @@ func printLong(servers []*drone.Server, a, h bool) {
if !a && server.State == "stopped" { if !a && server.State == "stopped" {
continue continue
} }
if s != "" && s != server.State {
continue
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\n", fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\n",
server.Name, server.Name,
server.Address, server.Address,