package osutils
import (
"bufio"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"ganigeorgiev.com/pocketbase/ozzo-validation/v4/is"
)
func LaunchURL(url string) error {
if err := is.URL.Validate(url); err != nil {
return err
}
switch runtime.GOOS {
case "darwin":
return exec.Command("open", url).Start()
case "windows":
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
default:
return exec.Command("xdg-open", url).Start()
}
}
func YesNoPrompt(message string, fallback bool) bool {
options := "Y/n"
if !fallback {
options = "y/N"
}
r := bufio.NewReader(os.Stdin)
var s string
for {
fmt.Fprintf(os.Stderr, "%s (%s) ", message, options)
s, _ = r.ReadString('\n')
s = strings.ToLower(strings.TrimSpace(s))
switch s {
case "":
return fallback
case "y", "yes":
return true
case "n", "no":
return false
}
}
}