-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
114 lines (93 loc) · 1.88 KB
/
config.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
package main
import (
"fmt"
"log"
"os"
"github.com/BurntSushi/toml"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
const (
Debug = "debug"
Release = "release"
)
type Config struct {
Mode string
Site struct {
Name string
Icon string
}
Server struct {
Base string
Port int
}
Database struct {
Host string
Name string
User string
Password string
}
Password string `toml:"-"`
}
func parseYN(str string) bool {
switch str {
case "y", "Y":
return true
default:
return false
}
}
func setupAdminPassword() {
var c Config
c = loadConfig()
fmt.Print("admin password: ")
fmt.Scanln(&c.Password)
db, err := initDB(c)
if err != nil {
panic(err)
}
setupDB(c, db)
}
// generate default configure
func genDefaultConf() {
var c Config
c.Server.Base = "http://localhost:8080"
c.Server.Port = 8080
c.Mode = Release
c.Site.Name = "Fever Pass"
c.Site.Icon = "/static/img/icon.png"
c.Database.Host = "localhost"
c.Database.Name = "fever_pass"
c.Database.User = "fever_pass_user"
writeConfig(c, ConfPath)
}
func writeConfig(c Config, path string) {
os.Remove(path)
file, err := os.Create(path)
if err != nil {
panic(err)
}
defer file.Close()
enc := toml.NewEncoder(file)
err = enc.Encode(c)
if err != nil {
panic(err)
}
fmt.Println("Configurations has been generated at", path)
}
func loadConfig() (c Config) {
if _, err := toml.DecodeFile(ConfPath, &c); err != nil {
log.Fatalln("No configuration file, please use -g to generate configure.")
}
return
}
func createDatabaseCode(c Config) {
fmt.Println("Copy the following code to sql.")
fmt.Printf(`
CREATE DATABASE IF NOT EXISTS %[1]s ;
DROP USER IF EXISTS '%[2]s'@'localhost';
FLUSH PRIVILEGES;
CREATE USER '%[2]s'@'localhost' IDENTIFIED BY '%[3]s';
GRANT ALL PRIVILEGES ON %[1]s . * TO '%[2]s'@'localhost';
FLUSH PRIVILEGES;
`, c.Database.Name, c.Database.User, c.Database.Password)
}