-
Notifications
You must be signed in to change notification settings - Fork 4
/
confgen.go
77 lines (62 loc) · 1.57 KB
/
confgen.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
package core
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
)
// template used for confgen
const CONF_TEMPLATE = `
[core]
listen = '{{.listen}}'
secret_key = '{{.secret_key}}'
[database]
driver = '{{.driver}}'
dsn = '{{.dsn}}'
[packages]
directory = '{{.packages_dir}}'
[download_stats]
archive_weekly = 4
archive_monthly = 4
`
var tpl *template.Template
func init() {
tpl = template.Must(template.New("test").Parse(CONF_TEMPLATE))
}
/*
NewConfGen returns ConfGen instance
*/
func NewConfGen() *confgen {
return &confgen{}
}
/*
confgen structure
*/
type confgen struct{}
/*
Run runs confgenerator
*/
func (c *confgen) Run() (err error) {
data := map[string]interface{}{
"filename": TerminalGetStringValue("Please enter config filename", "gopypi.conf"),
"listen": TerminalGetStringValue("Please enter listen", "0.0.0.0:80"),
"driver": TerminalGetStringValueChoices("Please enter driver", []string{"postgres"}, "postgres"),
"dsn": TerminalGetStringValue("Please enter database dsn"),
"packages_dir": TerminalGetStringValue("Please enter target packages directory", ".packages"),
"secret_key": fmt.Sprintf("%x", GenerateSalt(CONFGEN_SECRET_KEY_LENGTH)),
}
var buf bytes.Buffer
if err = tpl.Execute(&buf, data); err != nil {
return
}
targetFilename := data["filename"].(string)
// write file
if _, err := os.Stat(targetFilename); os.IsNotExist(err) {
ioutil.WriteFile(targetFilename, []byte(strings.TrimLeft(buf.String(), " ")), 0666)
} else {
return fmt.Errorf("File %v already exists.", targetFilename)
}
return
}