This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
lscmd.go
137 lines (111 loc) · 2.99 KB
/
lscmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"golang.org/x/xerrors"
"go.coder.com/cli"
"go.coder.com/flog"
)
type lscmd struct {
all bool
}
func (c *lscmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "ls",
Desc: fmt.Sprintf(`Lists all containers with the %v label.`, sailLabel),
}
}
func (c *lscmd) RegisterFlags(fl *flag.FlagSet) {
fl.BoolVar(&c.all, "all", false, "Show stopped container.")
}
// projectInfo contains high-level project metadata as returned by the ls
// command.
type projectInfo struct {
name string
hat string
url string
status string
}
// listProjects grabs a list of all projects.:
func listProjects() ([]projectInfo, error) {
cnts, err := listContainers()
if err != nil {
return nil, xerrors.Errorf("failed to list containers: %w", err)
}
infos := make([]projectInfo, 0, len(cnts))
for _, cnt := range cnts {
var info projectInfo
dockerName := trimDockerName(cnt)
if dockerName == "" {
flog.Error("container %v doesn't have a name.", cnt.ID)
continue
}
info.name = toSailName(dockerName)
url, err := proxyURL(dockerName)
if err != nil {
flog.Error("container %v doesn't have a proxy URL.", info.name)
continue
}
info.url = url
info.hat = cnt.Labels[hatLabel]
info.status = cnt.Status
infos = append(infos, info)
}
return infos, nil
}
func (c *lscmd) Run(fl *flag.FlagSet) {
infos, err := listProjects()
if err != nil {
flog.Fatal("failed to list projects: %v", err)
}
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintf(tw, "name\that\turl\tstatus\n")
for _, info := range infos {
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\n", info.name, info.hat, info.url, info.status)
}
tw.Flush()
os.Exit(0)
}
// listContainers lists the sail containers on the host that
// are filterable by the sail label: com.coder.sail
func listContainers() ([]types.Container, error) {
cli := dockerClient()
defer cli.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
filter := filters.NewArgs()
filter.Add("label", sailLabel)
return cli.ContainerList(ctx, types.ContainerListOptions{
All: true,
Filters: filter,
})
}
// trimDockerName trims the `/` prefix from the docker container name.
// If the container isn't named, this will return the empty string.
func trimDockerName(cnt types.Container) string {
if len(cnt.Names) == 0 {
// All sail containers should be named.
return ""
}
return strings.TrimPrefix(cnt.Names[0], "/")
}
// toSailName converts the first _ into a / in order to produce a
// sail-friendly name.
//
// TODO: this is super janky.
func toSailName(dockerName string) string {
return strings.Replace(dockerName, "_", "/", 1)
}
// toDockerName converts the first / into a _ in order to produce
// a docker-friendly name.
//
// TODO: this is super janky.
func toDockerName(sailName string) string {
return strings.Replace(sailName, "/", "_", 1)
}