about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorPedro L. Ramos <pedrolorgaramos@tecnico.ulisboa.pt>2019-07-13 18:42:22 +0100
committerDrew DeVault <sir@cmpwn.com>2019-07-15 09:42:03 -0400
commitd85f671bdf90dbdd725db88e5d6970630e36f9f1 (patch)
treef4cb94081d12087cc6de04dacd8a44157ccab2bb
parent9bf1a0418ba7fa817c4704d363bc4350ef883b59 (diff)
downloadaerc-d85f671bdf90dbdd725db88e5d6970630e36f9f1.tar.gz
71: Allow user to change config options at runtime
There is a LoadConf and a LoadConfFromFile.
LoadConfFromFile reads the iniFile into memory and and calls
LoadConf, which executes the old parsing commands from
LoadConf (old func).

The remaining of the LoadConfFromFile is the same as the old OldConf.
-rw-r--r--aerc.go2
-rw-r--r--commands/set.go69
-rw-r--r--config/config.go109
3 files changed, 129 insertions, 51 deletions
diff --git a/aerc.go b/aerc.go
index a248e18..dafdd22 100644
--- a/aerc.go
+++ b/aerc.go
@@ -121,7 +121,7 @@ func main() {
 	logger = log.New(logOut, "", log.LstdFlags)
 	logger.Println("Starting up aerc")
 
-	conf, err := config.LoadConfig(nil, ShareDir)
+	conf, err := config.LoadConfigFromFile(nil, ShareDir)
 	if err != nil {
 		fmt.Printf("Failed to load config: %v\n", err)
 		os.Exit(1)
diff --git a/commands/set.go b/commands/set.go
new file mode 100644
index 0000000..f5366ff
--- /dev/null
+++ b/commands/set.go
@@ -0,0 +1,69 @@
+package commands
+
+import (
+	"errors"
+	"strings"
+
+	"git.sr.ht/~sircmpwn/aerc/widgets"
+
+	"github.com/go-ini/ini"
+)
+
+type Set struct{}
+
+func setUsage() string {
+	return "set <category>.<option> <value>"
+}
+
+func init() {
+	register(Set{})
+}
+
+func (_ Set) Aliases() []string {
+	return []string{"set"}
+
+}
+
+func (_ Set) Complete(aerc *widgets.Aerc, args []string) []string {
+	return nil
+}
+
+func SetCore(aerc *widgets.Aerc, args []string) error {
+	if len(args) != 3 {
+		return errors.New("Usage: " + setUsage())
+	}
+
+	config := aerc.Config()
+
+	parameters := strings.Split(args[1], ".")
+
+	if len(parameters) != 2 {
+		return errors.New("Usage: " + setUsage())
+	}
+
+	category := parameters[0]
+	option := parameters[1]
+	value := args[2]
+
+	new_file := ini.Empty()
+
+	section, err := new_file.NewSection(category)
+
+	if err != nil {
+		return nil
+	}
+
+	if _, err := section.NewKey(option, value); err != nil {
+		return err
+	}
+
+	if err := config.LoadConfig(new_file); err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func (_ Set) Execute(aerc *widgets.Aerc, args []string) error {
+	return SetCore(aerc, args)
+}
diff --git a/config/config.go b/config/config.go
index d85c3c8..89d2b49 100644
--- a/config/config.go
+++ b/config/config.go
@@ -222,7 +222,61 @@ func installTemplate(root, sharedir, name string) error {
 	return nil
 }
 
-func LoadConfig(root *string, sharedir string) (*AercConfig, error) {
+func (config *AercConfig) LoadConfig(file *ini.File) error {
+	if filters, err := file.GetSection("filters"); err == nil {
+		// TODO: Parse the filter more finely, e.g. parse the regex
+		for _, match := range filters.KeyStrings() {
+			cmd := filters.KeysHash()[match]
+			filter := FilterConfig{
+				Command: cmd,
+				Filter:  match,
+			}
+			if strings.Contains(match, ",~") {
+				filter.FilterType = FILTER_HEADER
+				header := filter.Filter[:strings.Index(filter.Filter, ",")]
+				regex := filter.Filter[strings.Index(filter.Filter, "~")+1:]
+				filter.Header = strings.ToLower(header)
+				filter.Regex, err = regexp.Compile(regex)
+				if err != nil {
+					panic(err)
+				}
+			} else if strings.ContainsRune(match, ',') {
+				filter.FilterType = FILTER_HEADER
+				header := filter.Filter[:strings.Index(filter.Filter, ",")]
+				value := filter.Filter[strings.Index(filter.Filter, ",")+1:]
+				filter.Header = strings.ToLower(header)
+				filter.Regex, err = regexp.Compile(regexp.QuoteMeta(value))
+			} else {
+				filter.FilterType = FILTER_MIMETYPE
+			}
+			config.Filters = append(config.Filters, filter)
+		}
+	}
+	if viewer, err := file.GetSection("viewer"); err == nil {
+		if err := viewer.MapTo(&config.Viewer); err != nil {
+			return err
+		}
+		for key, val := range viewer.KeysHash() {
+			switch key {
+			case "alternatives":
+				config.Viewer.Alternatives = strings.Split(val, ",")
+			}
+		}
+	}
+	if compose, err := file.GetSection("compose"); err == nil {
+		if err := compose.MapTo(&config.Compose); err != nil {
+			return err
+		}
+	}
+	if ui, err := file.GetSection("ui"); err == nil {
+		if err := ui.MapTo(&config.Ui); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func LoadConfigFromFile(root *string, sharedir string) (*AercConfig, error) {
 	if root == nil {
 		_root := path.Join(xdg.ConfigHome(), "aerc")
 		root = &_root
@@ -274,61 +328,16 @@ func LoadConfig(root *string, sharedir string) (*AercConfig, error) {
 	}
 	quit, _ := ParseBinding("<C-q>", ":quit<Enter>")
 	config.Bindings.AccountWizard.Add(quit)
-	if filters, err := file.GetSection("filters"); err == nil {
-		// TODO: Parse the filter more finely, e.g. parse the regex
-		for _, match := range filters.KeyStrings() {
-			cmd := filters.KeysHash()[match]
-			filter := FilterConfig{
-				Command: cmd,
-				Filter:  match,
-			}
-			if strings.Contains(match, ",~") {
-				filter.FilterType = FILTER_HEADER
-				header := filter.Filter[:strings.Index(filter.Filter, ",")]
-				regex := filter.Filter[strings.Index(filter.Filter, "~")+1:]
-				filter.Header = strings.ToLower(header)
-				filter.Regex, err = regexp.Compile(regex)
-				if err != nil {
-					panic(err)
-				}
-			} else if strings.ContainsRune(match, ',') {
-				filter.FilterType = FILTER_HEADER
-				header := filter.Filter[:strings.Index(filter.Filter, ",")]
-				value := filter.Filter[strings.Index(filter.Filter, ",")+1:]
-				filter.Header = strings.ToLower(header)
-				filter.Regex, err = regexp.Compile(regexp.QuoteMeta(value))
-			} else {
-				filter.FilterType = FILTER_MIMETYPE
-			}
-			config.Filters = append(config.Filters, filter)
-		}
-	}
-	if viewer, err := file.GetSection("viewer"); err == nil {
-		if err := viewer.MapTo(&config.Viewer); err != nil {
-			return nil, err
-		}
-		for key, val := range viewer.KeysHash() {
-			switch key {
-			case "alternatives":
-				config.Viewer.Alternatives = strings.Split(val, ",")
-			}
-		}
-	}
-	if compose, err := file.GetSection("compose"); err == nil {
-		if err := compose.MapTo(&config.Compose); err != nil {
-			return nil, err
-		}
-	}
-	if ui, err := file.GetSection("ui"); err == nil {
-		if err := ui.MapTo(&config.Ui); err != nil {
-			return nil, err
-		}
+
+	if err = config.LoadConfig(file); err != nil {
+		return nil, err
 	}
 	if ui, err := file.GetSection("general"); err == nil {
 		if err := ui.MapTo(&config.General); err != nil {
 			return nil, err
 		}
 	}
+
 	accountsPath := path.Join(*root, "accounts.conf")
 	if accounts, err := loadAccountConfig(accountsPath); err != nil {
 		return nil, err
amp;id=848ebc1e6335cd1a34e07662242a367fefbc5229'>^
204dae92 ^

9e751bb8 ^
204dae92 ^




9e751bb8 ^


204dae92 ^

9e751bb8 ^
204dae92 ^




9e751bb8 ^



204dae92 ^

9e751bb8 ^
204dae92 ^




9e751bb8 ^
204dae92 ^

9e751bb8 ^
204dae92 ^




9e751bb8 ^
204dae92 ^

9e751bb8 ^
204dae92 ^




9e751bb8 ^
204dae92 ^

9e751bb8 ^
204dae92 ^

672e3e50 ^


a654e4ec ^
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132