github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/api_command.go (about)

     1  package v6
     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/v6/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, _ := shared.NewWrappedCloudControllerClient(config, ui)
    34  
    35  	cmd.Actor = v2action.NewActor(ccClient, nil, config)
    36  	cmd.UI = ui
    37  	cmd.Config = config
    38  	return nil
    39  }
    40  
    41  func (cmd *APICommand) Execute(args []string) error {
    42  	if cmd.Unset {
    43  		return cmd.ClearTarget()
    44  	}
    45  
    46  	if cmd.OptionalArgs.URL != "" {
    47  		err := cmd.setAPI()
    48  		if err != nil {
    49  			return err
    50  		}
    51  	}
    52  
    53  	if cmd.Config.Target() == "" {
    54  		cmd.UI.DisplayText("No api endpoint set. Use '{{.Name}}' to set an endpoint", map[string]interface{}{
    55  			"Name": "cf api",
    56  		})
    57  		return nil
    58  	}
    59  
    60  	if cmd.Config.APIVersion() != "" {
    61  		err := command.WarnIfAPIVersionBelowSupportedMinimum(cmd.Config.APIVersion(), cmd.UI)
    62  		if err != nil {
    63  			return err
    64  		}
    65  	}
    66  
    67  	cmd.UI.DisplayKeyValueTable("", [][]string{
    68  		{cmd.UI.TranslateText("api endpoint:"), cmd.Config.Target()},
    69  		{cmd.UI.TranslateText("api version:"), cmd.Config.APIVersion()},
    70  	}, 3)
    71  
    72  	user, err := cmd.Config.CurrentUser()
    73  	if user.Name == "" {
    74  		command.DisplayNotLoggedInText(cmd.Config.BinaryName(), cmd.UI)
    75  	}
    76  	return err
    77  }
    78  
    79  func (cmd *APICommand) ClearTarget() error {
    80  	cmd.UI.DisplayTextWithFlavor("Unsetting api endpoint...")
    81  	cmd.Actor.ClearTarget()
    82  	cmd.UI.DisplayOK()
    83  	return nil
    84  }
    85  
    86  func (cmd *APICommand) setAPI() error {
    87  	cmd.UI.DisplayTextWithFlavor("Setting api endpoint to {{.Endpoint}}...", map[string]interface{}{
    88  		"Endpoint": cmd.OptionalArgs.URL,
    89  	})
    90  
    91  	apiURL := processURL(cmd.OptionalArgs.URL)
    92  
    93  	_, err := cmd.Actor.SetTarget(v2action.TargetSettings{
    94  		URL:               apiURL,
    95  		SkipSSLValidation: cmd.SkipSSLValidation,
    96  		DialTimeout:       cmd.Config.DialTimeout(),
    97  	})
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	if strings.HasPrefix(apiURL, "http:") {
   103  		cmd.UI.DisplayText("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended")
   104  	}
   105  
   106  	cmd.UI.DisplayOK()
   107  	return nil
   108  }
   109  
   110  func processURL(apiURL string) string {
   111  	if !strings.HasPrefix(apiURL, "http") {
   112  		return fmt.Sprintf("https://%s", apiURL)
   113  
   114  	}
   115  	return apiURL
   116  }