This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
89 lines (73 loc) · 1.83 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
// This file is a part of git.thekondor.net/zvuchno.git (mirror: github.com/thekondor/zvuchno)
package main
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
)
type notificationConfigSection struct {
Timeout uint32
}
type appearanceConfigSection struct {
Width byte `yaml:"width,omitempty"`
Format formatConfigSection `yaml:"format,omitempty"`
Text textConfigSection `yaml:"text,omitempty"`
}
type formatConfigSection struct {
Full string `yaml:"full,omitempty"`
Bar string `yaml:"bar,omitempty"`
}
type textConfigSection struct {
Title string `yaml:"title"`
OnMute string `yaml:"on_mute"`
OnUnmute string `yaml:"on_unmute"`
}
type Config struct {
Notification notificationConfigSection `yaml:"notification,omitempty"`
Appearance appearanceConfigSection `yaml:"appearance,omitempty"`
}
func NewConfig() *Config {
configPath := locateConfigPath()
log.Printf(`Config = %s`, configPath)
self, err := newConfig(configPath)
if nil != err {
log.Printf("W: Failed to load config: %s, default values to be used", err)
}
return self
}
func locateConfigPath() string {
configPath := "${HOME}/.zvuchno.yml"
if _, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok {
configPath = "${XDG_CONFIG_HOME}/zvuchno.yml"
}
return os.ExpandEnv(configPath)
}
func newConfig(path string) (*Config, error) {
self := &Config{
Notification: notificationConfigSection{
Timeout: 1000,
},
Appearance: appearanceConfigSection{
Width: 20,
Format: formatConfigSection{
Full: "{{ .Percent }}% {{ .Bar }}",
Bar: "[=> ]",
},
Text: textConfigSection{
Title: "Volume",
OnMute: "🔇 muted",
OnUnmute: "🔈 unmuted",
},
},
}
file, err := ioutil.ReadFile(path)
if nil != err {
return self, err
}
err = yaml.Unmarshal(file, self)
if nil != err {
return self, err
}
return self, nil
}