github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/api_command.go (about) 1 package v2 2 3 import ( 4 "fmt" 5 "strings" 6 7 "code.cloudfoundry.org/cli/actor/v2action" 8 "code.cloudfoundry.org/cli/command" 9 "code.cloudfoundry.org/cli/command/flag" 10 "code.cloudfoundry.org/cli/command/v2/shared" 11 ) 12 13 //go:generate counterfeiter . ApiActor 14 15 type ApiActor interface { 16 ClearTarget() 17 SetTarget(settings v2action.TargetSettings) (v2action.Warnings, error) 18 } 19 20 type ApiCommand struct { 21 OptionalArgs flag.APITarget `positional-args:"yes"` 22 SkipSSLValidation bool `long:"skip-ssl-validation" description:"Skip verification of the API endpoint. Not recommended!"` 23 Unset bool `long:"unset" description:"Remove all api endpoint targeting"` 24 usage interface{} `usage:"CF_NAME api [URL]"` 25 relatedCommands interface{} `related_commands:"auth, login, target"` 26 27 UI command.UI 28 Actor ApiActor 29 Config command.Config 30 } 31 32 func (cmd *ApiCommand) Setup(config command.Config, ui command.UI) error { 33 ccClient, uaaClient, err := shared.NewClients(config, ui, false) 34 if err != nil { 35 return err 36 } 37 38 cmd.Actor = v2action.NewActor(ccClient, uaaClient, config) 39 cmd.UI = ui 40 cmd.Config = config 41 return nil 42 } 43 44 func (cmd *ApiCommand) Execute(args []string) error { 45 if cmd.Unset { 46 return cmd.ClearTarget() 47 } 48 49 if cmd.OptionalArgs.URL != "" { 50 err := cmd.setAPI() 51 if err != nil { 52 return err 53 } 54 } 55 56 if cmd.Config.Target() == "" { 57 cmd.UI.DisplayText("No api endpoint set. Use '{{.Name}}' to set an endpoint", map[string]interface{}{ 58 "Name": "cf api", 59 }) 60 return nil 61 } 62 63 if cmd.Config.APIVersion() != "" { 64 err := command.WarnAPIVersionCheck(cmd.Config.APIVersion(), cmd.UI) 65 if err != nil { 66 return err 67 } 68 } 69 70 cmd.UI.DisplayKeyValueTable("", [][]string{ 71 {cmd.UI.TranslateText("api endpoint:"), cmd.Config.Target()}, 72 {cmd.UI.TranslateText("api version:"), cmd.Config.APIVersion()}, 73 }, 3) 74 75 user, err := cmd.Config.CurrentUser() 76 if user.Name == "" { 77 cmd.UI.DisplayText("Not logged in. Use '{{.CFLoginCommand}}' to log in.", map[string]interface{}{ 78 "CFLoginCommand": fmt.Sprintf("%s login", cmd.Config.BinaryName()), 79 }) 80 } 81 return err 82 } 83 84 func (cmd *ApiCommand) ClearTarget() error { 85 cmd.UI.DisplayTextWithFlavor("Unsetting api endpoint...") 86 cmd.Actor.ClearTarget() 87 cmd.UI.DisplayOK() 88 return nil 89 } 90 91 func (cmd *ApiCommand) setAPI() error { 92 cmd.UI.DisplayTextWithFlavor("Setting api endpoint to {{.Endpoint}}...", map[string]interface{}{ 93 "Endpoint": cmd.OptionalArgs.URL, 94 }) 95 96 apiURL := processURL(cmd.OptionalArgs.URL) 97 98 _, err := cmd.Actor.SetTarget(v2action.TargetSettings{ 99 URL: apiURL, 100 SkipSSLValidation: cmd.SkipSSLValidation, 101 DialTimeout: cmd.Config.DialTimeout(), 102 }) 103 if err != nil { 104 return err 105 } 106 107 if strings.HasPrefix(apiURL, "http:") { 108 cmd.UI.DisplayText("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended") 109 } 110 111 cmd.UI.DisplayOK() 112 cmd.UI.DisplayNewline() 113 return nil 114 } 115 116 func processURL(apiURL string) string { 117 if !strings.HasPrefix(apiURL, "http") { 118 return fmt.Sprintf("https://%s", apiURL) 119 120 } 121 return apiURL 122 }