3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-14 16:09:32 +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 {
fd := int(os.Stdin.Fd())
if terminal.IsTerminal(fd) {
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin)) bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil { if err != nil {
log.Fatal("Error reading password:", err.Error()) 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,8 +59,11 @@ 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) {
var password string
fd := int(os.Stdin.Fd())
if terminal.IsTerminal(fd) {
fmt.Print("Enter Password: ") fmt.Print("Enter Password: ")
password := getPassword() password = getPassword()
fmt.Print("\n") fmt.Print("\n")
fmt.Print("Reenter Password: ") fmt.Print("Reenter Password: ")
confirm := getPassword() confirm := getPassword()
@ -60,11 +71,17 @@ Options:
if confirm != password { if confirm != password {
log.Fatal("passwords do not match") 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
} }