@@ -11,6 +11,7 @@ import (
11
11
"github.com/docker/docker/api/types"
12
12
"github.com/docker/docker/api/types/filters"
13
13
"go.coder.com/flog"
14
+ "golang.org/x/xerrors"
14
15
)
15
16
16
17
type lscmd struct {
@@ -29,7 +30,17 @@ func (c *lscmd) initFlags(fl *flag.FlagSet) {
29
30
fl .BoolVar (& c .all , "all" , false , "Show stopped container." )
30
31
}
31
32
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 ) {
33
44
cli := dockerClient ()
34
45
defer cli .Close ()
35
46
@@ -40,35 +51,48 @@ func (c *lscmd) handle(gf globalFlags, fl *flag.FlagSet) {
40
51
filter .Add ("label" , sailLabel )
41
52
42
53
cnts , err := cli .ContainerList (ctx , types.ContainerListOptions {
43
- All : c . all ,
54
+ All : true ,
44
55
Filters : filter ,
45
56
})
46
57
if err != nil {
47
- flog . Fatal ("failed to list containers: %v " , err )
58
+ return nil , xerrors . Errorf ("failed to list containers: %w " , err )
48
59
}
49
60
50
- tw := tabwriter . NewWriter ( os . Stdout , 0 , 0 , 3 , ' ' , 0 )
61
+ infos := make ([] projectInfo , 0 , len ( cnts ) )
51
62
52
- fmt .Fprintf (tw , "name\t hat\t url\t status\n " )
53
63
for _ , cnt := range cnts {
54
- var name string
64
+ var info projectInfo
55
65
if len (cnt .Names ) == 0 {
56
66
// All sail containers should be named.
57
67
flog .Error ("container %v doesn't have a name." , cnt .ID )
58
68
continue
59
69
}
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 ], "/" )
66
71
// Convert the first - into a / in order to produce a
67
72
// sail-friendly name.
68
73
// 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
+ }
70
81
71
- fmt .Fprintf (tw , "%v\t %v\t http://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\t hat\t url\t status\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 )
72
96
}
73
97
tw .Flush ()
74
98
0 commit comments