forked from ovhlabs/sail
/
list.go
58 lines (50 loc) · 1.73 KB
/
list.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
package service
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"text/tabwriter"
"github.com/spf13/cobra"
"github.com/runabove/sail/internal"
)
var cmdServiceList = &cobra.Command{
Use: "list",
Short: "List the docker services: sail service list [applicationName]",
Aliases: []string{"ls", "ps"},
Run: func(cmd *cobra.Command, args []string) {
serviceList(internal.GetListApplications(args))
},
}
func serviceList(apps []string) {
w := tabwriter.NewWriter(os.Stdout, 27, 1, 2, ' ', 0)
titles := []string{"NAME", "REPOSITORY", "IMAGE ID", "STATE", "CONTAINERS", "CREATED", "NETWORK"}
fmt.Fprintln(w, strings.Join(titles, "\t"))
services := []string{}
var service map[string]interface{}
for _, app := range apps {
b := internal.ReqWant("GET", http.StatusOK, fmt.Sprintf("/applications/%s/services", app), nil)
internal.Check(json.Unmarshal(b, &services))
for _, serviceID := range services {
b := internal.ReqWant("GET", http.StatusOK, fmt.Sprintf("/applications/%s/services/%s", app, serviceID), nil)
internal.Check(json.Unmarshal(b, &service))
ips := []string{}
for _, container := range service["containers"].(map[string]interface{}) {
for name, network := range container.(map[string]interface{})["network"].(map[string]interface{}) {
ips = append(ips, fmt.Sprintf("%s:%s", name, network.(map[string]interface{})["ip"]))
}
}
fmt.Fprintf(w, "%s/%s\t%s@%s\t%s\t%s\t%d\t%s\t%s\n",
app, service["name"],
service["repository"],
service["repository_tag"],
service["image"].(string)[:12],
strings.ToUpper(service["state"].(string)),
int(service["container_number"].(float64)),
service["creation_date"].(string)[:19],
strings.Join(ips, ","))
w.Flush()
}
}
}