...

Source file src/github.com/ory/x/cmdx/user_input.go

Documentation: github.com/ory/x/cmdx

     1  package cmdx
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"strings"
     9  )
    10  
    11  // asks for confirmation with the question string s and reads the answer
    12  // pass nil to use os.Stdin and os.Stdout
    13  func AskForConfirmation(s string, stdin io.Reader, stdout io.Writer) bool {
    14  	if stdin == nil {
    15  		stdin = os.Stdin
    16  	}
    17  	if stdout == nil {
    18  		stdout = os.Stdout
    19  	}
    20  
    21  	reader := bufio.NewReader(stdin)
    22  
    23  	for {
    24  		_, err := fmt.Fprintf(stdout, "%s [y/n]: ", s)
    25  		Must(err, "%s", err)
    26  
    27  		response, err := reader.ReadString('\n')
    28  		Must(err, "%s", err)
    29  
    30  		response = strings.ToLower(strings.TrimSpace(response))
    31  		if response == "y" || response == "yes" {
    32  			return true
    33  		} else if response == "n" || response == "no" {
    34  			return false
    35  		}
    36  	}
    37  }
    38  

View as plain text