github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/commands/api.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/cloudfoundry/cli/cf"
     8  	"github.com/cloudfoundry/cli/cf/commandregistry"
     9  	"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
    10  	"github.com/cloudfoundry/cli/cf/errors"
    11  	"github.com/cloudfoundry/cli/cf/flags"
    12  	. "github.com/cloudfoundry/cli/cf/i18n"
    13  	"github.com/cloudfoundry/cli/cf/requirements"
    14  	"github.com/cloudfoundry/cli/cf/terminal"
    15  )
    16  
    17  type API struct {
    18  	ui           terminal.UI
    19  	endpointRepo coreconfig.EndpointRepository
    20  	config       coreconfig.ReadWriter
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(API{})
    25  }
    26  
    27  func (cmd API) MetaData() commandregistry.CommandMetadata {
    28  	fs := make(map[string]flags.FlagSet)
    29  	fs["unset"] = &flags.BoolFlag{Name: "unset", Usage: T("Remove all api endpoint targeting")}
    30  	fs["skip-ssl-validation"] = &flags.BoolFlag{Name: "skip-ssl-validation", Usage: T("Skip verification of the API endpoint. Not recommended!")}
    31  
    32  	return commandregistry.CommandMetadata{
    33  		Name:        "api",
    34  		Description: T("Set or view target api url"),
    35  		Usage: []string{
    36  			T("CF_NAME api [URL]"),
    37  		},
    38  		Flags: fs,
    39  	}
    40  }
    41  
    42  func (cmd API) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    43  	reqs := []requirements.Requirement{}
    44  	return reqs, nil
    45  }
    46  
    47  func (cmd API) SetDependency(deps commandregistry.Dependency, _ bool) commandregistry.Command {
    48  	cmd.ui = deps.UI
    49  	cmd.config = deps.Config
    50  	cmd.endpointRepo = deps.RepoLocator.GetEndpointRepository()
    51  	return cmd
    52  }
    53  
    54  func (cmd API) Execute(c flags.FlagContext) error {
    55  	if c.Bool("unset") {
    56  		cmd.ui.Say(T("Unsetting api endpoint..."))
    57  		cmd.config.SetAPIEndpoint("")
    58  
    59  		cmd.ui.Ok()
    60  		cmd.ui.Say(T("\nNo api endpoint set."))
    61  
    62  	} else if len(c.Args()) == 0 {
    63  		if cmd.config.APIEndpoint() == "" {
    64  			cmd.ui.Say(fmt.Sprintf(T("No api endpoint set. Use '{{.Name}}' to set an endpoint",
    65  				map[string]interface{}{"Name": terminal.CommandColor(cf.Name + " api")})))
    66  		} else {
    67  			cmd.ui.Say(T("API endpoint: {{.APIEndpoint}} (API version: {{.APIVersion}})",
    68  				map[string]interface{}{"APIEndpoint": terminal.EntityNameColor(cmd.config.APIEndpoint()),
    69  					"APIVersion": terminal.EntityNameColor(cmd.config.APIVersion())}))
    70  		}
    71  	} else {
    72  		endpoint := c.Args()[0]
    73  
    74  		cmd.ui.Say(T("Setting api endpoint to {{.Endpoint}}...",
    75  			map[string]interface{}{"Endpoint": terminal.EntityNameColor(endpoint)}))
    76  		err := cmd.setAPIEndpoint(endpoint, c.Bool("skip-ssl-validation"), cmd.MetaData().Name)
    77  		if err != nil {
    78  			return err
    79  		}
    80  		cmd.ui.Ok()
    81  
    82  		cmd.ui.Say("")
    83  		cmd.ui.ShowConfiguration(cmd.config)
    84  	}
    85  	return nil
    86  }
    87  
    88  func (cmd API) setAPIEndpoint(endpoint string, skipSSL bool, cmdName string) error {
    89  	if strings.HasSuffix(endpoint, "/") {
    90  		endpoint = strings.TrimSuffix(endpoint, "/")
    91  	}
    92  
    93  	cmd.config.SetSSLDisabled(skipSSL)
    94  
    95  	refresher := coreconfig.APIConfigRefresher{
    96  		Endpoint:     endpoint,
    97  		EndpointRepo: cmd.endpointRepo,
    98  		Config:       cmd.config,
    99  	}
   100  
   101  	warning, err := refresher.Refresh()
   102  	if err != nil {
   103  		cmd.config.SetAPIEndpoint("")
   104  		cmd.config.SetSSLDisabled(false)
   105  
   106  		switch typedErr := err.(type) {
   107  		case *errors.InvalidSSLCert:
   108  			cfAPICommand := terminal.CommandColor(fmt.Sprintf("%s %s --skip-ssl-validation", cf.Name, cmdName))
   109  			tipMessage := fmt.Sprintf(T("TIP: Use '{{.APICommand}}' to continue with an insecure API endpoint",
   110  				map[string]interface{}{"APICommand": cfAPICommand}))
   111  			return errors.New(T("Invalid SSL Cert for {{.URL}}\n{{.TipMessage}}",
   112  				map[string]interface{}{"URL": typedErr.URL, "TipMessage": tipMessage}))
   113  		default:
   114  			return typedErr
   115  		}
   116  	}
   117  
   118  	if warning != nil {
   119  		cmd.ui.Say(terminal.WarningColor(warning.Warn()))
   120  	}
   121  	return nil
   122  }