func main() { // Well? [yN]: boolAnsFalseDefault := false err := interact.NewInteraction("Well?").Resolve(&boolAnsFalseDefault) if err != nil { fatal(err) } fmt.Println(boolAnsFalseDefault) // Well? [Yn]: boolAnsTrueDefault := true err = interact.NewInteraction("Well?").Resolve(&boolAnsTrueDefault) if err != nil { fatal(err) } fmt.Println(boolAnsTrueDefault) // Well? [yn]: var boolAnsNoDefault bool err = interact.NewInteraction("Well?").Resolve(interact.Required(&boolAnsNoDefault)) if err != nil { fatal(err) } fmt.Println(boolAnsNoDefault) // Message (hello): strAnsDefault := "hello" err = interact.NewInteraction("Message").Resolve(&strAnsDefault) if err != nil { fatal(err) } fmt.Println(strAnsDefault) // Message (): var strAnsEmptyDefault string err = interact.NewInteraction("Message").Resolve(&strAnsEmptyDefault) if err != nil { fatal(err) } fmt.Println(strAnsEmptyDefault) // Message: var strAnsNoDefault string err = interact.NewInteraction("Message").Resolve(interact.Required(&strAnsNoDefault)) if err != nil { fatal(err) } fmt.Println(strAnsNoDefault) numbers := []string{"uno", "dos", "tres"} // 1: One // 2: Two // 3: Three // Choose a number: var chosenFoo string err = interact.NewInteraction( "Choose a number", interact.Choice{Display: "One", Value: numbers[0]}, interact.Choice{Display: "Two", Value: numbers[1]}, interact.Choice{Display: "Three", Value: numbers[2]}, ).Resolve(&chosenFoo) if err != nil { fatal(err) } fmt.Println(chosenFoo) // 1: One // 2: Two // 3: Three // Choose a number (2): chosenFooWithDefault := "dos" err = interact.NewInteraction( "Choose a number", interact.Choice{Display: "One", Value: numbers[0]}, interact.Choice{Display: "Two", Value: numbers[1]}, interact.Choice{Display: "Three", Value: numbers[2]}, ).Resolve(&chosenFooWithDefault) if err != nil { fatal(err) } fmt.Println(chosenFooWithDefault) // 1. One // 2. Two // 3. Three // 4. none // Choose a number (4): var chosenFooOptional *string err = interact.NewInteraction( "Choose a number", interact.Choice{Display: "One", Value: &numbers[0]}, interact.Choice{Display: "Two", Value: &numbers[1]}, interact.Choice{Display: "Three", Value: &numbers[2]}, interact.Choice{Display: "none", Value: nil}, ).Resolve(&chosenFooOptional) if err != nil { fatal(err) } fmt.Println(chosenFooOptional) // Username: var username string err = interact.NewInteraction("Username").Resolve(interact.Required(&username)) if err != nil { fatal(err) } fmt.Println(username) // Password: var password interact.Password err = interact.NewInteraction("Password").Resolve(interact.Required(&password)) if err != nil { fatal(err) } fmt.Println(password) // Interrupt: var ctrlCTest string err = interact.NewInteraction("Interrupt").Resolve(interact.Required(&ctrlCTest)) if err != nil { fmt.Println(err) } }
ExpectedOutput: "some prompt (): \n", }), Entry("when a blank line is entered, followed by EOF", Example{ Prompt: "some prompt", Input: "\n", ExpectedAnswer: interact.Password(""), ExpectedOutput: "some prompt (): \n", }), ) Context("when required", func() { BeforeEach(func() { destination = interact.Required(destination) }) DescribeTable("Resolve", (Example).Run, Entry("when a string is entered", Example{ Prompt: "some prompt", Input: "forty two\n", ExpectedAnswer: interact.Password("forty two"), ExpectedOutput: "some prompt: \n", }), Entry("when a blank line is entered, followed by EOF", Example{ Prompt: "some prompt",
func (command *LoginCommand) loginWith(method atc.AuthMethod, connection concourse.Connection) error { var token atc.AuthToken switch method.Type { case atc.AuthTypeOAuth: fmt.Println("navigate to the following URL in your browser:") fmt.Println("") fmt.Printf(" %s\n", method.AuthURL) fmt.Println("") for { var tokenStr string err := interact.NewInteraction("enter token").Resolve(interact.Required(&tokenStr)) if err != nil { return err } segments := strings.SplitN(tokenStr, " ", 2) if len(segments) != 2 { fmt.Println("token must be of the format 'TYPE VALUE', e.g. 'Bearer ...'") continue } token.Type = segments[0] token.Value = segments[1] break } case atc.AuthTypeBasic: var username string err := interact.NewInteraction("username").Resolve(interact.Required(&username)) if err != nil { return err } var password interact.Password err = interact.NewInteraction("password").Resolve(interact.Required(&password)) if err != nil { return err } newUnauthedClient, err := rc.NewConnection(connection.URL(), command.Insecure) if err != nil { return err } basicAuthClient, err := concourse.NewConnection( newUnauthedClient.URL(), &http.Client{ Transport: basicAuthTransport{ username: username, password: string(password), base: newUnauthedClient.HTTPClient().Transport, }, }, ) if err != nil { return err } client := concourse.NewClient(basicAuthClient) token, err = client.AuthToken() if err != nil { return err } } err := rc.SaveTarget( Fly.Target, connection.URL(), command.Insecure, &rc.TargetToken{ Type: token.Type, Value: token.Value, }, ) if err != nil { return err } fmt.Println("token saved") return nil }
func (command *LoginCommand) loginWith(method atc.AuthMethod, client concourse.Client) error { var token atc.AuthToken switch method.Type { case atc.AuthTypeOAuth: fmt.Println("navigate to the following URL in your browser:") fmt.Println("") fmt.Printf(" %s\n", method.AuthURL) fmt.Println("") for { var tokenStr string err := interact.NewInteraction("enter token").Resolve(interact.Required(&tokenStr)) if err != nil { return err } segments := strings.SplitN(tokenStr, " ", 2) if len(segments) != 2 { fmt.Println("token must be of the format 'TYPE VALUE', e.g. 'Bearer ...'") continue } token.Type = segments[0] token.Value = segments[1] break } case atc.AuthTypeBasic: var username string if command.Username != "" { username = command.Username } else { err := interact.NewInteraction("username").Resolve(interact.Required(&username)) if err != nil { return err } } var password string if command.Password != "" { password = command.Password } else { var interactivePassword interact.Password err := interact.NewInteraction("password").Resolve(interact.Required(&interactivePassword)) if err != nil { return err } password = string(interactivePassword) } newUnauthedClient := rc.NewUnauthenticatedClient(client.URL(), command.Insecure) basicAuthClient := concourse.NewClient( newUnauthedClient.URL(), &http.Client{ Transport: basicAuthTransport{ username: username, password: password, base: newUnauthedClient.HTTPClient().Transport, }, }, ) var err error token, err = basicAuthClient.AuthToken() if err != nil { return err } } return command.saveTarget( client.URL(), &rc.TargetToken{ Type: token.Type, Value: token.Value, }, ) }