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

Commit 06e9a16

Browse files
committed
Abstract project listing for Kyle
1 parent 0c9921c commit 06e9a16

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

lscmd.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/docker/docker/api/types"
1212
"github.com/docker/docker/api/types/filters"
1313
"go.coder.com/flog"
14+
"golang.org/x/xerrors"
1415
)
1516

1617
type lscmd struct {
@@ -29,7 +30,17 @@ func (c *lscmd) initFlags(fl *flag.FlagSet) {
2930
fl.BoolVar(&c.all, "all", false, "Show stopped container.")
3031
}
3132

32-
func (c *lscmd) handle(gf globalFlags, fl *flag.FlagSet) {
33+
// projectInfo contains high-level project metadata as returned by the ls
34+
// command.
35+
type projectInfo struct {
36+
name string
37+
hat string
38+
url string
39+
status string
40+
}
41+
42+
// listProjects grabs a list of all projects.:
43+
func listProjects() ([]projectInfo, error) {
3344
cli := dockerClient()
3445
defer cli.Close()
3546

@@ -40,35 +51,48 @@ func (c *lscmd) handle(gf globalFlags, fl *flag.FlagSet) {
4051
filter.Add("label", sailLabel)
4152

4253
cnts, err := cli.ContainerList(ctx, types.ContainerListOptions{
43-
All: c.all,
54+
All: true,
4455
Filters: filter,
4556
})
4657
if err != nil {
47-
flog.Fatal("failed to list containers: %v", err)
58+
return nil, xerrors.Errorf("failed to list containers: %w", err)
4859
}
4960

50-
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
61+
infos := make([]projectInfo, 0, len(cnts))
5162

52-
fmt.Fprintf(tw, "name\that\turl\tstatus\n")
5363
for _, cnt := range cnts {
54-
var name string
64+
var info projectInfo
5565
if len(cnt.Names) == 0 {
5666
// All sail containers should be named.
5767
flog.Error("container %v doesn't have a name.", cnt.ID)
5868
continue
5969
}
60-
name = strings.TrimPrefix(cnt.Names[0], "/")
61-
62-
var (
63-
port = cnt.Labels[portLabel]
64-
hat = cnt.Labels[hatLabel]
65-
)
70+
info.name = strings.TrimPrefix(cnt.Names[0], "/")
6671
// Convert the first - into a / in order to produce a
6772
// sail-friendly name.
6873
// TODO: this is super janky.
69-
name = strings.Replace(name, "-", "/", 1)
74+
info.name = strings.Replace(info.name, "-", "/", 1)
75+
76+
info.url = "http://127.0.0.1:" + cnt.Labels[portLabel]
77+
info.hat = cnt.Labels[hatLabel]
78+
79+
infos = append(infos, info)
80+
}
7081

71-
fmt.Fprintf(tw, "%v\t%v\thttp://127.0.0.1:%v\t%v\n", name, hat, port, cnt.Status)
82+
return infos, nil
83+
}
84+
85+
func (c *lscmd) handle(gf globalFlags, fl *flag.FlagSet) {
86+
infos, err := listProjects()
87+
if err != nil {
88+
flog.Fatal("failed to list projects: %v", err)
89+
}
90+
91+
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
92+
93+
fmt.Fprintf(tw, "name\that\turl\tstatus\n")
94+
for _, info := range infos {
95+
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\n", info.name, info.hat, info.url, info.status)
7296
}
7397
tw.Flush()
7498

0 commit comments

Comments
 (0)