github.com/searKing/golang/go@v1.2.117/terminal/confirm.go (about) 1 // Copyright 2021 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package terminal 6 7 import ( 8 "bufio" 9 "fmt" 10 "os" 11 "strings" 12 ) 13 14 func AskForConfirmation(question string) (bool, error) { 15 reader := bufio.NewReader(os.Stdin) 16 17 for { 18 fmt.Printf("%s [y/n]: ", question) 19 20 response, err := reader.ReadString('\n') 21 if err != nil { 22 return false, fmt.Errorf("read string failed %w", err) 23 } 24 25 response = strings.ToLower(strings.TrimSpace(response)) 26 if response == "y" || response == "yes" { 27 return true, nil 28 } else if response == "n" || response == "no" { 29 return false, nil 30 } 31 } 32 }