Skip to content
This repository was archived by the owner on Aug 14, 2020. It is now read-only.

Commit eb7a569

Browse files
committed
Merge pull request #2 from endocode/krnowak/work
Address some TODOs
2 parents d277c9b + 22a59ad commit eb7a569

File tree

1 file changed

+64
-25
lines changed

1 file changed

+64
-25
lines changed

goaci.go

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"archive/tar"
55
"bytes"
66
"compress/gzip"
7+
"flag"
78
"fmt"
89
"io/ioutil"
910
"os"
@@ -18,9 +19,13 @@ import (
1819

1920
var Debug bool
2021

21-
func die(s string, i ...interface{}) {
22+
func warn(s string, i ...interface{}) {
2223
s = fmt.Sprintf(s, i...)
2324
fmt.Fprintln(os.Stderr, strings.TrimSuffix(s, "\n"))
25+
}
26+
27+
func die(s string, i ...interface{}) {
28+
warn(s, i...)
2429
os.Exit(1)
2530
}
2631

@@ -31,41 +36,71 @@ func debug(i ...interface{}) {
3136
}
3237
}
3338

39+
type StringVector []string
40+
41+
func (v *StringVector) String() string {
42+
return `"` + strings.Join(*v, `" "`) + `"`
43+
}
44+
45+
func (v *StringVector) Set(str string) error {
46+
*v = append(*v, str)
47+
return nil
48+
}
49+
3450
func main() {
51+
var (
52+
execOpts StringVector
53+
goDefaultBinaryDesc string
54+
goBinaryOpt string
55+
goPathOpt string
56+
)
57+
58+
// Find the go binary
59+
gocmd, err := exec.LookPath("go")
60+
61+
flag.Var(&execOpts, "exec", "Parameters passed to app, can be used multiple times")
62+
if err != nil {
63+
goDefaultBinaryDesc = "Go binary to use (default: none found in $PATH, so it must be provided)"
64+
} else {
65+
goDefaultBinaryDesc = "Go binary to use (default: whatever go in $PATH)"
66+
}
67+
flag.StringVar(&goBinaryOpt, "go-binary", gocmd, goDefaultBinaryDesc)
68+
flag.StringVar(&goPathOpt, "go-path", "", "Custom GOPATH (default: a temporary directory)")
69+
flag.Parse()
3570
if os.Getenv("GOPATH") != "" {
36-
die("to avoid confusion GOPATH must not be set")
71+
warn("GOPATH env var is ignored, use --go-path=\"$GOPATH\" option instead")
3772
}
38-
goroot := os.Getenv("GOROOT")
39-
if goroot == "" {
40-
die("GOROOT must be set")
73+
goRoot := os.Getenv("GOROOT")
74+
if goRoot != "" {
75+
warn("Overriding GOROOT env var to %s", goRoot)
4176
}
4277
if os.Getenv("GOACI_DEBUG") != "" {
4378
Debug = true
4479
}
4580

81+
if goBinaryOpt == "" {
82+
die("go binary not found")
83+
}
84+
4685
// Set up a temporary directory for everything (gopath and builds)
4786
tmpdir, err := ioutil.TempDir("", "goaci")
4887
if err != nil {
4988
die("error setting up temporary directory: %v", err)
5089
}
5190
defer os.RemoveAll(tmpdir)
91+
if goPathOpt == "" {
92+
goPathOpt = tmpdir
93+
}
5294

5395
// Scratch build dir for aci
5496
acidir := filepath.Join(tmpdir, "aci")
5597

56-
// Be explicit with gobin
98+
// Let's put final binary in tmpdir
5799
gobin := filepath.Join(tmpdir, "bin")
58100

59-
// Find the go binary
60-
gocmd, err := exec.LookPath("go")
61-
if err != nil {
62-
die("could not find `go` in path")
63-
}
64-
65101
// Construct args for a go get that does a static build
66-
// TODO(jonboulle): go version 1.4
67102
args := []string{
68-
gocmd,
103+
goBinaryOpt,
69104
"get",
70105
"-a",
71106
"-tags", "netgo",
@@ -87,6 +122,7 @@ func main() {
87122
if err != nil {
88123
die("bad app name: %v", err)
89124
}
125+
args = append(args, ns)
90126

91127
// Use the last component, e.g. example.com/my/app --> app
92128
ofn := filepath.Base(ns) + ".aci"
@@ -96,15 +132,18 @@ func main() {
96132
die("error opening output file: %v", err)
97133
}
98134

135+
env := []string{
136+
"GOPATH=" + goPathOpt,
137+
"GOBIN=" + gobin,
138+
"CGO_ENABLED=0",
139+
"PATH=" + os.Getenv("PATH"),
140+
}
141+
if goRoot != "" {
142+
env = append(env, "GOROOT="+goRoot)
143+
}
99144
cmd := exec.Cmd{
100-
Env: []string{
101-
"GOPATH=" + tmpdir,
102-
"GOBIN=" + gobin,
103-
"GOROOT=" + goroot,
104-
"CGO_ENABLED=0",
105-
"PATH=" + os.Getenv("PATH"),
106-
},
107-
Path: gocmd,
145+
Env: env,
146+
Path: goBinaryOpt,
108147
Args: args,
109148
Stderr: os.Stderr,
110149
Stdout: os.Stdout,
@@ -145,15 +184,15 @@ func main() {
145184
}
146185
debug("moved binary to:", ep)
147186

187+
exec := []string{filepath.Join("/", fn)}
188+
exec = append(exec, execOpts...)
148189
// Build the ACI
149190
im := schema.ImageManifest{
150191
ACKind: types.ACKind("ImageManifest"),
151192
ACVersion: schema.AppContainerVersion,
152193
Name: *name,
153194
App: &types.App{
154-
Exec: types.Exec{
155-
filepath.Join("/", fn),
156-
},
195+
Exec: exec,
157196
User: "0",
158197
Group: "0",
159198
},

0 commit comments

Comments
 (0)