2020-07-29 15:12:45 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker, Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-07-15 15:09:05 +02:00
|
|
|
package prompt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
)
|
|
|
|
|
2020-07-16 16:32:37 +02:00
|
|
|
// UI - prompt user input
|
2020-07-15 15:09:05 +02:00
|
|
|
type UI interface {
|
|
|
|
Select(message string, options []string) (int, error)
|
|
|
|
Input(message string, defaultValue string) (string, error)
|
|
|
|
Confirm(message string, defaultValue bool) (bool, error)
|
|
|
|
Password(message string) (string, error)
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:32:37 +02:00
|
|
|
// User - aggregates prompt methods
|
2020-07-15 15:09:05 +02:00
|
|
|
type User struct{}
|
|
|
|
|
2020-07-16 16:32:37 +02:00
|
|
|
// Select - displays a list
|
2020-07-15 15:09:05 +02:00
|
|
|
func (u User) Select(message string, options []string) (int, error) {
|
|
|
|
qs := &survey.Select{
|
|
|
|
Message: message,
|
|
|
|
Options: options,
|
|
|
|
}
|
|
|
|
var selected int
|
|
|
|
err := survey.AskOne(qs, &selected, nil)
|
|
|
|
return selected, err
|
|
|
|
}
|
2020-07-16 16:32:37 +02:00
|
|
|
|
|
|
|
// Input text with default value
|
2020-07-15 15:09:05 +02:00
|
|
|
func (u User) Input(message string, defaultValue string) (string, error) {
|
|
|
|
qs := &survey.Input{
|
|
|
|
Message: message,
|
|
|
|
Default: defaultValue,
|
|
|
|
}
|
|
|
|
var s string
|
|
|
|
err := survey.AskOne(qs, &s, nil)
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:32:37 +02:00
|
|
|
// Confirm asks for yes or no input
|
2020-07-15 15:09:05 +02:00
|
|
|
func (u User) Confirm(message string, defaultValue bool) (bool, error) {
|
|
|
|
qs := &survey.Confirm{
|
|
|
|
Message: message,
|
|
|
|
Default: defaultValue,
|
|
|
|
}
|
|
|
|
var b bool
|
|
|
|
err := survey.AskOne(qs, &b, nil)
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:32:37 +02:00
|
|
|
// Password implemetns a text input with masked characters
|
2020-07-15 15:09:05 +02:00
|
|
|
func (u User) Password(message string) (string, error) {
|
|
|
|
qs := &survey.Password{
|
|
|
|
Message: message,
|
|
|
|
}
|
|
|
|
var p string
|
|
|
|
err := survey.AskOne(qs, &p, nil)
|
|
|
|
return p, err
|
|
|
|
|
|
|
|
}
|