-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
173 lines (144 loc) · 4.09 KB
/
options.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// This file is part of autosr.
//
// autosr is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// autosr is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with autosr. If not, see <https://www.gnu.org/licenses/>.
package options
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var m sync.RWMutex
// Get an option
func Get(k string) string {
m.RLock()
defer m.RUnlock()
return v.GetString(k)
}
// GetDuration option
func GetDuration(k string) time.Duration {
m.RLock()
defer m.RUnlock()
return v.GetDuration(k)
}
const (
// Filename for config file
Filename = "autosr"
// Format for config file
Format = "toml"
defaultSavePath = "autosr"
configPathWindows = `AppData\Roaming\autosr\`
configPathUnix = ".config/autosr/"
defaultUserAgent = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36`
defaultStreamDownloader = `streamlink --http-header User-Agent={{UserAgent}} -o {{SavePath}} {{StreamURL}} best`
defaultListenAddr = ":4846"
defaultPollRate = 120 * time.Second
defaultSelectFGColor = "blue"
defaultSelectBGColor = "white"
)
// ConfigPath is the path where track list and config file are kept
var ConfigPath string
// EventHooks contains the names of valid event hooks
var EventHooks = []string{
"begin-snipe",
"begin-save",
"end-save",
"reload",
}
var v = viper.New()
func init() {
home, err := os.UserHomeDir()
if err != nil {
fmt.Println("options.init:", err)
os.Exit(1)
}
// set defaults
v.SetDefault("check_every", defaultPollRate)
v.SetDefault("user_agent", defaultUserAgent)
v.SetDefault("download_with", defaultStreamDownloader)
v.SetDefault("listen_on", defaultListenAddr)
v.SetDefault("select_fg_color", defaultSelectFGColor)
v.SetDefault("select_bg_color", defaultSelectBGColor)
v.SetConfigType(Format)
v.SetConfigName(Filename)
var configPath string
if runtime.GOOS == "windows" {
configPath = filepath.Join(home, configPathWindows)
} else {
configPath = filepath.Join(home, configPathUnix)
}
if err != nil {
fmt.Println("options.init:", err)
os.Exit(1)
}
savePath := filepath.Join(home, defaultSavePath)
ConfigPath = configPath
if err := os.MkdirAll(ConfigPath, 0700); err != nil {
fmt.Println("error:", err)
return
}
hooksPath := filepath.Join(ConfigPath, "hooks")
if err := os.MkdirAll(hooksPath, 0700); err != nil {
fmt.Println("error:", err)
return
}
// make each hook directory
for _, event := range EventHooks {
p := filepath.Join(hooksPath, event)
if err := os.MkdirAll(p, 0700); err != nil {
fmt.Println("error:", err)
return
}
}
v.SetDefault("save_to", savePath)
v.AddConfigPath(configPath)
if err := v.ReadInConfig(); err != nil {
p := filepath.Join(configPath, Filename+"."+Format)
if err := v.WriteConfigAs(p); err != nil {
panic(err)
}
fmt.Println("[ok] wrote new config file")
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
if ok, err := AreValid(); !ok {
if err == errInvalidPollRate {
v.Set("check_every", 1*time.Minute)
}
}
})
}
var errInvalidPollRate = fmt.Errorf("error: time must be greater than 30s")
// AreValid is true if the options are valid
func AreValid() (ok bool, err error) {
if v.GetDuration("check_every") < 30*time.Second {
err = errInvalidPollRate
return
}
downloader := v.GetString("download_with")
sp := strings.Split(downloader, " ")
app := sp[0]
_, err = exec.LookPath(app)
if err != nil {
err = fmt.Errorf("error: could not find downloader: %s", err)
return
}
return true, nil
}