-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_execute.go
169 lines (145 loc) · 4.43 KB
/
pipeline_execute.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
package cmd
import (
"encoding/json"
"fmt"
"github.com/dory-engine/dorycli/pkg"
"github.com/spf13/cobra"
"net/http"
"net/url"
"strings"
)
type OptionsPipelineExecute struct {
*OptionsCommon `yaml:"optionsCommon" json:"optionsCommon" bson:"optionsCommon" validate:""`
Batch string `yaml:"batch" json:"batch" bson:"batch" validate:""`
Params []string `yaml:"params" json:"params" bson:"params" validate:""`
Param struct {
PipelineName string `yaml:"pipelineName" json:"pipelineName" bson:"pipelineName" validate:""`
Batches []string `yaml:"batches" json:"batches" bson:"batches" validate:""`
QueryParam string `yaml:"queryParam" json:"queryParam" bson:"queryParam" validate:""`
}
}
func NewOptionsPipelineExecute() *OptionsPipelineExecute {
var o OptionsPipelineExecute
o.OptionsCommon = OptCommon
return &o
}
func NewCmdPipelineExecute() *cobra.Command {
o := NewOptionsPipelineExecute()
baseName := pkg.GetCmdBaseName()
msgUse := fmt.Sprintf("execute [pipelineName]")
_ = OptCommon.GetOptionsCommon()
msgShort := OptCommon.TransLang("cmd_pipeline_execute_short")
msgLong := OptCommon.TransLang("cmd_pipeline_execute_long")
msgExample := pkg.Indent(OptCommon.TransLang("cmd_pipeline_execute_example", baseName, baseName, baseName))
cmd := &cobra.Command{
Use: msgUse,
DisableFlagsInUseLine: true,
Short: msgShort,
Long: msgLong,
Example: msgExample,
Run: func(cmd *cobra.Command, args []string) {
CheckError(o.Validate(args))
CheckError(o.Run(args))
},
}
cmd.Flags().StringVarP(&o.Batch, "batch", "b", "", OptCommon.TransLang("param_pipeline_execute_batch"))
cmd.Flags().StringSliceVar(&o.Params, "params", []string{}, OptCommon.TransLang("param_pipeline_execute_params"))
CheckError(o.Complete(cmd))
return cmd
}
func (o *OptionsPipelineExecute) Complete(cmd *cobra.Command) error {
var err error
err = o.GetOptionsCommon()
if err != nil {
return err
}
cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
pipelineNames, err := o.GetPipelineNames()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return pipelineNames, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
return err
}
func (o *OptionsPipelineExecute) Validate(args []string) error {
var err error
err = o.GetOptionsCommon()
if err != nil {
return err
}
if len(args) != 1 {
err = fmt.Errorf("args error: only accept one args")
return err
}
s := args[0]
s = strings.Trim(s, " ")
err = pkg.ValidatePipelineName(s)
if err != nil {
err = fmt.Errorf("pipelineName error: %s", err.Error())
return err
}
o.Param.PipelineName = s
o.Batch = strings.Trim(o.Batch, " ")
arr := strings.Split(o.Batch, "::")
for _, val := range arr {
val = strings.Trim(val, " ")
if val != "" {
o.Param.Batches = append(o.Param.Batches, val)
}
}
params := []string{}
for _, param := range o.Params {
ss := strings.Split(param, "=")
if len(ss) < 2 {
err = fmt.Errorf("param %s format error: example: varName=varValue", param)
return err
}
k := url.QueryEscape(ss[0])
v := url.QueryEscape(strings.Join(ss[1:], "="))
params = append(params, fmt.Sprintf("%s=%s", k, v))
}
o.Param.QueryParam = strings.Join(params, "&")
return err
}
func (o *OptionsPipelineExecute) Run(args []string) error {
var err error
bs, _ := pkg.YamlIndent(o)
log.Debug(fmt.Sprintf("command options:\n%s", string(bs)))
param := map[string]interface{}{
"batches": o.Param.Batches,
}
urlQuery := fmt.Sprintf("api/cicd/pipeline/%s?%s", o.Param.PipelineName, o.Param.QueryParam)
result, _, err := o.QueryAPI(urlQuery, http.MethodPost, "", param, false)
if err != nil {
return err
}
runName := result.Get("data.runName").String()
if runName == "" {
err = fmt.Errorf("runName is empty")
return err
}
urlQuery = fmt.Sprintf("api/cicd/run/%s", runName)
result, _, err = o.QueryAPI(urlQuery, http.MethodGet, "", param, false)
if err != nil {
return err
}
run := pkg.Run{}
err = json.Unmarshal([]byte(result.Get("data.run").Raw), &run)
if err != nil {
return err
}
if run.RunName == "" {
err = fmt.Errorf("runName %s not exists", runName)
return err
}
urlQuery = fmt.Sprintf("api/ws/log/run/%s", runName)
err = o.QueryWebsocket(urlQuery, runName)
if err != nil {
return err
}
return err
}