/
execution.go
129 lines (114 loc) · 4.64 KB
/
execution.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
/*
Package cmd Copyright 2021 VMware, Inc.
SPDX-License-Identifier: BSD-2-Clause
*/
package cmd
import (
"fmt"
"os"
"github.com/sammcgeown/vra-cli/pkg/cmd/codestream"
"github.com/sammcgeown/vra-cli/pkg/util/helpers"
log "github.com/sirupsen/logrus"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
var nested, rollback bool
var inputs, comments string
// getExecutionCmd represents the executions command
var getExecutionCmd = &cobra.Command{
Use: "execution",
Short: "Get Pipeline Executions",
Long: `Get Code Stream Pipeline Executions by ID, Name, Project and Status
# Get only failed executions:
vra-cli get execution --status FAILED
# Get an execution by ID:
vra-cli get execution --id bb3f6aff-311a-45fe-8081-5845a529068d
# Get Failed executions in Project "Field Demo" with the name "Learn Code Stream"
vra-cli get execution --status FAILED --project "Field Demo" --name "Learn Code Stream"`,
Run: func(cmd *cobra.Command, args []string) {
response, err := codestream.GetExecution(APIClient, id, projectName, status, name, nested, rollback)
if err != nil {
log.Errorln("Unable to get executions: ", err)
}
var resultCount = len(response)
if resultCount == 0 {
// No results
log.Warnln("No results found")
return
}
if APIClient.Output == "json" {
helpers.PrettyPrint(response)
} else {
// Print result table
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Id", "Execution", "Project", "Status", "Message"})
for _, c := range response {
table.Append([]string{c.ID, c.Name + "#" + fmt.Sprint(c.Index), c.Project, c.Status, c.StatusMessage})
}
table.Render()
}
},
}
// delExecutionCmd represents the executions command
var delExecutionCmd = &cobra.Command{
Use: "execution",
Short: "Delete an Execution",
Long: `Delete an Execution with a specific Execution ID
`,
Run: func(cmd *cobra.Command, args []string) {
if id != "" {
_, err := codestream.DeleteExecution(APIClient, id)
if err != nil {
log.Errorln("Unable to delete execution:", err)
} else {
log.Infoln("Execution with id", id, "deleted")
}
} else {
response, err := codestream.DeleteExecutions(APIClient, projectName, status, name, nested, rollback)
if err != nil {
log.Errorln("Unable to delete executions:", err)
} else {
log.Infoln(len(response), "Executions deleted")
}
}
},
}
// createExecutionCmd represents the executions command
var createExecutionCmd = &cobra.Command{
Use: "execution",
Short: "Create an Execution",
Long: `Create an Execution with a specific pipeline ID and form payload.
`,
Run: func(cmd *cobra.Command, args []string) {
response, err := codestream.CreateExecution(APIClient, id, inputs, comments)
if err != nil {
log.Errorln("Unable to create execution:", err)
}
log.Infoln("Execution", response.ExecutionID, "created")
},
}
func init() {
// Get
getCmd.AddCommand(getExecutionCmd)
getExecutionCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the pipeline to list executions for")
getExecutionCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the executions to list")
getExecutionCmd.Flags().StringVarP(&status, "status", "s", "", "Filter executions by status (Completed|Waiting|Pausing|Paused|Resuming|Running)")
getExecutionCmd.Flags().StringVarP(&projectName, "project", "p", "", "Filter executions by Project")
getExecutionCmd.Flags().BoolVarP(&nested, "nested", "", false, "Include nested executions")
getExecutionCmd.Flags().BoolVarP(&rollback, "rollback", "", false, "Include rollback executions")
// Delete
deleteCmd.AddCommand(delExecutionCmd)
delExecutionCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the pipeline to delete executions for")
delExecutionCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the execution to delete")
delExecutionCmd.Flags().StringVarP(&status, "status", "s", "", "Delete executions by status (Completed|Waiting|Pausing|Paused|Resuming|Running)")
delExecutionCmd.Flags().StringVarP(&projectName, "project", "p", "", "Delete executions by Project")
delExecutionCmd.Flags().BoolVarP(&nested, "nested", "", false, "Delete nested executions")
delExecutionCmd.Flags().BoolVarP(&rollback, "rollback", "", false, "Delete rollback executions")
// Create
createCmd.AddCommand(createExecutionCmd)
createExecutionCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the pipeline to execute")
createExecutionCmd.Flags().StringVarP(&inputs, "inputs", "", "", "JSON form inputs")
createExecutionCmd.Flags().StringVarP(&importPath, "importPath", "", "", "JSON input file")
createExecutionCmd.Flags().StringVarP(&comments, "comments", "", "", "Execution comments")
createExecutionCmd.MarkFlagRequired("id")
}