Browse Source

Allow piping into genpasswd for docker

tags/v1.0.0-rc1
Daniel Oaks 5 years ago
parent
commit
6f2b610736
1 changed files with 30 additions and 13 deletions
  1. 30
    13
      oragono.go

+ 30
- 13
oragono.go View File

@@ -6,8 +6,10 @@
6 6
 package main
7 7
 
8 8
 import (
9
+	"bufio"
9 10
 	"fmt"
10 11
 	"log"
12
+	"os"
11 13
 	"strings"
12 14
 	"syscall"
13 15
 
@@ -23,11 +25,17 @@ var commit = ""
23 25
 
24 26
 // get a password from stdin from the user
25 27
 func getPassword() string {
26
-	bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
27
-	if err != nil {
28
-		log.Fatal("Error reading password:", err.Error())
28
+	fd := int(os.Stdin.Fd())
29
+	if terminal.IsTerminal(fd) {
30
+		bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
31
+		if err != nil {
32
+			log.Fatal("Error reading password:", err.Error())
33
+		}
34
+		return string(bytePassword)
29 35
 	}
30
-	return string(bytePassword)
36
+	reader := bufio.NewReader(os.Stdin)
37
+	text, _ := reader.ReadString('\n')
38
+	return text
31 39
 }
32 40
 
33 41
 func main() {
@@ -51,20 +59,29 @@ Options:
51 59
 
52 60
 	// don't require a config file for genpasswd
53 61
 	if arguments["genpasswd"].(bool) {
54
-		fmt.Print("Enter Password: ")
55
-		password := getPassword()
56
-		fmt.Print("\n")
57
-		fmt.Print("Reenter Password: ")
58
-		confirm := getPassword()
59
-		fmt.Print("\n")
60
-		if confirm != password {
61
-			log.Fatal("passwords do not match")
62
+		var password string
63
+		fd := int(os.Stdin.Fd())
64
+		if terminal.IsTerminal(fd) {
65
+			fmt.Print("Enter Password: ")
66
+			password = getPassword()
67
+			fmt.Print("\n")
68
+			fmt.Print("Reenter Password: ")
69
+			confirm := getPassword()
70
+			fmt.Print("\n")
71
+			if confirm != password {
72
+				log.Fatal("passwords do not match")
73
+			}
74
+		} else {
75
+			password = getPassword()
62 76
 		}
63 77
 		hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
64 78
 		if err != nil {
65 79
 			log.Fatal("encoding error:", err.Error())
66 80
 		}
67
-		fmt.Println(string(hash))
81
+		fmt.Print(string(hash))
82
+		if terminal.IsTerminal(fd) {
83
+			fmt.Println()
84
+		}
68 85
 		return
69 86
 	}
70 87
 

Loading…
Cancel
Save