This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
/
go.go
195 lines (167 loc) · 4.77 KB
/
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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package proj2aci
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/schema"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/schema/types"
)
type GoConfiguration struct {
CommonConfiguration
GoBinary string
GoPath string
}
type GoPaths struct {
CommonPaths
project string
realGo string
fakeGo string
goRoot string
goBin string
}
type GoCustomizations struct {
Configuration GoConfiguration
paths GoPaths
app string
}
func (custom *GoCustomizations) Name() string {
return "go"
}
func (custom *GoCustomizations) GetCommonConfiguration() *CommonConfiguration {
return &custom.Configuration.CommonConfiguration
}
func (custom *GoCustomizations) GetCommonPaths() *CommonPaths {
return &custom.paths.CommonPaths
}
func (custom *GoCustomizations) ValidateConfiguration() error {
if custom.Configuration.GoBinary == "" {
return fmt.Errorf("Go binary not found")
}
return nil
}
func (custom *GoCustomizations) SetupPaths() error {
custom.paths.realGo, custom.paths.fakeGo = custom.getGoPath()
if os.Getenv("GOPATH") != "" {
Warn("GOPATH env var is ignored, use --go-path=\"$GOPATH\" option instead")
}
custom.paths.goRoot = os.Getenv("GOROOT")
if custom.paths.goRoot != "" {
Warn("Overriding GOROOT env var to ", custom.paths.goRoot)
}
projectName := getProjectName(custom.Configuration.Project)
// Project name is path-like string with slashes, but slash is
// not a file separator on every OS.
custom.paths.project = filepath.Join(custom.paths.realGo, "src", filepath.Join(strings.Split(projectName, "/")...))
custom.paths.goBin = filepath.Join(custom.paths.fakeGo, "bin")
return nil
}
// getGoPath returns go path and fake go path. The former is either in
// /tmp (which is a default) or some other path as specified by
// --go-path parameter. The latter is always in /tmp.
func (custom *GoCustomizations) getGoPath() (string, string) {
fakeGoPath := filepath.Join(custom.paths.TmpDir, "gopath")
if custom.Configuration.GoPath == "" {
return fakeGoPath, fakeGoPath
}
return custom.Configuration.GoPath, fakeGoPath
}
func getProjectName(project string) string {
if filepath.Base(project) != "..." {
return project
}
return filepath.Dir(project)
}
func (custom *GoCustomizations) GetDirectoriesToMake() []string {
return []string{
custom.paths.fakeGo,
custom.paths.goBin,
}
}
func (custom *GoCustomizations) PrepareProject() error {
Info("Running go get")
// Construct args for a go get that does a static build
args := []string{
"go",
"get",
"-a",
custom.Configuration.Project,
}
env := []string{
"GOPATH=" + custom.paths.realGo,
"GOBIN=" + custom.paths.goBin,
"PATH=" + os.Getenv("PATH"),
}
if custom.paths.goRoot != "" {
env = append(env, "GOROOT="+custom.paths.goRoot)
}
cmd := exec.Cmd{
Env: env,
Path: custom.Configuration.GoBinary,
Args: args,
Stderr: os.Stderr,
Stdout: os.Stdout,
}
Debug("env: ", cmd.Env)
Debug("running command: ", strings.Join(cmd.Args, " "))
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func (custom *GoCustomizations) GetPlaceholderMapping() map[string]string {
return map[string]string{
"<PROJPATH>": custom.paths.project,
"<GOPATH>": custom.paths.realGo,
}
}
func (custom *GoCustomizations) GetAssets(aciBinDir string) ([]string, error) {
name, err := custom.GetBinaryName()
if err != nil {
return nil, err
}
aciAsset := filepath.Join(aciBinDir, name)
localAsset := filepath.Join(custom.paths.goBin, name)
return []string{GetAssetString(aciAsset, localAsset)}, nil
}
func (custom *GoCustomizations) GetImageName() (*types.ACIdentifier, error) {
imageName := custom.Configuration.Project
if filepath.Base(imageName) == "..." {
imageName = filepath.Dir(imageName)
if custom.Configuration.UseBinary != "" {
imageName += "-" + custom.Configuration.UseBinary
}
}
return types.NewACIdentifier(strings.ToLower(imageName))
}
func (custom *GoCustomizations) GetBinaryName() (string, error) {
if err := custom.findBinaryName(); err != nil {
return "", err
}
return custom.app, nil
}
func (custom *GoCustomizations) findBinaryName() error {
if custom.app != "" {
return nil
}
binaryName, err := GetBinaryName(custom.paths.goBin, custom.Configuration.UseBinary)
if err != nil {
return err
}
custom.app = binaryName
return nil
}
func (custom *GoCustomizations) GetRepoPath() (string, error) {
return custom.paths.project, nil
}
func (custom *GoCustomizations) GetImageFileName() (string, error) {
base := filepath.Base(custom.Configuration.Project)
if base == "..." {
base = filepath.Base(filepath.Dir(custom.Configuration.Project))
if custom.Configuration.UseBinary != "" {
base += "-" + custom.Configuration.UseBinary
}
}
return base + schema.ACIExtension, nil
}