-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.go
More file actions
83 lines (71 loc) · 1.81 KB
/
config.go
File metadata and controls
83 lines (71 loc) · 1.81 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
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
package main
import (
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
// Config is the top-level configuration for Lattice.
type Config struct {
// Columns is the number of columns in the grid layout (default: 2).
Columns int `yaml:"columns"`
// Modules lists which modules to display, in order.
// If empty, a sensible default set is used.
Modules []ModuleConfig `yaml:"modules"`
}
// ModuleConfig configures a single module instance.
type ModuleConfig struct {
Type string `yaml:"type"`
Config map[string]string `yaml:"config,omitempty"`
}
// DefaultConfig returns the default configuration when no config file exists.
func DefaultConfig() Config {
return Config{
Columns: 2,
Modules: []ModuleConfig{
{Type: "greeting"},
{Type: "clock"},
{Type: "system"},
{Type: "github"},
{Type: "weather"},
{Type: "uptime"},
},
}
}
// LoadConfig reads the config from ~/.config/lattice/config.yaml.
// Falls back to DefaultConfig if the file doesn't exist.
func LoadConfig() Config {
home, err := os.UserHomeDir()
if err != nil {
return DefaultConfig()
}
path := filepath.Join(home, ".config", "lattice", "config.yaml")
data, err := os.ReadFile(path)
if err != nil {
return DefaultConfig()
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return DefaultConfig()
}
if cfg.Columns < 1 {
cfg.Columns = 2
}
if len(cfg.Modules) == 0 {
cfg.Modules = DefaultConfig().Modules
}
return cfg
}
// Get retrieves a config value, falling back to an environment variable,
// then to a default. This lets users put secrets in env vars instead of
// the config file.
func (mc ModuleConfig) Get(key, envVar, fallback string) string {
if v, ok := mc.Config[key]; ok && v != "" {
return v
}
if envVar != "" {
if v := os.Getenv(envVar); v != "" {
return v
}
}
return fallback
}