-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
142 lines (123 loc) · 2.89 KB
/
main.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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/buildkite/codebuild-run-build/runner"
"github.com/urfave/cli"
)
var (
Version string
)
func main() {
app := cli.NewApp()
app.Name = "codebuild-run-build"
app.Usage = "Run a build on CodeBuild and tail the output from Cloudwatch"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "Show debugging information",
},
cli.StringFlag{
Name: "project-name, n",
Usage: "Project name",
},
cli.BoolFlag{
Name: "no-artifacts",
Usage: "Disable artifacts for this build",
},
cli.StringFlag{
Name: "source-type-override",
Usage: "Override the Source Type for this build",
},
cli.StringFlag{
Name: "source-location-override",
Usage: "Override the Source Location for this build",
},
cli.StringSliceFlag{
Name: "env, e",
Usage: "Additional environment",
},
cli.StringFlag{
Name: "env-file",
Usage: "Additional environment in a file in KEY=\"value\" form",
},
}
app.Action = func(ctx *cli.Context) error {
requireFlagValue(ctx, "project-name")
if !ctx.Bool("debug") {
log.SetOutput(ioutil.Discard)
}
r := runner.New()
r.ProjectName = ctx.String("project-name")
r.SourceType = ctx.String("source-type-override")
r.SourceLocation = ctx.String("source-location-override")
r.NoArtifacts = ctx.Bool("no-artifacts")
if ctx.IsSet("env-file") {
env, err := readEnvFile(ctx.String("env-file"))
if err != nil {
fmt.Println(err)
os.Exit(2)
}
r.Env = append(r.Env, env...)
}
if ctx.IsSet("env") {
for _, s := range ctx.StringSlice("env") {
env, err := parseEnv(s)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
r.Env = append(r.Env, env)
}
}
if err := r.Run(); err != nil {
if ec, ok := err.(cli.ExitCoder); ok {
return ec
}
fmt.Println(err)
os.Exit(1)
}
return nil
}
app.Run(os.Args)
}
func requireFlagValue(ctx *cli.Context, name string) {
if ctx.String(name) == "" {
fmt.Printf("ERROR: Required flag %q isn't set\n\n", name)
cli.ShowAppHelpAndExit(ctx, 1)
}
}
func parseEnv(s string) (runner.Env, error) {
parts := strings.SplitN(s, "=", 2)
if len(parts) != 2 {
return runner.Env{}, fmt.Errorf("Failed to parse env %q", s)
}
parsedString := strings.Trim(parts[1], `"`)
parsedString = strings.Replace(parsedString, `\t`, "\t", -1)
parsedString = strings.Replace(parsedString, `\n`, "\n", -1)
return runner.Env{Name: parts[0], Value: parsedString}, nil
}
func readEnvFile(fileName string) ([]runner.Env, error) {
var envs []runner.Env
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
env, err := parseEnv(scanner.Text())
if err != nil {
return nil, err
}
envs = append(envs, env)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return envs, nil
}