dsnet/util.go
2020-03-02 02:17:30 +00:00

51 lines
815 B
Go

package dsnet
import (
"os"
"bufio"
"strings"
"fmt"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func MustPromptString(prompt string, required bool) string {
reader := bufio.NewReader(os.Stdin)
var text string
var err error
for text == "" {
fmt.Printf("%s: ", prompt)
text, err = reader.ReadString('\n')
check(err)
text = strings.TrimSpace(text)
}
return text
}
func ExitFail(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, "\033[31m"+format+"\033[0m\n", a...)
os.Exit(1)
}
func ConfirmOrAbort(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format+" [y/n] ", a...)
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
if input == "y\n" {
return
} else {
ExitFail("Aborted.")
}
}