forked from bitrise-steplib/steps-project-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (171 loc) · 5.38 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
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
package main
import (
"errors"
"fmt"
"os"
"path"
"runtime"
"strings"
"github.com/bitrise-io/bitrise-init/scanner"
"github.com/bitrise-io/go-steputils/step"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-steplib/steps-activate-ssh-key/activatesshkey"
"github.com/bitrise-steplib/steps-git-clone/gitclone"
)
type config struct {
ScanDirectory string `env:"scan_dir,dir"`
ResultSubmitURL string `env:"scan_result_submit_url"`
ResultSubmitAPIToken stepconf.Secret `env:"scan_result_submit_api_token"`
IconCandidatesURL string `env:"icon_candidates_url"`
DebugLog bool `env:"verbose_log,opt[false,true]"`
// Enable activate SSH key and git clone
EnableRepoClone bool `env:"enable_repo_clone"`
// Activate SSH Key step
SSHRsaPrivateKey stepconf.Secret `env:"ssh_rsa_private_key"`
// Git clone step
RepositoryURL string `env:"repository_url"`
Branch string `env:"branch"`
}
func failf(format string, args ...interface{}) {
log.TErrorf(format, args...)
os.Exit(1)
}
func printDirTree() {
cmd := command.New("which", "tree")
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil || out == "" {
log.TErrorf("tree not installed, can not list files")
} else {
fmt.Println()
cmd := command.NewWithStandardOuts("tree", ".", "-L", "3")
log.TPrintf("$ %s", cmd.PrintableCommandArgs())
if err := cmd.Run(); err != nil {
log.TErrorf("Failed to list files in current directory, error: %s", err)
}
}
}
type repoConfig struct {
CloneIntoDir string
RepositoryURL string
SSHRsaPrivateKey stepconf.Secret
Branch string
}
func cloneRepo(cfg repoConfig) error {
cfg.RepositoryURL = strings.TrimSpace(cfg.RepositoryURL)
cfg.Branch = strings.TrimSpace(cfg.Branch)
if cfg.RepositoryURL == "" {
return newStepError(
"input_parse_failed",
errors.New("repository URL input missing"),
"Repository URL unspecified",
)
}
if cfg.Branch == "" {
return newStepError(
"input_parse_failed",
errors.New("repository bracnh input missing"),
"Repository branch unspecified",
)
}
// Activate SSH key is optional
if cfg.SSHRsaPrivateKey != "" {
if err := activatesshkey.Execute(activatesshkey.Config{
SSHRsaPrivateKey: cfg.SSHRsaPrivateKey,
SSHKeySavePath: path.Join(pathutil.UserHomeDir(), ".ssh", "steplib_ssh_step_id_rsa"),
IsRemoveOtherIdentities: false,
}); err != nil {
return err
}
}
// Git clone
if err := gitclone.Execute(gitclone.Config{
RepositoryURL: cfg.RepositoryURL,
CloneIntoDir: cfg.CloneIntoDir, // Using same directory later to run scan
Branch: cfg.Branch,
// BuildURL and BuildAPIToken used for merging only
BuildURL: "",
BuildAPIToken: "",
UpdateSubmodules: true,
ManualMerge: true,
}); err != nil {
return err
}
return nil
}
func main() {
var cfg config
if err := stepconf.Parse(&cfg); err != nil {
failf("Invalid configuration: %s", err)
}
stepconf.Print(cfg)
log.SetEnableDebugLog(cfg.DebugLog)
var resultClient *resultClient
if strings.TrimSpace(cfg.ResultSubmitURL) != "" {
if strings.TrimSpace(string(cfg.ResultSubmitAPIToken)) == "" {
log.TWarnf("Build trigger token is empty.")
}
var err error
if resultClient, err = newResultClient(cfg.ResultSubmitURL, cfg.ResultSubmitAPIToken); err != nil {
failf(fmt.Sprintf("%v", err))
}
}
if !(runtime.GOOS == "darwin" || runtime.GOOS == "linux") {
failf("Unsupported OS: %s", runtime.GOOS)
}
if cfg.EnableRepoClone {
handleStepError := func(stepID, tag string, err error, shortMsg string) {
LogError(stepID, tag, err, shortMsg)
if resultClient != nil {
if err := resultClient.uploadErrorResult(stepID, err); err != nil {
log.TWarnf("Failed to submit result: %s", err)
}
}
}
if err := cloneRepo(repoConfig{
CloneIntoDir: cfg.ScanDirectory,
RepositoryURL: cfg.RepositoryURL,
SSHRsaPrivateKey: cfg.SSHRsaPrivateKey,
Branch: cfg.Branch,
}); err != nil {
if stepError, ok := err.(*step.Error); ok {
handleStepError(stepError.StepID, stepError.Tag, stepError, stepError.ShortMsg)
} else {
wrappedStepError := newStepError("error_cast_failed", err, "Failed to cast error")
handleStepError(wrappedStepError.StepID, wrappedStepError.Tag, wrappedStepError.Err, wrappedStepError.ShortMsg)
}
failf("%v", err)
}
}
searchDir, err := pathutil.AbsPath(cfg.ScanDirectory)
if err != nil {
failf("failed to expand path (%s), error: %s", cfg.ScanDirectory, err)
}
isPrivateRepo := cfg.SSHRsaPrivateKey != ""
result, platformsDetected := scanner.GenerateScanResult(searchDir, isPrivateRepo)
// Upload results
if resultClient != nil {
log.TInfof("Submitting results...")
if err := resultClient.uploadResults(result); err != nil {
failf("Could not send back results: %s", err)
}
log.TDonef("Submitted.")
}
// Upload icons
if strings.TrimSpace(cfg.IconCandidatesURL) != "" {
if err := uploadIcons(result.Icons,
iconCandidateQuery{
URL: cfg.IconCandidatesURL,
buildTriggerToken: string(cfg.ResultSubmitAPIToken),
}); err != nil {
log.TWarnf("Failed to submit icons, error: %s", err)
}
}
if !platformsDetected {
printDirTree()
failf("No known platform detected")
}
log.TDonef("Scan finished.")
}