-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
98 lines (92 loc) · 3.25 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
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"text/template"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
func loadConfig() Config {
pflag.String("config", "", "path to config file")
pflag.String("maildir", "", "path to maildir folder")
pflag.String("outputpath", "", "path to output file")
pflag.String("template", "", "output template")
pflag.String("addr-book-cmd", "", "optional command to query addresses from your addressbook")
pflag.Bool("addr-book-add-unmatched", false, "flag to determine if you want unmatched addressbook contacts to be added to the output")
pflag.StringSlice("addresses", []string{}, "comma separated list of your email addresses (regex possible)")
pflag.StringSlice("filters", []string{}, "comma separated list of regexes to filter")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
dir, direrr := os.UserConfigDir()
if direrr != nil {
dir, _ = os.Getwd()
}
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath(dir + "/maildir-rank-addr")
viper.AddConfigPath(".")
dir, direrr = os.UserCacheDir()
if direrr != nil {
dir, _ = os.Getwd()
}
viper.SetDefault("outputpath", dir+"/maildir-rank-addr/addressbook.tsv")
viper.SetDefault("addresses", []string{})
viper.SetDefault("filters", []string{})
viper.SetDefault("template", "{{.Address}}\t{{.Name}}")
configPath, err := pflag.CommandLine.GetString("config")
if configPath != "" && err == nil {
viper.SetConfigFile(configPath)
}
err = viper.ReadInConfig()
if err != nil { // Handle errors reading the config file
if _, ok := err.(viper.ConfigFileNotFoundError); !ok || configPath != "" {
panic(fmt.Errorf("fatal error config file: %w", err))
}
}
if viper.Get("maildir") == "" {
pflag.PrintDefaults()
os.Exit(1)
}
maildir, _ := homedir.Expand(viper.GetString("maildir"))
outputpath, _ := homedir.Expand(viper.GetString("outputpath"))
filterInput := viper.GetStringSlice("filters")
customFilters := make([]*regexp.Regexp, len(filterInput))
for i, filter := range filterInput {
customFilters[i] = regexp.MustCompile(filter)
}
addressesInput := viper.GetStringSlice("addresses")
addresses := make([]*regexp.Regexp, len(addressesInput))
for i, filter := range addressesInput {
addresses[i] = regexp.MustCompile(filter)
}
templateString := viper.GetString("template")
addressbookLookupCommandString := viper.GetString("addr-book-cmd")
addressbookAddUnmatched := viper.GetBool("addr-book-add-unmatched")
var addressbookLookupCommand *exec.Cmd
if addressbookLookupCommandString != "" {
args := strings.Fields(addressbookLookupCommandString)
application, arguments := args[0], args[1:]
addressbookLookupCommand = exec.Command(application, arguments...)
}
if !strings.HasSuffix(templateString, "\n") {
templateString += "\n"
}
tmpl, err := template.New("output").Parse(templateString)
if err != nil {
panic(fmt.Errorf("bad template"))
}
config := Config{
maildir: maildir,
outputpath: outputpath,
addresses: addresses,
template: tmpl,
customFilters: customFilters,
addressbookLookupCommand: addressbookLookupCommand,
addressbookAddUnmatched: addressbookAddUnmatched,
}
return config
}