-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
86 lines (70 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
package main
import (
"encoding/json"
"errors"
"flag"
"io/ioutil"
"os"
"os/user"
"strings"
"time"
)
var configPath = flag.String("configPath", "~/.slingdvr.json", "path to config")
var recordedPath = flag.String("recordedPath", "~/.slingdvr-recorded.json", "path to recorded")
type Config struct {
ReceiverId string `json:"receiverId"`
CorrelationId string `json:"correlationId"`
Titles []string `json:"titles"`
EarliestShowingTime time.Time `json:"earliestShowingTime"`
RecStartTime time.Time `json:"recStartTime"`
RecEndTime time.Time `json:"recEndTime"`
RecordingDir string `json:"recordingDir"`
}
var config Config
var rawConfig map[string]interface{}
func ReadConfig() (err error) {
*configPath = expandConfigPath(*configPath)
file, err := os.Open(*configPath)
if err != nil {
return
}
// Overwrite existing config
config = Config{}
if err = json.NewDecoder(file).Decode(&config); err != nil {
return
}
// Overwrite existing config
rawConfig = make(map[string]interface{})
file.Seek(0, 0)
if err = json.NewDecoder(file).Decode(&rawConfig); err != nil {
return
}
return
}
func ReadRecorded() ([]string, error) {
*recordedPath = expandConfigPath(*recordedPath)
b, err := ioutil.ReadFile(*recordedPath)
if err != nil {
return nil, errors.New("Cannot read recorded: " + err.Error())
}
var o []string
if err = json.Unmarshal(b, &o); err != nil {
return nil, errors.New("Cannot unmarshal recorded: " + err.Error())
}
return o, nil
}
func WriteRecorded(r []string) (err error) {
b, err := json.Marshal(r)
if err != nil {
return errors.New("Cannot marshal recorded: " + err.Error())
}
return ioutil.WriteFile(*recordedPath, b, 0600)
}
func expandConfigPath(p string) string {
if p[:2] == "~/" {
usr, _ := user.Current()
dir := usr.HomeDir
return strings.Replace(p, "~", dir, 1)
}
return p
}