forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_command.go
121 lines (98 loc) · 3.16 KB
/
api_command.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
package v2
import (
"fmt"
"strings"
"code.cloudfoundry.org/cli/actors/configactions"
"code.cloudfoundry.org/cli/api/cloudcontrollerv2"
"code.cloudfoundry.org/cli/commands"
"code.cloudfoundry.org/cli/commands/flags"
"code.cloudfoundry.org/cli/commands/ui"
)
//go:generate counterfeiter . APIConfigActor
type APIConfigActor interface {
ClearTarget()
SetTarget(CCAPI string, skipSSLValidation bool) (configactions.Warnings, error)
}
type ApiCommand struct {
OptionalArgs flags.APITarget `positional-args:"yes"`
SkipSSLValidation bool `long:"skip-ssl-validation" description:"Skip verification of the API endpoint. Not recommended!"`
Unset bool `long:"unset" description:"Remove all api endpoint targeting"`
usage interface{} `usage:"CF_NAME api [URL]"`
relatedCommands interface{} `related_commands:"auth, login, target"`
UI commands.UI
Actor APIConfigActor
Config commands.Config
}
func (cmd *ApiCommand) Setup(config commands.Config, ui commands.UI) error {
cmd.Actor = configactions.NewActor(config, cloudcontrollerv2.NewCloudControllerClient())
cmd.UI = ui
cmd.Config = config
return nil
}
func (cmd *ApiCommand) Execute(args []string) error {
if cmd.Unset {
return cmd.ClearTarget()
}
if cmd.OptionalArgs.URL != "" {
err := cmd.SetAPI()
if err != nil {
return err
}
}
if cmd.Config.Target() == "" {
cmd.UI.DisplayText("No api endpoint set. Use '{{.Name}}' to set an endpoint", map[string]interface{}{
"Name": "cf api",
})
return nil
}
return DisplayCurrentTargetInformation(cmd.Config, cmd.UI)
}
func (cmd *ApiCommand) ClearTarget() error {
cmd.UI.DisplayHeaderFlavorText("Unsetting api endpoint...")
cmd.Actor.ClearTarget()
cmd.UI.DisplayOK()
return nil
}
func DisplayCurrentTargetInformation(config commands.Config, commandUI commands.UI) error {
user, err := config.CurrentUser()
if err != nil {
return err
}
commandUI.DisplayPair("API endpoint", config.Target())
commandUI.DisplayPair("API version", config.APIVersion())
commandUI.DisplayPair("User", user.Name)
commandUI.DisplayPair("Org", config.TargetedOrganization().Name)
commandUI.DisplayPair("Space", config.TargetedSpace().Name)
return nil
}
func (cmd *ApiCommand) SetAPI() error {
cmd.UI.DisplayHeaderFlavorText("Setting api endpoint to {{.Endpoint}}...", map[string]interface{}{
"Endpoint": cmd.OptionalArgs.URL,
})
api := cmd.processURL(cmd.OptionalArgs.URL)
_, err := cmd.Actor.SetTarget(api, cmd.SkipSSLValidation)
if err != nil {
return cmd.handleError(err)
}
if strings.HasPrefix(api, "http:") {
cmd.UI.DisplayText("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended")
}
cmd.UI.DisplayOK()
cmd.UI.DisplayNewline()
return nil
}
func (_ ApiCommand) processURL(apiURL string) string {
if !strings.HasPrefix(apiURL, "http") {
return fmt.Sprintf("https://%s", apiURL)
}
return apiURL
}
func (cmd ApiCommand) handleError(err error) error {
switch e := err.(type) {
case cloudcontrollerv2.UnverifiedServerError:
return ui.InvalidSSLCertError{API: cmd.OptionalArgs.URL}
case cloudcontrollerv2.RequestError:
return ui.APIRequestError{Err: e}
}
return err
}