forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
97 lines (82 loc) · 3.03 KB
/
api.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
package commands
import (
"fmt"
"strings"
"github.com/cloudfoundry/cli/cf"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_metadata"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/codegangsta/cli"
)
type Api struct {
ui terminal.UI
endpointRepo api.EndpointRepository
config core_config.ReadWriter
}
func NewApi(ui terminal.UI, config core_config.ReadWriter, endpointRepo api.EndpointRepository) (cmd Api) {
cmd.ui = ui
cmd.config = config
cmd.endpointRepo = endpointRepo
return
}
func (cmd Api) Metadata() command_metadata.CommandMetadata {
return command_metadata.CommandMetadata{
Name: "api",
Description: T("Set or view target api url"),
Usage: T("CF_NAME api [URL]"),
Flags: []cli.Flag{
cli.BoolFlag{Name: "skip-ssl-validation", Usage: T("Please don't")},
},
}
}
func (cmd Api) GetRequirements(_ requirements.Factory, _ *cli.Context) (reqs []requirements.Requirement, err error) {
return
}
func (cmd Api) Run(c *cli.Context) {
if len(c.Args()) == 0 {
if cmd.config.ApiEndpoint() == "" {
cmd.ui.Say(fmt.Sprintf(T("No api endpoint set. Use '{{.Name}}' to set an endpoint",
map[string]interface{}{"Name": terminal.CommandColor(cf.Name() + " api")})))
} else {
cmd.ui.Say(T("API endpoint: {{.ApiEndpoint}} (API version: {{.ApiVersion}})",
map[string]interface{}{"ApiEndpoint": terminal.EntityNameColor(cmd.config.ApiEndpoint()),
"ApiVersion": terminal.EntityNameColor(cmd.config.ApiVersion())}))
}
return
}
endpoint := c.Args()[0]
cmd.ui.Say(T("Setting api endpoint to {{.Endpoint}}...",
map[string]interface{}{"Endpoint": terminal.EntityNameColor(endpoint)}))
cmd.setApiEndpoint(endpoint, c.Bool("skip-ssl-validation"), cmd.Metadata().Name)
cmd.ui.Ok()
cmd.ui.Say("")
cmd.ui.ShowConfiguration(cmd.config)
}
func (cmd Api) setApiEndpoint(endpoint string, skipSSL bool, cmdName string) {
if strings.HasSuffix(endpoint, "/") {
endpoint = strings.TrimSuffix(endpoint, "/")
}
cmd.config.SetSSLDisabled(skipSSL)
endpoint, err := cmd.endpointRepo.UpdateEndpoint(endpoint)
if err != nil {
cmd.config.SetApiEndpoint("")
cmd.config.SetSSLDisabled(false)
switch typedErr := err.(type) {
case *errors.InvalidSSLCert:
cfApiCommand := terminal.CommandColor(fmt.Sprintf("%s %s --skip-ssl-validation", cf.Name(), cmdName))
tipMessage := fmt.Sprintf(T("TIP: Use '{{.ApiCommand}}' to continue with an insecure API endpoint",
map[string]interface{}{"ApiCommand": cfApiCommand}))
cmd.ui.Failed(T("Invalid SSL Cert for {{.URL}}\n{{.TipMessage}}",
map[string]interface{}{"URL": typedErr.URL, "TipMessage": tipMessage}))
default:
cmd.ui.Failed(typedErr.Error())
}
}
if !strings.HasPrefix(endpoint, "https://") {
cmd.ui.Say(terminal.WarningColor(T("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended\n")))
}
}