-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
134 lines (114 loc) · 3.09 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
// I know, "this should be a struct!"
// But flags need to be parsed first, applying conf file values if not set by flags
// Check the parseConfigFile and validateConfigOrDie functions first
// Then try to convince me to make it a struct for no benefit :)
type Config map[string]string
var config Config
func parseFlags() {
// Parse the flags
version := flag.Bool("version", false, "Print the application version and exit")
configFile := flag.String("config-file", "", "Path to config file")
server := flag.String("server", "", "Matrix homeserver url")
token := flag.String("token", "", "Matrix account access token")
room := flag.String("room", "", "Matrix Room ID")
preface := flag.String("preface", "", "Preface the matrix message with arbitrary text (optional)")
flag.Parse()
// If the version flag is set, print the version and exit
if *version {
fmt.Println(Version)
os.Exit(0)
}
// Put them in the config
config["configFile"] = *configFile
config["server"] = *server
config["token"] = *token
config["room"] = *room
config["preface"] = *preface
}
func generateConfig() {
// Prompt user
configFile := ask("Output path for generated config:")
server := ask("Matrix home server (ex. https://matrix.org):")
user := ask("Matrix username:")
password := ask("Matrix password:")
room := ask("Matrix room:")
preface := ask("Message preface:")
// Fetch access_token
token, err := getToken(server, user, password)
if err != nil {
log.Fatal(err)
}
// Generate json
data := map[string]string{
"server": server,
"token": token,
"room": room,
"preface": preface,
}
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Fatal(err)
}
// Print the result
fmt.Printf("\n%s\n\n", string(b))
// Save to file
err = ioutil.WriteFile(configFile, b, 0600)
if err != nil {
log.Fatal(err)
}
// Notify user
fmt.Println("")
fmt.Printf("Saved config to: %s\n", configFile)
}
func getConfig() {
config = make(Config)
// Parse command line arguments. These override config file values.
// We need it parsed before for the --config-file flag
parseFlags()
// Parse config file (if set)
if config["configFile"] != "" {
parseConfigFile(config["configFile"])
}
}
func parseConfigFile(path string) {
var confFile Config
// Read config file
content, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal("Error when opening file: ", err)
}
// Unmarshall the contents
err = json.Unmarshal(content, &confFile)
if err != nil {
log.Fatal("Error trying to parse JSON file: ", err)
}
// Set the values only if they weren't already set by flags
keys := []string{"server", "token", "room", "preface"}
for _, key := range keys {
if config[key] == "" {
config[key] = confFile[key]
}
}
}
func validateConfigOrDie() {
var missing []string
required := []string{"server", "token", "room"}
for _, key := range required {
if config[key] == "" {
missing = append(missing, key)
}
}
if len(missing) > 0 {
log.Fatal("Missing required parameters: ", strings.Join(missing, ", "))
}
}