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

123 lines
2.1 KiB
Go
Raw Permalink Normal View History

2018-02-14 22:06:05 +01:00
package server
import (
2018-03-08 08:38:33 +01:00
"fmt"
2018-02-14 22:06:05 +01:00
"os"
2018-03-08 08:38:33 +01:00
"text/tabwriter"
2018-02-14 22:06:05 +01:00
"text/template"
2018-03-08 08:38:33 +01:00
"time"
2018-02-14 22:06:05 +01:00
2018-03-08 08:38:33 +01:00
"github.com/docker/go-units"
2018-02-14 22:06:05 +01:00
"github.com/drone/drone-cli/drone/internal"
2018-03-08 08:38:33 +01:00
"github.com/drone/drone-go/drone"
"github.com/drone/funcmap"
"github.com/urfave/cli"
2018-02-14 22:06:05 +01:00
)
var serverListCmd = cli.Command{
Name: "ls",
Usage: "list all servers",
ArgsUsage: " ",
Action: serverList,
Flags: []cli.Flag{
2019-10-18 10:06:12 +02:00
cli.StringFlag{
Name: "s, state",
Usage: "filter by state",
},
2018-03-01 20:23:03 +01:00
cli.BoolFlag{
Name: "a, all",
2018-03-08 08:38:33 +01:00
Usage: "include stopped servers",
},
cli.BoolFlag{
Name: "l, long",
2018-03-08 08:38:33 +01:00
Usage: "list in long format",
},
cli.BoolTFlag{
Name: "H, headers",
2019-05-05 23:21:17 +02:00
Usage: "include column headers",
2018-03-01 20:23:03 +01:00
},
2018-02-14 22:06:05 +01:00
cli.StringFlag{
2019-07-18 23:43:42 +02:00
Name: "format",
Usage: "format output",
Value: tmplServerList,
2018-02-14 22:06:05 +01:00
},
2018-03-09 18:18:47 +01:00
cli.BoolFlag{
Name: "la",
Hidden: true,
},
2018-02-14 22:06:05 +01:00
},
}
func serverList(c *cli.Context) error {
client, err := internal.NewAutoscaleClient(c)
if err != nil {
return err
}
2019-10-18 10:06:12 +02:00
s := c.String("s")
2018-03-08 08:38:33 +01:00
a := c.Bool("a")
l := c.Bool("l")
h := c.BoolT("H")
2018-02-14 22:06:05 +01:00
2018-03-09 18:18:47 +01:00
if c.BoolT("la") {
l = true
a = true
}
2018-02-14 22:06:05 +01:00
servers, err := client.ServerList()
if err != nil || len(servers) == 0 {
return err
}
2018-03-08 08:38:33 +01:00
if l && h {
2019-10-18 10:06:12 +02:00
printLong(servers, s, a, h)
2018-03-08 08:38:33 +01:00
return nil
}
tmpl, err := template.New("_").Funcs(funcmap.Funcs).Parse(c.String("format") + "\n")
2018-02-14 22:06:05 +01:00
if err != nil {
return err
}
2018-03-08 08:38:33 +01:00
2018-02-14 22:06:05 +01:00
for _, server := range servers {
2018-03-08 08:38:33 +01:00
if !a && server.State == "stopped" {
2018-03-01 20:23:03 +01:00
continue
}
2019-10-18 10:06:12 +02:00
if s != "" && s != server.State {
continue
}
2018-02-14 22:06:05 +01:00
tmpl.Execute(os.Stdout, server)
}
return nil
}
2019-10-18 10:06:12 +02:00
func printLong(servers []*drone.Server, s string, a, h bool) {
2018-03-08 08:38:33 +01:00
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
if h {
fmt.Fprintln(w, "Name\tAddress\tState\tCreated")
}
for _, server := range servers {
if !a && server.State == "stopped" {
continue
}
2019-10-18 10:06:12 +02:00
if s != "" && s != server.State {
continue
}
2018-03-08 08:38:33 +01:00
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\n",
server.Name,
server.Address,
server.State,
units.HumanDuration(
time.Now().Sub(
time.Unix(server.Created, 0),
),
),
)
}
w.Flush()
}
2018-02-14 22:06:05 +01:00
// template for server list items
var tmplServerList = `{{ .Name }}`