-
Notifications
You must be signed in to change notification settings - Fork 3
/
docker.go
151 lines (137 loc) · 3.61 KB
/
docker.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
package build
import (
"context"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"github.com/sohaha/zlsgo/zshell"
"github.com/sohaha/zlsgo/zutil"
"github.com/sohaha/zzz/util"
)
var DockerDist = "seekwe/go-builder:"
type OSData struct {
Goos string
Goarch string
CXX string
CC string
}
func CheckDocker() error {
util.Log.Println("Checking docker ...")
if _, _, _, err := zshell.Run("docker version"); err != nil {
return err
}
return nil
}
func CheckDockerImage(goVersion string) (bool, error, string) {
image := DockerDist + goVersion
util.Log.Printf("Checking for required docker image %s ...\n", image)
out, err := exec.Command("docker", "images", "--no-trunc").Output()
if err != nil {
return false, err, ""
}
res := strings.SplitN(image, ":", 2)
r, t := res[0], res[1]
match, _ := regexp.Match(fmt.Sprintf(`%s\s+%s`, r, t), out)
return match, nil, image
}
func PullDockerImage(image string) error {
util.Log.Printf("Pulling %s from docker registry...\n", image)
_, _, _, err := zshell.ExecCommand(context.Background(), []string{"docker", "pull", image}, nil, os.Stdout, os.Stderr)
return err
}
func CommadString(os []OSData, isVendor, isCGO bool, packageName, outDir string) (commad []string) {
cgo := ""
vendor := ""
if isCGO {
cgo = " CGO_ENABLED=1"
}
if isVendor {
vendor = " -mod=vendor "
}
for _, v := range os {
cc := ""
if isCGO {
switch v.Goos {
case "windows":
if v.Goarch == "386" {
cc = fmt.Sprintf("CC=%s CXX=%s", "i686-w64-mingw32-gcc", "i686-w64-mingw32-g++")
} else {
cc = fmt.Sprintf("CC=%s CXX=%s", "x86_64-w64-mingw32-gcc", "x86_64-w64-mingw32-g++")
}
case "darwin":
if v.Goarch == "386" {
cc = fmt.Sprintf("CC=%s CXX=%s", "o32-clang", "o32-clang++")
} else {
cc = fmt.Sprintf("CC=%s CXX=%s", "o64-clang", "o64-clang++")
}
case "linux":
if v.Goarch == "386" {
cc = "HOST=i686-linux PREFIX=/usr/local"
}
}
}
name := packageName + "_" + v.Goos + "_" + v.Goarch
commad = append(commad, fmt.Sprintf("%s%s GOARCH=%s GOOS=%s go build -o=%s%s%s", cc, cgo, v.Goarch, v.Goos, outDir, zutil.IfVal(v.Goos == "windows", name+".exe", name).(string), vendor))
}
if len(commad) == 0 {
cmd := "go build " + vendor
commad = []string{cmd}
if outDir != "" {
name := packageName
commad = []string{cmd + " -o=" + outDir + zutil.IfVal(zutil.IsWin(), name+".exe", name).(string)}
}
}
return
}
func TargetsCommad(target string) map[string][]string {
var (
goos = target
goarch = ""
commad = map[string][]string{}
)
t := strings.Split(target, "/")
if len(t) > 1 {
goos = t[0]
goarch = t[1]
}
switch goos {
case "w", "win", "windows":
commad["windows"] = ParserArch(goarch, []string{"386", "amd64"})
case "l", "linux":
commad["linux"] = ParserArch(goarch, []string{"386", "amd64"})
case "d", "darwin", "mac", "m":
commad["darwin"] = ParserArch(goarch, []string{"amd64"})
case "android", "a":
commad["android"] = ParserArch(goarch, []string{"arm64"})
default:
if goos == "" {
break
}
if goarch == "*" || goarch == "" {
util.Log.Errorf("There is no GOARCH preset for %s, please complete it, for example: linux/amd64,windows/386\n", goos)
return nil
}
commad[goos] = ParserArch(goarch, []string{})
}
return commad
}
func ParserArch(arch string, lists []string) []string {
archs := make([]string, 0)
switch arch {
case "", "*":
archs = lists
case "32":
archs = append(archs, "386")
case "64":
archs = append(archs, "amd64")
default:
archs = append(archs, arch)
}
return archs
}
func ParserTarget(cross string) []string {
targets := strings.Split(cross, ",")
return targets
}