2019-07-13 19:42:22 +02:00
|
|
|
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{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Set) Aliases() []string {
|
2019-07-13 19:42:22 +02:00
|
|
|
return []string{"set"}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Set) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-07-13 19:42:22 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-31 13:37:46 +02:00
|
|
|
// ensure any ui changes take effect
|
|
|
|
aerc.Invalidate()
|
|
|
|
|
2019-07-13 19:42:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Set) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-07-13 19:42:22 +02:00
|
|
|
return SetCore(aerc, args)
|
|
|
|
}
|