-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
148 lines (122 loc) · 4 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
143
144
145
146
147
148
package main
import (
"fmt"
"os"
"os/exec"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-steputils/tools"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/log"
)
// Define variable
var isError bool = false
// Config ...
type Config struct {
GMCloudSaaSEmail string `env:"email"`
GMCloudSaaSPassword stepconf.Secret `env:"password"`
GMCloudSaaSGmsaasVersion string `env:"gmsaas_version"`
GMCloudSaaSAPIToken stepconf.Secret `env:"api_token"`
}
// install gmsaas if not installed.
func ensureGMSAASisInstalled(version string) error {
path, err := exec.LookPath("gmsaas")
if err != nil {
log.Infof("Installing gmsaas...")
var installCmd *exec.Cmd
if version != "" {
installCmd = exec.Command("pip3", "install", "gmsaas=="+version, "--break-system-packages")
} else {
installCmd = exec.Command("pip3", "install", "gmsaas", "--break-system-packages")
}
if out, err := installCmd.CombinedOutput(); err != nil {
return fmt.Errorf("%s failed, error: %s | output: %s", installCmd.Args, err, out)
}
// Execute asdf reshim to update PATH
exec.Command("asdf", "reshim", "python").CombinedOutput()
if version != "" {
log.Infof("gmsaas %s has been installed.", version)
} else {
log.Infof("gmsaas has been installed.")
}
} else {
log.Infof("gmsaas is already installed: %s", path)
}
// Set Custom user agent to improve customer support
os.Setenv("GMSAAS_USER_AGENT_EXTRA_DATA", "bitrise.io")
return nil
}
// printError prints an error.
func printError(format string, args ...interface{}) {
log.Errorf(format, args...)
}
// abortf prints an error and terminates step
func abortf(format string, args ...interface{}) {
printError(format, args...)
os.Exit(1)
}
// setOperationFailed marked step as failed
func setOperationFailed(format string, args ...interface{}) {
printError(format, args...)
isError = true
}
func configureAndroidSDKPath() {
log.Infof("Configure Android SDK configuration")
value, exists := os.LookupEnv("ANDROID_HOME")
if exists {
cmd := command.New("gmsaas", "config", "set", "android-sdk-path", value)
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
setOperationFailed("Failed to set android-sdk-path, error: error: %s | output: %s", cmd.PrintableCommandArgs(), err, out)
return
}
log.Infof("Android SDK is configured")
} else {
setOperationFailed("Please set ANDROID_HOME environment variable")
return
}
}
func login(api_token, username, password string) {
log.Infof("Login Genymotion Account")
var cmd *exec.Cmd
if api_token != "" {
cmd = exec.Command("gmsaas", "auth", "token", api_token)
} else if username != "" && password != "" {
cmd = exec.Command("gmsaas", "auth", "login", username, password)
} else {
abortf("Invalid arguments. Must provide either a token or both email and password.")
return
}
if out, err := cmd.CombinedOutput(); err != nil {
abortf("Failed to login with gmsaas, error: error: %s | output: %s", cmd.Args, err, out)
return
}
log.Infof("Logged to Genymotion Cloud SaaS platform")
}
func main() {
var c Config
if err := stepconf.Parse(&c); err != nil {
abortf("Issue with input: %s", err)
}
stepconf.Print(c)
if err := ensureGMSAASisInstalled(c.GMCloudSaaSGmsaasVersion); err != nil {
abortf("%s", err)
}
configureAndroidSDKPath()
if err := tools.ExportEnvironmentWithEnvman("GMSAAS_USER_AGENT_EXTRA_DATA", "bitrise.io"); err != nil {
printError("Failed to export %s, error: %v", "GMSAAS_USER_AGENT_EXTRA_DATA", err)
}
if c.GMCloudSaaSAPIToken != "" {
login(string(c.GMCloudSaaSAPIToken), "", "")
} else {
login("", c.GMCloudSaaSEmail, string(c.GMCloudSaaSPassword))
}
// --- Exit codes:
// The exit code of your Step is very important. If you return
// with a 0 exit code `bitrise` will register your Step as "successful".
// Any non zero exit code will be registered as "failed" by `bitrise`.
if isError {
// If at least one error happens, step will fail
os.Exit(1)
}
os.Exit(0)
}