-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
50 lines (39 loc) · 959 Bytes
/
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
package newspaper
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
const DefaultConfigPath string = "/.config/newspaper.json"
type Config struct {
Root string `json:"root"`
FileExtension string `json:"file_extension"`
RemindersFirst bool `json:"reminders_first"`
YearFromat string `json:"year_format"`
MonthFromat string `json:"month_format"`
DayFormat string `json:"day_format"`
StorageFilePath string `json:"storage_file_path"`
}
func (c *Config) TimeFormat() string {
return filepath.Join(c.YearFromat, c.MonthFromat, c.DayFormat)
}
func (c *Config) FileFormat() string {
return fmt.Sprintf(
"%v.%v",
c.TimeFormat(),
c.FileExtension,
)
}
func LoadConfig(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
cfg := new(Config)
if err := json.NewDecoder(file).Decode(cfg); err != nil {
return nil, err
}
cfg.Root = filepath.Clean(cfg.Root)
return cfg, nil
}