func()

in cli/commands/login.go [113:196]


func (cmd *Login) Run(scope scope.Scope, c *cli.Context) {
	if !c.Args().Present() {
		error_handler.ErrorExit("A URL must be provided as the first argument", error_handler.CLIUsageErrorExitCode)
	}

	// If an argument was not supplied, it is set to empty string
	cmd.network.BrooklynUrl = c.Args().Get(0)
	cmd.brooklynUser = c.Args().Get(1)
	cmd.brooklynPass = c.Args().Get(2)
	cmd.network.SkipSslChecks = c.Bool("skipSslChecks")

	//clear credentials
	cmd.network.Credentials = ""

	authParamValue := c.String(authorizationParam)
	if authParamValue != "" {
		parts := strings.SplitN(authParamValue, ":", 2)
		if len(parts) == 2 && strings.EqualFold(parts[0], BEARER_AUTH) {
			cmd.network.AuthorizationType = BEARER_AUTH
			cmd.network.Credentials = parts[1]
		} else {
			cmd.network.AuthorizationType = BASIC_AUTH
		}
	} else {
		cmd.network.AuthorizationType = BASIC_AUTH
	}

	config := io.GetConfig()

	if err := net.VerifyLoginURL(cmd.network); err != nil {
		error_handler.ErrorExit(err)
	}

	// Strip off trailing '/' from URL if present.
	if cmd.network.BrooklynUrl[len(cmd.network.BrooklynUrl)-1] == '/' {
		if len(cmd.network.BrooklynUrl) == 1 {
			error_handler.ErrorExit("URL must not be a single \"/\" character", error_handler.CLIUsageErrorExitCode)
		}
		cmd.network.BrooklynUrl = cmd.network.BrooklynUrl[0 : len(cmd.network.BrooklynUrl)-1]
	}

	if cmd.network.BrooklynUrl != "" && cmd.brooklynUser == "" && authParamValue == "" {
		// if only target supplied at command line see if it already exists in the config file
		if credentials, err := config.GetNetworkCredentialsForTarget(cmd.network.BrooklynUrl); err == nil {
			cmd.network.Credentials = credentials
		}
		if authorizationType, err := config.GetAuthType(cmd.network.BrooklynUrl); err == nil {
			cmd.network.AuthorizationType = authorizationType
		}
	}

	if cmd.network.AuthorizationType == BASIC_AUTH && cmd.network.Credentials == "" {
		cmd.getCredentialsFromCommandLine()
	}

	// now persist these credentials to the yaml file
	cmd.config.SetNetworkCredentials(cmd.network.BrooklynUrl, cmd.network.Credentials)
	cmd.config.SetAuthType(cmd.network.BrooklynUrl, cmd.network.AuthorizationType)
	cmd.config.SetSkipSslChecks(cmd.network.SkipSslChecks)
	cmd.config.Write()

	loginVersion, code, err := version.Version(cmd.network)
	if nil != err {
		if code == http.StatusUnauthorized {
			err = errors.New("Unauthorized")
		}
		switch err.(type) {
		case *url.Error:
			urlError := err.(*url.Error)
			if !cmd.network.SkipSslChecks && strings.Contains(fmt.Sprint(urlError.Err), "x509") {
				message := fmt.Sprint(urlError) + `

This may be due to a missing or untrusted certificate in use by the server.
If this is expected and you trust the connection to the server, you can ignore this with:

    br login --skipSslChecks ` + cmd.network.BrooklynUrl + `
`
				err = errors.New(message)
			}
		}
		error_handler.ErrorExit(err)
	}
	fmt.Printf("Connected to Brooklyn version %s at %s\n", loginVersion.Version, cmd.network.BrooklynUrl)
}