storj/private/prompt/confirm.go
Egon Elbre 9752d01884 private/prompt: remove dependency to go-prompt
Change-Id: Ida8ef731ce806cec076343dc77d72a3b0d7736b4
2020-02-25 13:09:41 +02:00

37 lines
716 B
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
// Package prompt implements asking input from command line.
package prompt
import (
"fmt"
"strings"
"github.com/zeebo/errs"
)
// Error is the default error class for prompt package.
var Error = errs.Class("prompt")
// Confirm asks to confirm a question.
func Confirm(prompt string) (bool, error) {
for {
fmt.Printf(prompt + " :")
var response string
_, err := fmt.Scanln(&response)
if err != nil {
return false, Error.Wrap(err)
}
response = strings.TrimSpace(response)
switch strings.ToLower(response) {
case "yes", "y", "true":
return true, nil
case "no", "n", "false":
return false, nil
}
}
}