-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathconfig_file.go
107 lines (92 loc) · 2.7 KB
/
config_file.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
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/pelletier/go-toml/v2"
)
// Config represents the entire TOML configuration
type ConfigFile struct {
Header string `toml:"header"`
Zones []ConfigFileZone `toml:"zones"`
Keymaps ConfigFileKeymaps `toml:"keymaps"`
}
// Zone represents a single zone entry in the TOML file
type ConfigFileZone struct {
ID string `toml:"id"`
Name string `toml:"name"`
}
// Keymaps represents the key mappings in the TOML file
type ConfigFileKeymaps struct {
PrevMinute []string `toml:"prev_minute"`
NextMinute []string `toml:"next_minute"`
ZeroMinute []string `toml:"zero_minute"`
PrevHour []string `toml:"prev_hour"`
NextHour []string `toml:"next_hour"`
PrevDay []string `toml:"prev_day"`
NextDay []string `toml:"next_day"`
PrevWeek []string `toml:"prev_week"`
NextWeek []string `toml:"next_week"`
PrevLine []string `toml:"prev_line_select"`
NextLine []string `toml:"next_line_select"`
PrevFStyle []string `toml:"prev_format_style"`
NextFStyle []string `toml:"next_format_style"`
PrevZStyle []string `toml:"prev_zone_style"`
NextZStyle []string `toml:"next_zone_style"`
ToggleDate []string `toml:"toggle_date"`
OpenWeb []string `toml:"open_web"`
Now []string `toml:"now"`
Help []string `toml:"help"`
Quit []string `toml:"quit"`
}
func ReadZonesFromFile(now time.Time, zoneConf ConfigFileZone) (*Zone, error) {
name := zoneConf.Name
dbName := zoneConf.ID
loc, err := time.LoadLocation(dbName)
if err != nil {
return nil, fmt.Errorf("looking up zone %s: %w", dbName, err)
}
if name == "" {
name = loc.String()
}
return &Zone{
Loc: loc,
DbName: loc.String(),
Name: name,
}, nil
}
func DefaultConfigFile() (*string, error) {
// Return early if we can't find a home dir.
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
configFilePath := filepath.Join(homeDir, ".config", "tz", "conf.toml")
return &configFilePath, nil
}
func LoadConfigFile(configFilePath string, now time.Time) (*Config, error) {
conf := Config{}
configFile, err := os.ReadFile(configFilePath)
if err != nil {
// Ignore unreadable config file.
logger.Printf("Config file '%s' not found. Skipping...\n", configFilePath)
return &conf, nil
}
var config ConfigFile
if err = toml.Unmarshal(configFile, &config); err != nil {
return nil, fmt.Errorf("Parsing %s: %w\n", configFilePath, err)
}
// Add zones from config file
zones := make([]*Zone, len(config.Zones))
for i, zoneConf := range config.Zones {
zone, err := ReadZonesFromFile(now, zoneConf)
if err != nil {
return nil, err
}
zones[i] = zone
}
conf.Zones = zones
conf.Keymaps = Keymaps(config.Keymaps)
return &conf, nil
}