netcaptain/cmd/ncpt/helpers.go

50 lines
820 B
Go

package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func PromptWDefault(label, def string) string {
ret := Prompt(label, false)
if ret == "" {
return def
}
return ret
}
func Prompt(label string, required bool) string {
reader := bufio.NewReader(os.Stdin)
var res string
fmt.Println(label + ": ")
res, _ = reader.ReadString('\n')
res = strings.TrimSpace(res)
if res == "" && required {
fmt.Println("Non-empty response is required")
return Prompt(label, required)
}
return res
}
func FileExists(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func DirExists(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
func PrintErr(o string) {
fmt.Fprint(os.Stderr, o)
}