-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
163 lines (137 loc) · 5 KB
/
run.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
/*
MIT License
Copyright (c) 2021 Rally Health, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package cmd
import (
"fmt"
"strconv"
"strings"
tm "github.com/buger/goterm"
"github.com/AlecAivazis/survey/v2"
"github.com/jedib0t/go-pretty/table"
. "github.com/logrusorgru/aurora/v3"
"github.com/rallyhealth/goose/pkg"
"github.com/spf13/cobra"
)
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
Aliases: []string{"honk"},
Args: cobra.ArbitraryArgs,
Short: "Run a jenkins job",
Long: `Run a jenkins job based on the current git repo and branch.
Otherwise, you can specify a job to be run.
For example, Example/kireledan//long-branch-name specifies the Job , 'Example' , given the branch kireledan/long-branch-name
Note that '/' in git branches are replaced with '//'`,
Run: func(cmd *cobra.Command, args []string) {
thejob, jobPath := pkg.AutoLocateJob(args, cmd.Flag("branch").Value.String(), Jenky)
params := pkg.GetJobParameters(Jenky, jobPath)
choices := map[string]string{}
interactive, _ := strconv.ParseBool(cmd.Flag("interactive").Value.String())
if !interactive {
jobArgs := map[string]*string{}
for _, param := range params {
tester := cmd.LocalFlags().String(param.Name, fmt.Sprintf("%v", param.DefaultParameterValue.Value), param.Description)
jobArgs[param.Name] = tester
}
err := cmd.LocalFlags().Parse(args)
if err != nil {
cmd.LocalFlags().Args()
panic(err)
}
for arg, argstuff := range jobArgs {
fmt.Println(arg, *argstuff)
choices[arg] = *argstuff
}
} else {
// Pick params
fmt.Println("Please pick your parameters for", thejob.Raw.FullDisplayName)
for _, param := range params {
if len(param.Choices) > 0 {
result := ""
prompt := &survey.Select{
Message: fmt.Sprintf("%s: ", param.Name),
Help: param.Description,
Options: param.Choices,
Default: param.DefaultParameterValue.Value,
}
survey.AskOne(prompt, &result)
choices[param.Name] = result
} else {
result := ""
prompt := &survey.Input{
Message: fmt.Sprintf("%s: ", param.Name),
Help: param.Description,
Default: fmt.Sprintf("%v", param.DefaultParameterValue.Value),
}
survey.AskOne(prompt, &result)
choices[param.Name] = result
}
}
tm.Clear()
tm.Flush()
paramTable := table.NewWriter()
//paramTable.SetOutputMirror(os.Stdout)
paramTable.AppendHeader(table.Row{"PARAMETER", "CHOICE"})
for name, choice := range choices {
if choice == "" {
choice = "None"
}
paramTable.AppendRow(table.Row{name, choice})
}
output := paramTable.Render()
shortcut := printShortCut(jobPath, choices)
fmt.Println(shortcut)
fmt.Println("----")
// Add some content to the box
// Note that you can add ANY content, even tables
job := strings.Split(thejob.Raw.FullName, "/")[:len(strings.Split(thejob.Raw.FullName, "/"))-1]
branch := strings.Split(thejob.Raw.FullName, "/")[len(strings.Split(thejob.Raw.FullName, "/"))-1]
fmt.Print("Job: ", Cyan(job), "\n")
fmt.Print("Branch: ", Cyan(branch), "\n")
fmt.Print(output)
fmt.Print("\nPlease confirm these parameters by pressing ", Yellow("[Enter]"), "\n")
// Move Box to approx center of the screen
fmt.Scanln()
}
pkg.InvokeJob(Jenky, thejob, choices)
},
}
func printShortCut(job string, choices map[string]string) string {
builderString := fmt.Sprintf("Next time, type this to run this build without a prompt!\n")
builderString += fmt.Sprintf("%s %s run %s --interactive=False -- ", Yellow("$"), Green("goose"), Cyan(job))
for arg, value := range choices {
if value != "" {
builderString += fmt.Sprintf("--%s='%s' ", arg, value)
}
}
return builderString
}
func init() {
rootCmd.AddCommand(runCmd)
runCmd.Flags().BoolP("interactive", "i", true, "Help message for toggle")
runCmd.Flags().StringP("branch", "b", "", "Help message for toggle")
}
func getJenkinsJob(args []string) []string {
if len(args) == 0 {
return pkg.FindCurrentRepoJobs()
} else {
return []string{args[0]}
}
}