-
Notifications
You must be signed in to change notification settings - Fork 288
/
read.go
63 lines (49 loc) · 1.18 KB
/
read.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
package dockercompose
import (
"context"
"strings"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v2"
)
func ReadConfigAndServiceNames(ctx context.Context, dcc DockerComposeClient,
configPaths []string) (conf Config, svcNames []string, err error) {
// calls to `docker-compose config` take a bit, and we need two,
// so do them in parallel to make things faster
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
configOut, err := dcc.Config(ctx, configPaths)
if err != nil {
return err
}
err = yaml.Unmarshal([]byte(configOut), &conf)
if err != nil {
return err
}
return nil
})
g.Go(func() error {
var err error
svcNames, err = serviceNames(ctx, dcc, configPaths)
if err != nil {
return err
}
return nil
})
err = g.Wait()
return conf, svcNames, err
}
func serviceNames(ctx context.Context, dcc DockerComposeClient, configPaths []string) ([]string, error) {
servicesText, err := dcc.Services(ctx, configPaths)
if err != nil {
return nil, err
}
serviceNames := strings.Split(servicesText, "\n")
var result []string
for _, name := range serviceNames {
if name == "" {
continue
}
result = append(result, name)
}
return result, nil
}