-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.go
More file actions
135 lines (125 loc) · 2.82 KB
/
Copy pathoption.go
File metadata and controls
135 lines (125 loc) · 2.82 KB
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
package beamer
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"strings"
"unicode"
)
const (
optionSearchPattern = "options.get"
blank = ""
)
type JobConfig struct {
JobName string
GCSLocation string
StagingLocation string
Region string
Project string
ServiceAccountEmail string
Parameters map[string]string
}
func (jc *JobConfig) Validate(skipValidation bool) {
errors := []string{}
if jc.GCSLocation == blank {
errors = append(errors, "Error: GcsLocation is required")
}
if jc.StagingLocation == blank {
errors = append(errors, "Error: Staging Location is required")
}
if jc.JobName == blank {
errors = append(errors, "Error: Job Name is required")
}
if jc.Project == blank {
errors = append(errors, "Error: Project is required")
}
if jc.ServiceAccountEmail == blank {
errors = append(errors, "Error: ServiceAccount is required")
}
for k, v := range jc.Parameters {
if v == blank {
errors = append(errors, fmt.Sprintf("Parameter Error: %v is required", k))
}
}
if !skipValidation {
if jc.Region == blank {
errors = append(errors, "Error: Region is required")
}
}
if len(errors) > 0 {
for _, err := range errors {
fmt.Println(err)
}
os.Exit(64)
}
}
func (jc *JobConfig) ParamString() string {
params := []string{}
for k, v := range jc.Parameters {
params = append(params, fmt.Sprintf("%v=\"%v\"", k, v))
}
return strings.Join(params, ",")
}
type JobOptions []string
func (options JobOptions) WriteToFile(fileName string) {
config := map[string]interface{}{
"JobName": blank,
"GCSLocation": blank,
"StagingLocation": blank,
"Region": blank,
"Project": blank,
"ServiceAccountEmail": blank,
}
if len(options) < 1 {
return
}
parameters := map[string]string{}
for _, option := range options {
parameters[option] = blank
}
config["Parameters"] = parameters
b, err := json.Marshal(config)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(fmt.Sprintf(".beamer/%s", fileName), b, 0644)
if err != nil {
panic(err)
}
}
func ExtractOptionsFromFile(filePath string) JobOptions {
var options JobOptions
reader := open(filePath)
re := regexp.MustCompile("[^a-zA-Z0-9]+")
for {
line, err := reader.ReadBytes('\n')
if err == io.EOF {
break
}
line = line[:len(line)-1]
text := string(line)
if strings.Contains(text, optionSearchPattern) {
s := strings.Split(text, optionSearchPattern)
option := re.ReplaceAllString(s[1], blank)
options = append(options, lcFirst(option))
}
}
return options
}
func open(path string) *bufio.Reader {
file, err := os.Open(path)
if err != nil {
panic("cannot open file")
}
return bufio.NewReader(file)
}
func lcFirst(str string) string {
for i, v := range str {
return string(unicode.ToLower(v)) + str[i+1:]
}
return blank
}