This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
103 lines (89 loc) · 2.69 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
package lassie
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
)
const (
// DefaultAddr is the default address of the Congress API. You normally won't
// have to change this.
DefaultAddr = "https://api.lora.telenor.io"
// ConfigFile is the default name for the config file. The configuration
// file is a plain text file that contains the default Lassie configuration.
// The configuration file is expected to be at the current home directory.
ConfigFile = ".lassie"
// AddressEnvironmentVariable is the name of the environment variable that
// can be used to override the address set in the configuration file.
// If the environment variable isn't set (or is empty) the configuration
// file settings will be used.
AddressEnvironmentVariable = "LASSIE_ADDRESS"
// TokenEnvironmentVariable is the name of the environment variable that
// can be used to override the token set in the configuration file.
TokenEnvironmentVariable = "LASSIE_TOKEN"
)
// These are the configuration file directives.
const (
addressKey = "address"
tokenKey = "token"
)
// Return both address and token from configuration file. The file name is
// for testing purposes; use the ConfigFile constant when calling the functino.
func addressTokenFromConfig(filename string) (string, string) {
address, token := readConfig(getFullPath(filename))
envAddress := os.Getenv(AddressEnvironmentVariable)
if envAddress != "" {
address = envAddress
}
envToken := os.Getenv(TokenEnvironmentVariable)
if envToken != "" {
token = envToken
}
return address, token
}
func getFullPath(filename string) string {
usr, err := user.Current()
if err != nil {
return ""
}
return filepath.Join(usr.HomeDir, filename)
}
// readConfig reads the config file and returns the address and token
// settings from the file.
func readConfig(filename string) (string, string) {
address := DefaultAddr
token := ""
buf, err := ioutil.ReadFile(filename)
if err != nil {
return address, token
}
scanner := bufio.NewScanner(bytes.NewReader(buf))
scanner.Split(bufio.ScanLines)
lineno := 0
for scanner.Scan() {
lineno++
line := strings.ToLower(scanner.Text())
if len(line) == 0 || line[0] == '#' {
// ignore comments and empty lines
continue
}
words := strings.Split(scanner.Text(), "=")
if len(words) != 2 {
fmt.Printf("Not a key value expression on line %d in %s: %s\n", lineno, filename, scanner.Text())
continue
}
switch words[0] {
case addressKey:
address = strings.TrimSpace(words[1])
case tokenKey:
token = strings.TrimSpace(words[1])
default:
fmt.Printf("Unknown keyword on line %d in %s: %s\n", lineno, filename, scanner.Text())
}
}
return address, token
}