Repository navigation
Expand file tree
/
Copy pathconfig.go
More file actions
84 lines (72 loc) · 1.56 KB
/
Copy pathconfig.go
File metadata and controls
84 lines (72 loc) · 1.56 KB
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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"log"
)
type Config struct {
authToken string
defaultRoomId string
defaultRoomName string
test bool
}
func getConfigPath() string {
homedir := os.Getenv("HOME")
if homedir == "" {
exitErr(fmt.Errorf("$HOME not set"))
}
return homedir + "/.hipcat"
}
func readConfig() *Config {
config := &Config{
authToken: "",
defaultRoomId: "",
test: false,
}
lines := readLines(getConfigPath())
for _, line := range lines {
s := strings.Split(line, "=")
if len(s) != 2 {
exitErr(fmt.Errorf("failed to parse config at: %s\n", line))
}
key := strip(s[0])
switch key {
case "auth_token":
config.authToken = strip(s[1])
case "default_room_name":
config.defaultRoomName = strip(s[1])
case "default_room_id":
config.defaultRoomId = strip(s[1])
case "test":
if strip(s[1]) == "true" {
config.test = true
} else if strip(s[1]) == "false" {
config.test = false
} else {
output(fmt.Sprintf("unrecognized value for 'test' in config: %s\n", strip(s[1])))
log.Println("")
}
default:
output(fmt.Sprintf("unrecognized config parameter: %s\n", line))
}
}
return config
}
func strip(s string) string {
return strings.Replace(s, " ", "", -1)
}
func readLines(path string) []string {
var lines []string
file, err := os.Open(path)
failOnError(err, "unable to read config", true)
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if scanner.Text() != "" {
lines = append(lines, scanner.Text())
}
}
return lines
}