3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-25 13:29:27 +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
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"syscall"
@ -23,11 +25,17 @@ var commit = ""
// get a password from stdin from the user
func getPassword() string {
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)
}
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
return text
}
func main() {
@ -51,8 +59,11 @@ Options:
// don't require a config file for genpasswd
if arguments["genpasswd"].(bool) {
var password string
fd := int(os.Stdin.Fd())
if terminal.IsTerminal(fd) {
fmt.Print("Enter Password: ")
password := getPassword()
password = getPassword()
fmt.Print("\n")
fmt.Print("Reenter Password: ")
confirm := getPassword()
@ -60,11 +71,17 @@ Options:
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
}