-
Notifications
You must be signed in to change notification settings - Fork 74
/
deps_go.go
75 lines (61 loc) 路 1.65 KB
/
deps_go.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
package build
import (
"context"
"io/ioutil"
"os"
"path"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
"github.com/testground/testground/pkg/rpc"
)
func parseDependencies(raw string) map[string]string {
rawModules := strings.Split(raw, "\n")
modules := map[string]string{}
for _, module := range rawModules {
module = strings.TrimSpace(module)
if module == "" {
continue
}
parts := strings.SplitN(module, " ", 2)
path := strings.TrimSpace(parts[0])
version := ""
if len(parts) == 2 {
version = strings.TrimSpace(parts[1])
}
modules[path] = version
}
return modules
}
func parseDependenciesFromDocker(ctx context.Context, ow *rpc.OutputWriter, cli *client.Client, imageID string) (map[string]string, error) {
res, err := cli.ContainerCreate(ctx, &container.Config{Image: imageID}, nil, nil, "")
if err != nil {
return nil, err
}
defer func() {
err = cli.ContainerRemove(context.Background(), res.ID, types.ContainerRemoveOptions{Force: true})
if err != nil {
ow.Warnf("error while removing container %s: %v", res.ID, err)
}
}()
tar, _, err := cli.CopyFromContainer(ctx, res.ID, "/testground_dep_list")
if err != nil {
return nil, err
}
dir, err := ioutil.TempDir("", res.ID)
if err != nil {
return nil, err
}
defer os.RemoveAll(dir)
err = archive.Untar(tar, dir, &archive.TarOptions{NoLchown: true})
if err != nil {
return nil, err
}
deps, err := ioutil.ReadFile(path.Join(dir, "testground_dep_list"))
if err != nil {
return nil, err
}
return parseDependencies(string(deps)), nil
}