forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_command.go
116 lines (94 loc) · 2.87 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
package v6
import (
"fmt"
"strings"
"code.cloudfoundry.org/cli/actor/v2action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/v6/shared"
)
//go:generate counterfeiter . APIActor
type APIActor interface {
ClearTarget()
SetTarget(settings v2action.TargetSettings) (v2action.Warnings, error)
}
type APICommand struct {
OptionalArgs flag.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 command.UI
Actor APIActor
Config command.Config
}
func (cmd *APICommand) Setup(config command.Config, ui command.UI) error {
ccClient, _ := shared.NewWrappedCloudControllerClient(config, ui)
cmd.Actor = v2action.NewActor(ccClient, nil, config)
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
}
if cmd.Config.APIVersion() != "" {
err := command.WarnIfAPIVersionBelowSupportedMinimum(cmd.Config.APIVersion(), cmd.UI)
if err != nil {
return err
}
}
cmd.UI.DisplayKeyValueTable("", [][]string{
{cmd.UI.TranslateText("api endpoint:"), cmd.Config.Target()},
{cmd.UI.TranslateText("api version:"), cmd.Config.APIVersion()},
}, 3)
user, err := cmd.Config.CurrentUser()
if user.Name == "" {
command.DisplayNotLoggedInText(cmd.Config.BinaryName(), cmd.UI)
}
return err
}
func (cmd *APICommand) ClearTarget() error {
cmd.UI.DisplayTextWithFlavor("Unsetting api endpoint...")
cmd.Actor.ClearTarget()
cmd.UI.DisplayOK()
return nil
}
func (cmd *APICommand) setAPI() error {
cmd.UI.DisplayTextWithFlavor("Setting api endpoint to {{.Endpoint}}...", map[string]interface{}{
"Endpoint": cmd.OptionalArgs.URL,
})
apiURL := processURL(cmd.OptionalArgs.URL)
_, err := cmd.Actor.SetTarget(v2action.TargetSettings{
URL: apiURL,
SkipSSLValidation: cmd.SkipSSLValidation,
DialTimeout: cmd.Config.DialTimeout(),
})
if err != nil {
return err
}
if strings.HasPrefix(apiURL, "http:") {
cmd.UI.DisplayText("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended")
}
cmd.UI.DisplayOK()
return nil
}
func processURL(apiURL string) string {
if !strings.HasPrefix(apiURL, "http") {
return fmt.Sprintf("https://%s", apiURL)
}
return apiURL
}