forked from csghuser/go-gocd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipelineconfig.go
188 lines (165 loc) · 6.01 KB
/
pipelineconfig.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
package cli
import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"github.com/drewsonne/go-gocd/gocd"
"github.com/urfave/cli"
)
// List of command name and descriptions
const (
CreatePipelineConfigCommandName = "create-pipeline-config"
CreatePipelineConfigCommandUsage = "Create Pipeline config"
UpdatePipelineConfigCommandName = "update-pipeline-config"
UpdatePipelineConfigCommandUsage = "Update Pipeline config"
DeletePipelineConfigCommandName = "delete-pipeline-config"
DeletePipelineConfigCommandUsage = "Remove Pipeline. This will not delete the pipeline history, which will be stored in the database"
GetPipelineConfigCommandName = "get-pipeline-config"
GetPipelineConfigCommandUsage = "Get a Pipeline Configuration"
)
// CreatePipelineConfigAction handles the interaction between the cli flags and the action handler for
// create-pipeline-config-action
func createPipelineConfigAction(client *gocd.Client, c *cli.Context) (r interface{}, resp *gocd.APIResponse, err error) {
group := c.String("group")
if group == "" {
return nil, nil, NewFlagError("group")
}
pipeline := c.String("pipeline-json")
pipelineFile := c.String("pipeline-file")
if pipeline == "" && pipelineFile == "" {
return nil, nil, errors.New("One of '--pipeline-file' or '--pipeline-json' must be specified")
}
if pipeline != "" && pipelineFile != "" {
return nil, nil, errors.New("Only one of '--pipeline-file' or '--pipeline-json' can be specified")
}
var pf []byte
if pipelineFile != "" {
pf, err = ioutil.ReadFile(pipelineFile)
if err != nil {
return nil, nil, err
}
} else {
pf = []byte(pipeline)
}
p := &gocd.Pipeline{}
err = json.Unmarshal(pf, &p)
if err != nil {
return nil, nil, err
}
return client.PipelineConfigs.Create(context.Background(), group, p)
}
// UpdatePipelineConfigAction handles the interaction between the cli flags and the action handler for
// update-pipeline-config-action
func updatePipelineConfigAction(client *gocd.Client, c *cli.Context) (r interface{}, resp *gocd.APIResponse, err error) {
var name, version string
if name = c.String("name"); name == "" {
return nil, nil, NewFlagError("name")
}
if version = c.String("pipeline-version"); version == "" {
return nil, nil, NewFlagError("pipeline-version")
}
pipeline := c.String("pipeline")
pipelineFile := c.String("pipeline-file")
if pipeline == "" && pipelineFile == "" {
return nil, nil, errors.New("One of '--pipeline-file' or '--pipeline' must be specified")
}
if pipeline != "" && pipelineFile != "" {
return nil, nil, errors.New("Only one of '--pipeline-file' or '--pipeline' can be specified")
}
var pf []byte
if pipelineFile != "" {
pf, err = ioutil.ReadFile(pipelineFile)
if err != nil {
return nil, nil, err
}
} else {
pf = []byte(pipeline)
}
p := &gocd.Pipeline{
Version: version,
}
err = json.Unmarshal(pf, &p)
if err != nil {
return nil, nil, err
}
return client.PipelineConfigs.Update(context.Background(), name, p)
}
// DeletePipelineConfigAction handles the interaction between the cli flags and the action handler for
// delete-pipeline-config-action
func deletePipelineConfigAction(client *gocd.Client, c *cli.Context) (r interface{}, resp *gocd.APIResponse, err error) {
name := c.String("name")
if name == "" {
return nil, nil, NewFlagError("name")
}
deleteResponse, resp, err := client.PipelineConfigs.Delete(context.Background(), name)
if resp.HTTP.StatusCode == 406 {
err = errors.New(deleteResponse)
}
return deleteResponse, resp, err
}
// GetPipelineConfigAction handles the interaction between the cli flags and the action handler for get-pipeline-config
func getPipelineConfigAction(client *gocd.Client, c *cli.Context) (r interface{}, resp *gocd.APIResponse, err error) {
name := c.String("name")
if name == "" {
return nil, nil, NewFlagError("name")
}
getResponse, resp, err := client.PipelineConfigs.Get(context.Background(), name)
if resp.HTTP.StatusCode != 404 {
getResponse.RemoveLinks()
}
return getResponse, resp, err
}
// CreatePipelineConfigCommand handles the interaction between the cli flags and the action handler for create-pipeline-config
func createPipelineConfigCommand() *cli.Command {
return &cli.Command{
Name: CreatePipelineConfigCommandName,
Usage: CreatePipelineConfigCommandUsage,
Action: ActionWrapper(createPipelineConfigAction),
Category: "Pipeline Configs",
Flags: []cli.Flag{
cli.StringFlag{Name: "group"},
cli.StringFlag{Name: "pipeline-json", Usage: "A JSON string describing the pipeline configuration"},
cli.StringFlag{Name: "pipeline-file", Usage: "Path to a JSON file describing the pipeline configuration"},
},
}
}
// UpdatePipelineConfigCommand handles the interaction between the cli flags and the action handler for update-pipeline-config
func updatePipelineConfigCommand() *cli.Command {
return &cli.Command{
Name: UpdatePipelineConfigCommandName,
Usage: UpdatePipelineConfigCommandUsage,
Action: ActionWrapper(updatePipelineConfigAction),
Category: "Pipeline Configs",
Flags: []cli.Flag{
cli.StringFlag{Name: "name"},
cli.StringFlag{Name: "pipeline-version"},
cli.StringFlag{Name: "pipeline"},
cli.StringFlag{Name: "pipeline-file"},
},
}
}
// DeletePipelineConfigCommand handles the interaction between the cli flags and the action handler for delete-pipeline-config
func deletePipelineConfigCommand() *cli.Command {
return &cli.Command{
Name: DeletePipelineConfigCommandName,
Usage: DeletePipelineConfigCommandUsage,
Category: "Pipeline Configs",
Action: ActionWrapper(deletePipelineConfigAction),
Flags: []cli.Flag{
cli.StringFlag{Name: "name"},
},
}
}
// GetPipelineConfigCommand handles the interaction between the cli flags and the action handler for get-pipeline-config
func getPipelineConfigCommand() *cli.Command {
return &cli.Command{
Name: GetPipelineConfigCommandName,
Usage: GetPipelineConfigCommandUsage,
Action: ActionWrapper(getPipelineConfigAction),
Category: "Pipeline Configs",
Flags: []cli.Flag{
cli.StringFlag{Name: "name"},
},
}
}