/
context.go
78 lines (60 loc) · 1.59 KB
/
context.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
package config
import (
libcontext "context"
"os"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
// Context represents all the values stored in the configuration.
type Context interface {
libcontext.Context
// FS returns the underlying filesystem to read/write from.
FS() afero.Fs
// Config returns the entire configuration as a struct.
Config() Global
// Accounts are all of the calendar accounts to sync.
Accounts() []Account
// CalendarDir is the path to the base directory to store calendars in.
CalendarDir() string
// TokenDir is the path to store account tokens
TokenDir() string
}
type context struct {
libcontext.Context
fs afero.Fs
}
// NewContext returns a new configuration context.
func NewContext() Context {
return context{libcontext.Background(), afero.NewOsFs()}
}
// FS returns the configured filesystem.
func (ctx context) FS() afero.Fs {
return ctx.fs
}
// Accounts satisiefs the Context interface
func (context) Config() Global {
var cfg Global
if err := viper.Unmarshal(&cfg); err != nil {
panic(err)
}
return cfg
}
// Accounts satisiefs the Context interface
func (context) Accounts() []Account {
var accounts []Account
if err := viper.UnmarshalKey("accounts", &accounts); err != nil {
panic(err)
}
return accounts
}
// CalendarDir is the directory to put calendars in
func (ctx context) CalendarDir() string {
return ctx.path("calendarDir")
}
// TokenDir is the directory to put tokens in
func (ctx context) TokenDir() string {
return ctx.path("tokenDir")
}
func (context) path(key string) string {
return os.ExpandEnv(viper.GetString(key))
}