github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/prompting/command_line.go (about)

     1  package prompting
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mutagen-io/gopass"
     7  )
     8  
     9  // PromptCommandLineWithResponseMode performs command line prompting using the
    10  // specified response mode.
    11  func PromptCommandLineWithResponseMode(prompt string, mode ResponseMode) (string, error) {
    12  	// Figure out which getter to use.
    13  	var getter func() ([]byte, error)
    14  	if mode == ResponseModeEcho {
    15  		getter = gopass.GetPasswdEchoed
    16  	} else if mode == ResponseModeMasked {
    17  		getter = gopass.GetPasswdMasked
    18  	} else {
    19  		getter = gopass.GetPasswd
    20  	}
    21  
    22  	// Print the prompt.
    23  	fmt.Print(prompt)
    24  
    25  	// Get the result.
    26  	result, err := getter()
    27  	if err != nil {
    28  		return "", fmt.Errorf("unable to read response: %w", err)
    29  	}
    30  
    31  	// Success.
    32  	return string(result), nil
    33  }
    34  
    35  // PromptCommandLine performs command line prompting using an automatically
    36  // determined response mode.
    37  func PromptCommandLine(prompt string) (string, error) {
    38  	return PromptCommandLineWithResponseMode(prompt, determineResponseMode(prompt))
    39  }