-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
105 lines (83 loc) · 2.23 KB
/
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
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
package stickers
import (
"fmt"
"strings"
"gopkg.in/yaml.v3"
)
// instance variable
var config = &Config{}
// Labels is a collection of Label.
type Labels []*Label
// LocalFilters is a collection of Filter.
type ConfigFilters []ConfigFilter
// ConfigFilter represents a gmail filter.
type ConfigFilter struct {
Action string `yaml:"action"`
Label string `yaml:"label"`
Query *FilterQuery `yaml:"query,omitempty"`
}
// FilterQuery is a query object for filters.
type FilterQuery struct {
From *[]string `yaml:"from,omitempty"`
NotFrom *[]string `yaml:"not_from,omitempty"`
To *[]string `yaml:"to,omitempty"`
NotTo *[]string `yaml:"not_to,omitempty"`
}
// ToString will convert a query string to a compatible filter.
func (f *FilterQuery) ToString() string {
var q string
if f.From != nil {
q = fmt.Sprintf("from:{%s}", strings.Join(*f.From, " "))
}
if f.NotFrom != nil {
q += fmt.Sprintf("-from:{%s}", strings.Join(*f.NotFrom, " "))
}
if f.To != nil {
q += fmt.Sprintf("to:{%s}", strings.Join(*f.To, " "))
}
if f.NotTo != nil {
q += fmt.Sprintf("-to:{%s}", strings.Join(*f.NotTo, " "))
}
return q
}
// Config is a standard config struct.
type Config struct {
Labels Labels `yaml:"labels"`
Filters ConfigFilters `yaml:"filters"`
Domain string `yaml:"domain"`
Impersonate string `yaml:"impersonate"`
}
// ErrMissingConfig is used to
type ErrMissingConfig struct{}
// Error
func (e ErrMissingConfig) Error() string {
return "config could not be found"
}
// LoadConfig will read the configuration from yaml.
func LoadConfig(b []byte) error {
err := yaml.Unmarshal(b, &config)
if err != nil {
return err
}
return nil
}
// GetConfig will return the active config.
func GetConfig() *Config {
return config
}
// GetDomain returns a string of the configured domain.
func GetDomain() string {
return config.Domain
}
// GetImpersonation returns a string of the user to impersonate.
func GetImpersonation() string {
return config.Impersonate
}
// GetLabels returns a collection of labels to configure.
func GetLabels() Labels {
return config.Labels
}
// GetFilters returns a collection of filters to configure.
func GetFilters() ConfigFilters {
return config.Filters
}