From 6f2b610736622fbb3722e215f80edbf996ad0dac Mon Sep 17 00:00:00 2001 From: Daniel Oaks Date: Sun, 3 Feb 2019 07:21:46 +1000 Subject: [PATCH] Allow piping into genpasswd for docker --- oragono.go | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/oragono.go b/oragono.go index 03242897..02375070 100644 --- a/oragono.go +++ b/oragono.go @@ -6,8 +6,10 @@ package main import ( + "bufio" "fmt" "log" + "os" "strings" "syscall" @@ -23,11 +25,17 @@ var commit = "" // get a password from stdin from the user func getPassword() string { - bytePassword, err := terminal.ReadPassword(int(syscall.Stdin)) - if err != nil { - log.Fatal("Error reading password:", err.Error()) + fd := int(os.Stdin.Fd()) + if terminal.IsTerminal(fd) { + bytePassword, err := terminal.ReadPassword(int(syscall.Stdin)) + if err != nil { + log.Fatal("Error reading password:", err.Error()) + } + return string(bytePassword) } - return string(bytePassword) + reader := bufio.NewReader(os.Stdin) + text, _ := reader.ReadString('\n') + return text } func main() { @@ -51,20 +59,29 @@ Options: // don't require a config file for genpasswd if arguments["genpasswd"].(bool) { - fmt.Print("Enter Password: ") - password := getPassword() - fmt.Print("\n") - fmt.Print("Reenter Password: ") - confirm := getPassword() - fmt.Print("\n") - if confirm != password { - log.Fatal("passwords do not match") + var password string + fd := int(os.Stdin.Fd()) + if terminal.IsTerminal(fd) { + fmt.Print("Enter Password: ") + password = getPassword() + fmt.Print("\n") + fmt.Print("Reenter Password: ") + confirm := getPassword() + fmt.Print("\n") + if confirm != password { + log.Fatal("passwords do not match") + } + } else { + password = getPassword() } hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost) if err != nil { log.Fatal("encoding error:", err.Error()) } - fmt.Println(string(hash)) + fmt.Print(string(hash)) + if terminal.IsTerminal(fd) { + fmt.Println() + } return }