forked from cloudfoundry/cli
/
api.go
73 lines (60 loc) · 1.68 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
package commands
import (
"cf"
"cf/api"
"cf/configuration"
"cf/requirements"
"cf/terminal"
"fmt"
"github.com/codegangsta/cli"
"strings"
)
type Api struct {
ui terminal.UI
endpointRepo api.EndpointRepository
config configuration.Reader
}
type ApiEndpointSetter interface {
SetApiEndpoint(endpoint string)
}
func NewApi(ui terminal.UI, config configuration.Reader, endpointRepo api.EndpointRepository) (cmd Api) {
cmd.ui = ui
cmd.config = config
cmd.endpointRepo = endpointRepo
return
}
func (cmd Api) GetRequirements(reqFactory requirements.Factory, c *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("No api endpoint set. Use '%s' to set an endpoint", terminal.CommandColor(cf.Name()+" api")))
} else {
cmd.ui.Say(
"API endpoint: %s (API version: %s)",
terminal.EntityNameColor(cmd.config.ApiEndpoint()),
terminal.EntityNameColor(cmd.config.ApiVersion()),
)
}
return
}
cmd.SetApiEndpoint(c.Args()[0])
}
func (cmd Api) SetApiEndpoint(endpoint string) {
if strings.HasSuffix(endpoint, "/") {
endpoint = strings.TrimSuffix(endpoint, "/")
}
cmd.ui.Say("Setting api endpoint to %s...", terminal.EntityNameColor(endpoint))
endpoint, apiErr := cmd.endpointRepo.UpdateEndpoint(endpoint)
if apiErr != nil {
cmd.ui.Failed(apiErr.Error())
return
}
cmd.ui.Ok()
cmd.ui.Say("")
if !strings.HasPrefix(endpoint, "https://") {
cmd.ui.Say(terminal.WarningColor("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended\n"))
}
cmd.ui.ShowConfiguration(cmd.config)
}