code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/api_command.go (about)

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