forked from cloudfoundry/cli
/
api_command.go
107 lines (86 loc) · 2.74 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
package v7
import (
"fmt"
"strings"
"code.cloudfoundry.org/clock"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/v7/shared"
)
type APICommand struct {
BaseCommand
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"`
}
func (cmd *APICommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
ccClient, _ := shared.NewWrappedCloudControllerClient(config, ui)
cmd.Actor = v7action.NewActor(ccClient, config, nil, nil, nil, clock.NewClock())
return nil
}
func (cmd *APICommand) Execute(args []string) error {
if cmd.Unset {
return cmd.clearTarget()
}
if cmd.OptionalArgs.URL != "" {
return cmd.setAPI()
}
return cmd.viewTarget()
}
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 := cmd.processURL(cmd.OptionalArgs.URL)
_, err := cmd.Actor.SetTarget(v7action.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 cmd.displayTarget()
}
func (cmd *APICommand) processURL(apiURL string) string {
if !strings.HasPrefix(apiURL, "http") {
return fmt.Sprintf("https://%s", apiURL)
}
return apiURL
}
func (cmd *APICommand) viewTarget() error {
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 cmd.displayTarget()
}
func (cmd *APICommand) displayTarget() error {
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 == "" {
cmd.UI.DisplayNewline()
command.DisplayNotLoggedInText(cmd.Config.BinaryName(), cmd.UI)
}
return err
}