3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-28 23:19:30 +01:00

Allow piping into genpasswd for docker

This commit is contained in:
Daniel Oaks 2019-02-03 07:21:46 +10:00
parent becbf409da
commit 6f2b610736

View File

@ -6,8 +6,10 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"log" "log"
"os"
"strings" "strings"
"syscall" "syscall"
@ -23,11 +25,17 @@ var commit = ""
// get a password from stdin from the user // get a password from stdin from the user
func getPassword() string { func getPassword() string {
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin)) fd := int(os.Stdin.Fd())
if err != nil { if terminal.IsTerminal(fd) {
log.Fatal("Error reading password:", err.Error()) 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() { func main() {
@ -51,20 +59,29 @@ Options:
// don't require a config file for genpasswd // don't require a config file for genpasswd
if arguments["genpasswd"].(bool) { if arguments["genpasswd"].(bool) {
fmt.Print("Enter Password: ") var password string
password := getPassword() fd := int(os.Stdin.Fd())
fmt.Print("\n") if terminal.IsTerminal(fd) {
fmt.Print("Reenter Password: ") fmt.Print("Enter Password: ")
confirm := getPassword() password = getPassword()
fmt.Print("\n") fmt.Print("\n")
if confirm != password { fmt.Print("Reenter Password: ")
log.Fatal("passwords do not match") 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) hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
if err != nil { if err != nil {
log.Fatal("encoding error:", err.Error()) log.Fatal("encoding error:", err.Error())
} }
fmt.Println(string(hash)) fmt.Print(string(hash))
if terminal.IsTerminal(fd) {
fmt.Println()
}
return return
} }