build: PKGBUILD added
[zvuchno.git] / config.go
1 // This file is a part of git.thekondor.net/zvuchno.git (mirror: github.com/thekondor/zvuchno)
2
3 package main
4
5 import (
6         "gopkg.in/yaml.v2"
7         "io/ioutil"
8         "log"
9         "os"
10 )
11
12 type notificationConfigSection struct {
13         Timeout uint32
14 }
15
16 type appearanceConfigSection struct {
17         Width  byte                `yaml:"width,omitempty"`
18         Format formatConfigSection `yaml:"format,omitempty"`
19         Text   textConfigSection   `yaml:"text,omitempty"`
20 }
21
22 type formatConfigSection struct {
23         Full string `yaml:"full,omitempty"`
24         Bar  string `yaml:"bar,omitempty"`
25 }
26
27 type textConfigSection struct {
28         Title    string `yaml:"title"`
29         OnMute   string `yaml:"on_mute"`
30         OnUnmute string `yaml:"on_unmute"`
31 }
32
33 type Config struct {
34         Notification notificationConfigSection `yaml:"notification,omitempty"`
35         Appearance   appearanceConfigSection   `yaml:"appearance,omitempty"`
36 }
37
38 func NewConfig() *Config {
39         configPath := locateConfigPath()
40         log.Printf(`Config = %s`, configPath)
41
42         self, err := newConfig(configPath)
43         if nil != err {
44                 log.Printf("W: Failed to load config: %s, default values to be used", err)
45         }
46
47         return self
48 }
49
50 func locateConfigPath() string {
51         configPath := "${HOME}/.zvuchno.yml"
52         if _, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok {
53                 configPath = "${XDG_CONFIG_HOME}/zvuchno.yml"
54         }
55
56         return os.ExpandEnv(configPath)
57 }
58
59 func newConfig(path string) (*Config, error) {
60         self := &Config{
61                 Notification: notificationConfigSection{
62                         Timeout: 1000,
63                 },
64                 Appearance: appearanceConfigSection{
65                         Width: 20,
66                         Format: formatConfigSection{
67                                 Full: "{{ .Percent }}% {{ .Bar }}",
68                                 Bar:  "[=> ]",
69                         },
70                         Text: textConfigSection{
71                                 Title:    "Volume",
72                                 OnMute:   "🔇 muted",
73                                 OnUnmute: "🔈 unmuted",
74                         },
75                 },
76         }
77
78         file, err := ioutil.ReadFile(path)
79         if nil != err {
80                 return self, err
81         }
82
83         err = yaml.Unmarshal(file, self)
84         if nil != err {
85                 return self, err
86         }
87
88         return self, nil
89 }