forked from csghuser/go-gocd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
120 lines (104 loc) · 3.12 KB
/
cli.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
package cli
import (
"encoding/json"
"fmt"
"github.com/drewsonne/go-gocd/gocd"
"github.com/urfave/cli"
)
// GetCliCommands returns a list of all CLI Command structs
func GetCliCommands() []cli.Command {
return []cli.Command{
*configureCommand(),
*listAgentsCommand(),
*listPipelineTemplatesCommand(),
*getAgentCommand(),
*getPipelineTemplateCommand(),
*createPipelineTemplateCommand(),
*updateAgentCommand(),
*updateAgentsCommand(),
*updatePipelineConfigCommand(),
*updatePipelineTemplateCommand(),
*deleteAgentCommand(),
*deleteAgentsCommand(),
*deletePipelineTemplateCommand(),
*deletePipelineConfigCommand(),
*listPipelineGroupsCommand(),
*getPipelineHistoryCommand(),
*getPipelineCommand(),
*createPipelineConfigCommand(),
*generateJSONSchemaCommand(),
*getPipelineStatusCommand(),
*pausePipelineCommand(),
*unpausePipelineCommand(),
*releasePipelineLockCommand(),
*getConfigurationCommand(),
*encryptCommand(),
*getVersionCommand(),
*listPluginsCommand(),
*getPluginCommand(),
*listScheduledJobsCommand(),
*getPipelineConfigCommand(),
*listEnvironmentsCommand(),
*getEnvironmentCommand(),
*addPipelinesToEnvironmentCommand(),
*removePipelinesFromEnvironmentCommand(),
*listPropertiesCommand(),
*createPropertyCommand(),
}
}
// NewCliClient creates a new gocd client for use by cli actions.
func NewCliClient(c *cli.Context) (cl *gocd.Client, err error) {
var profile string
if profile = c.String("profile"); profile == "" {
profile = "default"
}
cfg := &gocd.Configuration{}
err = gocd.LoadConfigByName(profile, cfg)
setStringFromContext(&cfg.Server, "server", c)
if cfg.Server == "" {
if err == nil {
// If we didn't have any errors, and our server is empty, use the local.
cfg.Server = "https://127.0.0.1:8154/go/"
} else {
return
}
}
setStringFromContext(&cfg.Username, "username", c)
setStringFromContext(&cfg.Password, "password", c)
cfg.SkipSslCheck = cfg.SkipSslCheck || c.Bool("skip_ssl_check")
return cfg.Client(), nil
}
func setStringFromContext(dest *string, key string, c *cli.Context) {
var value string
if value = c.String(key); value != "" {
*dest = value
}
}
func handleOutput(r interface{}, reqType string) cli.ExitCoder {
o := map[string]interface{}{
fmt.Sprintf("%s-response", reqType): r,
}
b, err := json.MarshalIndent(o, "", " ")
if err != nil {
return NewCliError(reqType, nil, err)
}
fmt.Println(string(b))
return nil
}
// ActionWrapperFunc describes the callback provided to ActionWrapper
type ActionWrapperFunc func(client *gocd.Client, c *cli.Context) (interface{}, *gocd.APIResponse, error)
// ActionWrapper handles the deferencing, and casting of the client object, and error handling.
func ActionWrapper(callback ActionWrapperFunc) interface{} {
return func(c *cli.Context) error {
cl := c.App.Metadata["c"].(func(c *cli.Context) (*gocd.Client, error))
client, err := cl(c.Parent())
if err != nil {
return NewCliError(c.Command.Name, nil, err)
}
v, resp, err := callback(client, c)
if err != nil {
return NewCliError(c.Command.Name, resp, err)
}
return handleOutput(v, c.Command.Name)
}
}