This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
/
genenv.go
128 lines (107 loc) · 2.98 KB
/
genenv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"text/template"
"github.com/danjac/podbaby/cmd/Godeps/_workspace/src/golang.org/x/crypto/ssh/terminal"
"github.com/danjac/podbaby/config"
)
func fileExists(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}
func readLine(prompt, defaultValue string, required bool) string {
for {
fmt.Printf("\r\n%s ", prompt)
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
if text == "" {
text = defaultValue
}
if text != "" || !required {
return text
}
}
}
func readSecret(prompt, defaultValue string, required bool) string {
for {
fmt.Printf("\r\n%s ", prompt)
bytePassword, _ := terminal.ReadPassword(int(os.Stdin.Fd()))
text := string(bytePassword)
text = strings.TrimSpace(text)
if text == "" {
text = defaultValue
}
if text != "" || !required {
return text
}
return text
}
}
// SecretKey prints a 32 byte random key you can use for secure keys
func SecretKey() {
fmt.Println(config.RandomKey())
}
// Genenv interactively generates an environment settings file
// Usage: podbaby genenv [dest-file.env]
func Genenv(dst string) {
if err := genenv(dst); err != nil {
panic(err)
}
}
func genenv(dst string) error {
// does destination file exist?
if fileExists(dst) {
text := readLine("This file already exists. Overwrite (Y/N)?", "N", false)
if strings.ToLower(text) != "y" {
return nil
}
}
pgHost := readLine("PostgreSQL Host (localhost:5432)?", "localhost:5432", true)
pgName := readLine("PostgreSQL database name?", "podbaby", true)
pgUser := readLine("PostgreSQL User?", "", false)
pgPass := readSecret("PostgreSQL Password?", "", false)
pgDisable := readLine("PostgreSQL SSL enabled (Y/N)", "N", false)
smtpHost := readLine("SMTP Host? (localhost)", "localhost", true)
smtpUser := readLine("SMTP User?", "", false)
smtpPass := readSecret("SMPT Password?", "", false)
secretKey := readSecret("Secret key?", config.RandomKey(), true)
secureCookieKey := readSecret("Secure cookie key?", config.RandomKey(), true)
gaKey := readLine("Google analytics key?", "", false)
dbURL := fmt.Sprintf("postgres://%s:%s@%s/%s",
pgUser, pgPass, pgHost, pgName)
if strings.ToLower(pgDisable) == "n" {
dbURL += "?sslmode=disable"
}
cfg := config.Default()
cfg.DatabaseURL = dbURL
cfg.SecretKey = secretKey
cfg.SecureCookieKey = secureCookieKey
cfg.GoogleAnalyticsID = gaKey
cfg.Mail.Host = smtpHost
cfg.Mail.User = smtpUser
cfg.Mail.Password = smtpPass
t, err := template.New("env").Parse(`
DB_URL="{{.DatabaseURL}}"
MAX_DB_CONNECTIONS="{{.MaxDBConnections}}"
GOOGLE_ANALYTICS_ID="{{.GoogleAnalyticsID}}"
SECRET_KEY="{{.SecretKey}}"
SECURE_COOKIE_KEY="{{.SecureCookieKey}}"
MAIL_ADDR="{{.Mail.Addr}}"
MAIL_HOST="{{.Mail.Host}}"
MAIL_USER="{{.Mail.User}}"
MAIL_PASSWORD="{{.Mail.Password}}"
`)
if err != nil {
return err
}
f, err := os.Create(dst)
if err != nil {
return err
}
defer f.Close()
return t.Execute(f, cfg)
}