-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (120 loc) · 2.8 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"context"
"encoding/json"
"flag"
"log"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
type App struct {
Name string `json:"name"`
Url string `json:"url"`
Icon string `json:"icon"`
}
func main() {
outputFile := flag.String("apps-config", "./config/apps.json", "Location of apps.json file")
flag.Parse()
log.Println("Run started")
containers, err := getContainers()
if err != nil {
log.Fatal(err.Error())
}
apps, err := parseLabels(containers)
if err != nil {
log.Fatal(err.Error())
}
err = writeAppsFile(apps, *outputFile)
if err != nil {
log.Fatal(err.Error())
}
log.Println("Run finished")
}
// parseLabels retrieves Docker containers with sui labels
func parseLabels(containers []types.Container) ([]App, error) {
apps := []App{}
for _, container := range containers {
app := newApp(container)
// If app is empty, we do not append
if app == (App{}) {
log.Println("Container has no sui labels")
continue
}
apps = append(apps, app)
}
return apps, nil
}
// writeAppsFile writes apps to a json file. outputFile will be created if it does not exist.
// outputFile will always be overwritten.
func writeAppsFile(apps []App, outputFile string) error {
err := createFileIfNotExists(outputFile)
if err != nil {
return err
}
a := struct {
Apps []App `json:"apps"`
}{
apps,
}
data, err := json.MarshalIndent(a, "", " ")
if err != nil {
return err
}
err = os.WriteFile(outputFile, data, 0755)
if err != nil {
return err
}
return nil
}
func newApp(c types.Container) App {
log.Printf("Parsing labels from container: %+q", c.Names)
app := App{}
app.Name = parseName(c.Labels)
app.Url = parseUrl(c.Labels)
app.Icon = parseIcon(c.Labels)
return app
}
func parseName(m map[string]string) string {
if val, ok := m["sui.app.name"]; ok {
log.Printf("Container label sui.app.name: %s\n", val)
return val
}
return ""
}
func parseUrl(m map[string]string) string {
if val, ok := m["sui.app.url"]; ok {
log.Printf("Container label sui.app.url: %s\n", val)
return val
}
return ""
}
func parseIcon(m map[string]string) string {
if val, ok := m["sui.app.icon"]; ok {
log.Printf("Container label sui.app.icon: %s\n", val)
return val
}
return ""
}
func getContainers() ([]types.Container, error) {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return []types.Container{}, err
}
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
return []types.Container{}, err
}
return containers, nil
}
func createFileIfNotExists(file string) error {
_, err := os.Stat(file)
if os.IsNotExist(err) {
_, err := os.Create(file)
if err != nil {
return err
}
}
return nil
}