dsnet/util.go

51 lines
803 B
Go
Raw Normal View History

2020-03-02 00:48:02 +01:00
package dsnet
2020-03-02 03:16:53 +01:00
import (
"bufio"
"fmt"
2020-03-02 03:54:43 +01:00
"os"
"strings"
2020-03-02 03:16:53 +01:00
)
2020-03-02 00:48:02 +01:00
func check(e error) {
2020-03-02 03:54:43 +01:00
if e != nil {
panic(e)
}
2020-03-02 00:48:02 +01:00
}
2020-03-02 03:16:53 +01:00
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.")
}
}