github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/api_command.go (about)

     1  package v2
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/liamawhite/cli-with-i18n/actor/v2action"
     8  	"github.com/liamawhite/cli-with-i18n/command"
     9  	"github.com/liamawhite/cli-with-i18n/command/flag"
    10  	"github.com/liamawhite/cli-with-i18n/command/v2/shared"
    11  )
    12  
    13  //go:generate counterfeiter . APIActor
    14  
    15  type APIActor interface {
    16  	ClearTarget(config v2action.Config)
    17  	SetTarget(config v2action.Config, 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  	cmd.UI.DisplayKeyValueTable("", [][]string{
    64  		{cmd.UI.TranslateText("api endpoint:"), cmd.Config.Target()},
    65  		{cmd.UI.TranslateText("api version:"), cmd.Config.APIVersion()},
    66  	}, 3)
    67  
    68  	user, err := cmd.Config.CurrentUser()
    69  	if user.Name == "" {
    70  		cmd.UI.DisplayText("Not logged in. Use '{{.CFLoginCommand}}' to log in.", map[string]interface{}{
    71  			"CFLoginCommand": fmt.Sprintf("%s login", cmd.Config.BinaryName()),
    72  		})
    73  	}
    74  	return err
    75  }
    76  
    77  func (cmd *ApiCommand) ClearTarget() error {
    78  	cmd.UI.DisplayTextWithFlavor("Unsetting api endpoint...")
    79  	cmd.Actor.ClearTarget(cmd.Config)
    80  	cmd.UI.DisplayOK()
    81  	return nil
    82  }
    83  
    84  func (cmd *ApiCommand) setAPI() error {
    85  	cmd.UI.DisplayTextWithFlavor("Setting api endpoint to {{.Endpoint}}...", map[string]interface{}{
    86  		"Endpoint": cmd.OptionalArgs.URL,
    87  	})
    88  
    89  	apiURL := processURL(cmd.OptionalArgs.URL)
    90  
    91  	_, err := cmd.Actor.SetTarget(cmd.Config, v2action.TargetSettings{
    92  		URL:               apiURL,
    93  		SkipSSLValidation: cmd.SkipSSLValidation,
    94  		DialTimeout:       cmd.Config.DialTimeout(),
    95  	})
    96  	if err != nil {
    97  		return shared.HandleError(err)
    98  	}
    99  
   100  	if strings.HasPrefix(apiURL, "http:") {
   101  		cmd.UI.DisplayText("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended")
   102  	}
   103  
   104  	cmd.UI.DisplayOK()
   105  	cmd.UI.DisplayNewline()
   106  	return nil
   107  }
   108  
   109  func processURL(apiURL string) string {
   110  	if !strings.HasPrefix(apiURL, "http") {
   111  		return fmt.Sprintf("https://%s", apiURL)
   112  
   113  	}
   114  	return apiURL
   115  }