Setup config parsing, commands TODO: Iterating over maildirs
Anirudh Oppiliappan x@icyphox.sh
Wed, 30 Jun 2021 23:07:58 +0530
6 files changed,
109 insertions(+),
8 deletions(-)
A
commands/commands.go
@@ -0,0 +1,22 @@
+package commands + +type Command interface { + Aliases() []string + Execute(cmd string) (interface{}, error) +} + +var Commands = make(map[string]Command) + +func Register(cmd Command) { + for _, alias := range cmd.Aliases() { + Commands[alias] = cmd + } +} + +func ProcessCommands(cmd string) { + for k, v := range Commands { + if cmd == k { + v.Execute(cmd) + } + } +}
A
commands/list.go
@@ -0,0 +1,35 @@
+package commands + +import ( + "fmt" + + "git.icyphox.sh/mx/config" + "github.com/emersion/go-maildir" +) + +func init() { + Register(List{}) +} + +type List struct{} + +func (List) Aliases() []string { + return []string{ + "header", + "he", + "list", + "ls", + } +} + +func (List) Execute(cmd string) (interface{}, error) { + cfg := config.Config + d := maildir.Dir(cfg.Maildir) + keys, _ := d.Keys() + fmt.Println(keys) + for _, k := range keys { + flags, _ := d.Flags(k) + fmt.Println(k, flags) + } + return cmd, nil +}
A
config/config.go
@@ -0,0 +1,38 @@
+package config + +import ( + "fmt" + "os" + "path/filepath" + + "gopkg.in/yaml.v2" +) + +var Config ConfigYaml + +func init() { + Config = *ParseConfig() +} + +type ConfigYaml struct { + Maildir string `yaml:"maildir"` +} + +func ParseConfig() *ConfigYaml { + cfg := ConfigYaml{} + hd, _ := os.UserHomeDir() + + // ~/.config/mx/mx.yaml + configPath := filepath.Join(hd, ".config", "mx", "mx.yaml") + + cfile, err := os.ReadFile(configPath) + if err != nil { + fmt.Printf("error: %+v\n", err) + return nil + } + if err = yaml.Unmarshal(cfile, &cfg); err != nil { + fmt.Printf("error: %+v\n", err) + return nil + } + return &cfg +}
M
go.sum
→
go.sum
@@ -1,4 +1,10 @@
+github.com/emersion/go-maildir v0.2.0 h1:fC4+UVGl8GcQGbFF7AWab2JMf4VbKz+bMNv07xxhzs8= +github.com/emersion/go-maildir v0.2.0/go.mod h1:I2j27lND/SRLgxROe50Vam81MSaqPFvJ0OHNnDZ7n84= github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4= github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/peterh/liner v1.2.1 h1:O4BlKaq/LWu6VRWmol4ByWfzx6MfXc5Op5HETyIy5yg= github.com/peterh/liner v1.2.1/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
M
main.go
→
main.go
@@ -7,17 +7,13 @@ "os"
"path/filepath" "strings" + "git.icyphox.sh/mx/commands" "github.com/peterh/liner" ) var ( hd, _ = os.UserHomeDir() histfile = filepath.Join(hd, ".mx.hist") - commands = []string{ - "some", - "commands", - "here", - } ) func main() {@@ -27,7 +23,7 @@
line.SetCtrlCAborts(true) line.SetCompleter(func(line string) (c []string) { - for _, n := range commands { + for n, _ := range commands.Commands { if strings.HasPrefix(n, strings.ToLower(line)) { c = append(c, n) }@@ -43,7 +39,7 @@
prompt := "" for { if cmd, err := line.Prompt(prompt + " > "); err == nil { - // processCmd(cmd) + commands.ProcessCommands(cmd) line.AppendHistory(cmd) prompt = cmd } else if err == liner.ErrPromptAborted || err == io.EOF {