func (command *LoginCommand) Execute(args []string) error { var connection concourse.Connection var err error if command.ATCURL != "" { connection, err = rc.NewConnection(command.ATCURL, command.Insecure) } else { connection, err = rc.CommandTargetConnection(Fly.Target, &command.Insecure) } if err != nil { return err } client := concourse.NewClient(connection) authMethods, err := client.ListAuthMethods() if err != nil { return err } var chosenMethod atc.AuthMethod switch len(authMethods) { case 0: fmt.Println("no auth methods configured; updating target data") err := rc.SaveTarget( Fly.Target, connection.URL(), command.Insecure, &rc.TargetToken{}, ) if err != nil { return err } return nil case 1: chosenMethod = authMethods[0] default: choices := make([]interact.Choice, len(authMethods)) for i, method := range authMethods { choices[i] = interact.Choice{ Display: method.DisplayName, Value: method, } } err = interact.NewInteraction("choose an auth method", choices...).Resolve(&chosenMethod) if err != nil { return err } } return command.loginWith(chosenMethod, connection) }
func (command *LoginCommand) Execute(args []string) error { if isURL(Fly.Target) { return errors.New("name for the target must be specified (--target/-t)") } var connection concourse.Connection var err error if command.ATCURL != "" { connection, err = rc.NewConnection(command.ATCURL, command.Insecure) } else { connection, err = rc.CommandTargetConnection(Fly.Target, &command.Insecure) } if err != nil { return err } client := concourse.NewClient(connection) authMethods, err := client.ListAuthMethods() if err != nil { return err } var chosenMethod atc.AuthMethod if command.Username != "" && command.Password != "" { for _, method := range authMethods { if method.Type == atc.AuthTypeBasic { chosenMethod = method break } } if chosenMethod.Type == "" { return errors.New("basic auth is not available") } } else { switch len(authMethods) { case 0: return command.saveTarget( connection.URL(), &rc.TargetToken{}, ) case 1: chosenMethod = authMethods[0] default: choices := make([]interact.Choice, len(authMethods)) for i, method := range authMethods { choices[i] = interact.Choice{ Display: method.DisplayName, Value: method, } } err = interact.NewInteraction("choose an auth method", choices...).Resolve(&chosenMethod) if err != nil { return err } } } return command.loginWith(chosenMethod, connection) }