forked from diamondburned/hrtclicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
43 lines (38 loc) · 1.06 KB
/
config.go
File metadata and controls
43 lines (38 loc) · 1.06 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
package hrtclicker
import (
"encoding/json"
"fmt"
"io"
"os"
"libdb.so/hrtclicker/internal/cfgtypes"
)
// Config contains the configuration for the hrtclicker application.
// See config.json for an example configuration.
type Config struct {
HRT struct {
Type HRTType `json:"type"`
Interval cfgtypes.Duration `json:"interval"`
} `json:"hrt"`
Gotify struct {
Endpoint string `json:"endpoint"`
Token string `json:"token"`
Notification Notification `json:"notification"`
} `json:"gotify"`
}
// ReadJSONConfig reads a Config from the provided io.Reader in JSON format.
func ReadJSONConfig(r io.Reader) (*Config, error) {
var cfg Config
if err := json.NewDecoder(r).Decode(&cfg); err != nil {
return nil, err
}
return &cfg, nil
}
// ReadJSONConfigFile reads a Config from the provided file path as a JSON file.
func ReadJSONConfigFile(path string) (*Config, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open config file: %w", err)
}
defer f.Close()
return ReadJSONConfig(f)
}