-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathconfig.go
executable file
·204 lines (173 loc) · 6.36 KB
/
config.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
196
197
198
199
200
201
202
203
204
package main
import (
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
)
// Configs stores the step's inputs
type Configs struct {
JSONKeyPath stepconf.Secret `env:"service_account_json_key_path,required"`
PackageName string `env:"package_name,required"`
AppPath string `env:"app_path,required"`
ExpansionfilePath string `env:"expansionfile_path"`
Track string `env:"track,required"`
UserFraction float64 `env:"user_fraction,range]0.0..1.0["`
UpdatePriority int `env:"update_priority,range[0..5]"`
WhatsnewsDir string `env:"whatsnews_dir"`
MappingFile string `env:"mapping_file"`
ReleaseName string `env:"release_name"`
Status string `env:"status"`
RetryWithoutSendingToReview bool `env:"retry_without_sending_to_review,opt[true,false]"`
AckBundleInstallationWarning bool `env:"ack_bundle_installation_warning,opt[true,false]"`
DryRun bool `env:"dry_run,opt[true,false]"`
IsDebugLog bool `env:"verbose_log,opt[true,false]"`
}
// validate validates the Configs.
func (c Configs) validate() error {
if err := c.validateJSONKeyPath(); err != nil {
return err
}
if err := c.validateWhatsnewsDir(); err != nil {
return err
}
if err := c.validateMappingFile(); err != nil {
return err
}
return c.validateApps()
}
// validateJSONKeyPath validates if service_account_json_key_path input value exists if defined and has file:// URL scheme.
func (c Configs) validateJSONKeyPath() error {
if !strings.HasPrefix(string(c.JSONKeyPath), "file://") {
return nil
}
pth := strings.TrimPrefix(string(c.JSONKeyPath), "file://")
if exist, err := pathutil.IsPathExists(pth); err != nil {
return fmt.Errorf("failed to check if json key path exist at: %s, error: %s", pth, err)
} else if !exist {
return errors.New("json key path not exist at: " + pth)
}
return nil
}
// validateWhatsnewsDir validates if whatsnews_dir input value exists if provided.
func (c Configs) validateWhatsnewsDir() error {
if c.WhatsnewsDir == "" {
return nil
}
if exist, err := pathutil.IsDirExists(c.WhatsnewsDir); err != nil {
return fmt.Errorf("failed to check if what's new directory exist at: %s, error: %s", c.WhatsnewsDir, err)
} else if !exist {
return errors.New("what's new directory not exist at: " + c.WhatsnewsDir)
}
log.Infof("Using what's new data from: %v", c.WhatsnewsDir)
return nil
}
// validateMappingFile validates if mapping_file input value exists if provided.
func (c Configs) validateMappingFile() error {
if c.MappingFile == "" {
return nil
}
for _, path := range parseInputList(c.MappingFile) {
if exist, err := pathutil.IsPathExists(path); err != nil {
return fmt.Errorf("failed to check if mapping file exist at: %s, error: %s", path, err)
} else if !exist {
return errors.New("mapping file doesn't exist at: " + path)
}
log.Infof("Using mapping file from: %v", path)
}
return nil
}
func splitElements(list []string, sep string) (s []string) {
for _, e := range list {
s = append(s, strings.Split(e, sep)...)
}
return
}
func parseInputList(list string) (elements []string) {
log.Debugf("Parsing list input: '%v'", list)
list = strings.TrimSpace(list)
if len(list) == 0 {
return nil
}
s := []string{list}
for _, sep := range []string{"\n", `\n`, "|"} {
s = splitElements(s, sep)
}
for _, element := range s {
element = strings.TrimSpace(element)
if len(element) > 0 {
elements = append(elements, element)
log.Debugf("Found element: %v", element)
}
}
return
}
// appPaths returns the app to deploy, by preferring .aab files.
func (c Configs) appPaths() ([]string, []string) {
var apks, aabs, warnings []string
for _, pth := range parseInputList(c.AppPath) {
pth = strings.TrimSpace(pth)
ext := strings.ToLower(filepath.Ext(pth))
if ext == ".aab" {
aabs = append(aabs, pth)
} else if ext == ".apk" {
apks = append(apks, pth)
} else {
warnings = append(warnings, fmt.Sprintf("unknown app path extension in path: %s, supported extensions: .apk, .aab", pth))
}
}
if len(aabs) > 0 && len(apks) > 0 {
warnings = append(warnings, fmt.Sprintf("Both .aab and .apk files provided, using the .aab file(s): %s", strings.Join(aabs, ",")))
}
if len(aabs) > 0 {
return aabs, warnings
}
return apks, warnings
}
func (c Configs) mappingPaths() []string {
var mappingPaths []string
for _, path := range parseInputList(c.MappingFile) {
mappingPaths = append(mappingPaths, strings.TrimSpace(path))
}
return mappingPaths
}
// validateApps validates if files provided via app_path are existing files,
// if app_path is empty it validates if files provided via app_path input are existing .apk or .aab files.
func (c Configs) validateApps() error {
apps, warnings := c.appPaths()
for _, warn := range warnings {
log.Warnf(warn)
}
if len(apps) == 0 {
return fmt.Errorf("no app provided")
}
for _, pth := range apps {
if exist, err := pathutil.IsPathExists(pth); err != nil {
return fmt.Errorf("failed to check if app exist at: %s, error: %s", pth, err)
} else if !exist {
return errors.New("app not exist at: " + pth)
}
log.Infof("Using app from: %v", pth)
}
return nil
}
// expansionFiles gets the expansion files from the received configuration. Returns true and the entries (type and
// path) of them when any found, false or error otherwise.
func expansionFiles(appPaths []string, expansionFilePathConfig string) ([]string, error) {
// "main:/file/path/1.obb|patch:/file/path/2.obb"
var expansionFileEntries = []string{}
if strings.TrimSpace(expansionFilePathConfig) != "" {
expansionFileEntries = strings.Split(expansionFilePathConfig, "|")
if len(appPaths) != len(expansionFileEntries) {
return []string{}, fmt.Errorf("mismatching number of APKs(%d) and Expansionfiles(%d)", len(appPaths), len(expansionFileEntries))
}
log.Infof("Found %v expansion file(s) to upload.", len(expansionFileEntries))
for i, expansionFile := range expansionFileEntries {
log.Debugf("%v - %v", i+1, expansionFile)
}
}
return expansionFileEntries, nil
}