changed config from yaml to json

This commit is contained in:
Michael 2022-06-21 23:57:42 +01:00
parent 11cac30163
commit 855d45f3fd
Signed by: michael
GPG Key ID: 523BD9EF68BDD44C
5 changed files with 57 additions and 49 deletions

2
.gitignore vendored
View File

@ -1,6 +1,6 @@
gomail
logs
msgs
config.yaml
config.json
go.sum
.vscode

View File

@ -1,45 +1,36 @@
package main
import (
"io/ioutil"
"encoding/json"
"log"
"gopkg.in/yaml.v3"
"os"
)
// Config application configuration
type Config struct {
Path string `yaml:"path"`
Account []Account
Path string `json:"path"`
Account []Account `json:"account"`
Logger struct {
Path string `yaml:"path"`
Mode string `yaml:"mode"`
Level int `yaml:"level"`
}
Path string `json:"path"`
Mode string `json:"mode"`
Level int `json:"level"`
} `json:"logger"`
}
type Account struct {
Addr string `yaml:"addr"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Addr string `json:"addr"`
Username string `json:"username"`
Password string `json:"password"`
}
// NewConfig - create new config instance from file path
func NewConfig(configFile string) (config *Config) {
file, err := ioutil.ReadFile(configFile)
file, err := os.ReadFile(configFile)
if err != nil {
log.Fatalf("Error loading yaml config file %v", err)
log.Fatalf("Error loading config file %v", err)
}
if err = yaml.Unmarshal(file, &config); err != nil {
log.Fatalf("Error Unmarshaling yaml config file %v", err)
}
return config
}
// NewFile create new config instance from file
func NewFile(configFile []byte) (config *Config) {
if err := yaml.Unmarshal(configFile, &config); err != nil {
log.Fatalf("Error Unmarshaling yaml config file %v", err)
if err := json.Unmarshal(file, &config); err != nil {
log.Fatalf("Error Unmarshaling json config file %v", err)
}
return config
}

3
go.mod
View File

@ -5,6 +5,5 @@ go 1.16
require (
github.com/emersion/go-imap v1.2.1
github.com/emersion/go-sasl v0.0.0-20211008083017-0b9dcfb154ac // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
golang.org/x/net v0.0.0-20220621193019-9d032be2e588
)

26
main.go
View File

@ -1,21 +1,20 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"git.0cd.xyz/michael/gomail/mail"
"git.0cd.xyz/michael/gomail/ui"
)
func main() {
cfgFile, mailbox, list := ui()
cfg := NewConfig(*cfgFile)
cmd := ui.New()
cfg := NewConfig(cmd.ConfigFile)
ch := make(chan error)
for _, account := range cfg.Account {
go getmail(account, cfg.Path, *mailbox, *list, ch)
go getmail(account, cfg.Path, cmd.Mailbox, cmd.List, ch)
}
for c := 0; c < len(cfg.Account); c++ {
if <-ch != nil {
@ -44,23 +43,6 @@ func getmail(account Account, ph, mailbox string, list bool, ch chan error) {
ch <- nil
}
func ui() (cfgFile, mailbox *string, list *bool) {
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
home, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
cfgFile = flag.String("cfgFile", home+"/.config/gomail/config.yaml", "path to config file")
mailbox = flag.String("mailbox", "inbox", "mailbox to scan messages")
list = flag.Bool("list", false, "list all mailboxes")
flag.Parse()
return cfgFile, mailbox, list
}
func makePath(paths ...string) {
for _, p := range paths {
if _, err := os.Stat(p); os.IsNotExist(err) {

36
ui/ui.go Normal file
View File

@ -0,0 +1,36 @@
package ui
import (
"flag"
"fmt"
"log"
"os"
)
type Command struct {
ConfigFile string
Mailbox string
List bool
}
func New() (cmd Command) {
flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
home, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
cfgFile := flag.String("cfgFile", home+"/.config/gomail/config.json", "path to config file")
mailbox := flag.String("mailbox", "inbox", "mailbox to scan messages")
list := flag.Bool("list", false, "list all mailboxes")
flag.Parse()
return Command{
ConfigFile: *cfgFile,
Mailbox: *mailbox,
List: *list,
}
}