diff --git a/cmd/cli/command/command.go b/cmd/cli/command/command.go index c905531..03d9429 100644 --- a/cmd/cli/command/command.go +++ b/cmd/cli/command/command.go @@ -9,7 +9,10 @@ import ( var ( ErrInvalidAmountOfArgs = errors.New("invalid amount of args") + ErrInvalidArg = errors.New("invalid argument") ErrCouldNotFindTask = errors.New("could not find task") + ErrFieldAlreadyUsed = errors.New("field was already used") + ErrUnknownFolder = errors.New("unknown folder") ) type Command interface { @@ -42,6 +45,8 @@ func Parse(args []string, conf *configuration.Configuration) (Command, error) { return NewFolder(conf, cmdArgs) case "add": return NewAdd(conf, cmdArgs) + case "remote": + return parseRemote(conf, cmdArgs) default: return NewEmpty() } @@ -64,3 +69,18 @@ func parseTaskCommand(id int, tArgs []string, conf *configuration.Configuration) return NewShow(id, conf) } } + +func parseRemote(conf *configuration.Configuration, cmdArgs []string) (Command, error) { + switch { + case len(cmdArgs) < 1: + cmd, _ := NewEmpty() + return cmd, ErrInvalidAmountOfArgs + case cmdArgs[0] == "recur": + return NewRecur(conf, cmdArgs[1:]) + case cmdArgs[0] == "inbox": + return NewInbox(conf) + default: + cmd, _ := NewEmpty() + return cmd, ErrInvalidArg + } +} diff --git a/cmd/cli/command/folder.go b/cmd/cli/command/folder.go index 579625b..133b519 100644 --- a/cmd/cli/command/folder.go +++ b/cmd/cli/command/folder.go @@ -1,7 +1,6 @@ package command import ( - "errors" "strings" "git.ewintr.nl/gte/cmd/cli/format" @@ -11,10 +10,6 @@ import ( "git.ewintr.nl/gte/internal/task" ) -var ( - ErrUnknownFolder = errors.New("unknown folder") -) - type Folder struct { lister *process.List } diff --git a/cmd/cli/command/inbox.go b/cmd/cli/command/inbox.go new file mode 100644 index 0000000..e85ac3f --- /dev/null +++ b/cmd/cli/command/inbox.go @@ -0,0 +1,32 @@ +package command + +import ( + "fmt" + + "git.ewintr.nl/gte/cmd/cli/format" + "git.ewintr.nl/gte/internal/configuration" + "git.ewintr.nl/gte/internal/process" + "git.ewintr.nl/gte/internal/storage" + "git.ewintr.nl/gte/pkg/mstore" +) + +type Inbox struct { + inboxer *process.Inbox +} + +func NewInbox(conf *configuration.Configuration) (*Inbox, error) { + remote := storage.NewRemoteRepository(mstore.NewIMAP(conf.IMAP())) + + return &Inbox{ + inboxer: process.NewInbox(remote), + }, nil +} + +func (i *Inbox) Do() string { + res, err := i.inboxer.Process() + if err != nil { + return format.FormatError(err) + } + + return fmt.Sprintf("processed %d tasks\n", res.Count) +} diff --git a/cmd/cli/command/recur.go b/cmd/cli/command/recur.go new file mode 100644 index 0000000..f3e2115 --- /dev/null +++ b/cmd/cli/command/recur.go @@ -0,0 +1,43 @@ +package command + +import ( + "fmt" + "strconv" + + "git.ewintr.nl/gte/cmd/cli/format" + "git.ewintr.nl/gte/internal/configuration" + "git.ewintr.nl/gte/internal/process" + "git.ewintr.nl/gte/internal/storage" + "git.ewintr.nl/gte/pkg/msend" + "git.ewintr.nl/gte/pkg/mstore" +) + +type Recur struct { + recurrer *process.Recur +} + +func NewRecur(conf *configuration.Configuration, cmdArgs []string) (*Recur, error) { + remote := storage.NewRemoteRepository(mstore.NewIMAP(conf.IMAP())) + disp := storage.NewDispatcher(msend.NewSSLSMTP(conf.SMTP())) + + if len(cmdArgs) < 1 { + return &Recur{}, ErrInvalidAmountOfArgs + } + daysAhead, err := strconv.Atoi(cmdArgs[0]) + if err != nil { + return &Recur{}, ErrInvalidArg + } + + return &Recur{ + recurrer: process.NewRecur(remote, disp, daysAhead), + }, nil +} + +func (r *Recur) Do() string { + res, err := r.recurrer.Process() + if err != nil { + return format.FormatError(err) + } + + return fmt.Sprintf("generated %d tasks\n", res.Count) +} diff --git a/cmd/cli/command/update.go b/cmd/cli/command/update.go index 374a909..e497d37 100644 --- a/cmd/cli/command/update.go +++ b/cmd/cli/command/update.go @@ -1,7 +1,6 @@ package command import ( - "errors" "fmt" "strings" @@ -13,10 +12,6 @@ import ( "git.ewintr.nl/gte/pkg/msend" ) -var ( - ErrFieldAlreadyUsed = errors.New("field was already used") -) - type Update struct { updater *process.Update } diff --git a/cmd/generate-recurring/main.go b/cmd/generate-recurring/main.go deleted file mode 100644 index c4a719a..0000000 --- a/cmd/generate-recurring/main.go +++ /dev/null @@ -1,42 +0,0 @@ -package main - -import ( - "flag" - "os" - - "git.ewintr.nl/go-kit/log" - "git.ewintr.nl/gte/internal/configuration" - "git.ewintr.nl/gte/internal/process" - "git.ewintr.nl/gte/internal/storage" - "git.ewintr.nl/gte/pkg/msend" - "git.ewintr.nl/gte/pkg/mstore" -) - -func main() { - logger := log.New(os.Stdout).WithField("cmd", "generate-recurring") - - configPath := flag.String("c", "~/.config/gte/gte.conf", "path to configuration file") - daysAhead := flag.Int("d", 6, "generate for this amount of days from now") - flag.Parse() - - configFile, err := os.Open(*configPath) - if err != nil { - logger.WithErr(err).Error("could not open config file") - os.Exit(1) - } - config := configuration.New(configFile) - - msgStore := mstore.NewIMAP(config.IMAP()) - mailSend := msend.NewSSLSMTP(config.SMTP()) - taskRepo := storage.NewRemoteRepository(msgStore) - taskDisp := storage.NewDispatcher(mailSend) - recur := process.NewRecur(taskRepo, taskDisp, *daysAhead) - - result, err := recur.Process() - if err != nil { - logger.WithErr(err).Error("unable to process recurring") - os.Exit(1) - } - - logger.WithField("result", result).Info("finished generating recurring tasks") -} diff --git a/cmd/process-inbox/main.go b/cmd/process-inbox/main.go deleted file mode 100644 index 4c2cc84..0000000 --- a/cmd/process-inbox/main.go +++ /dev/null @@ -1,35 +0,0 @@ -package main - -import ( - "flag" - "os" - - "git.ewintr.nl/go-kit/log" - "git.ewintr.nl/gte/internal/configuration" - "git.ewintr.nl/gte/internal/process" - "git.ewintr.nl/gte/internal/storage" - "git.ewintr.nl/gte/pkg/mstore" -) - -func main() { - logger := log.New(os.Stdout).WithField("cmd", "process-inbox") - - configPath := flag.String("c", "~/.config/gte/gte.conf", "path to configuration file") - flag.Parse() - - configFile, err := os.Open(*configPath) - if err != nil { - logger.WithErr(err).Error("could not open config file") - os.Exit(1) - } - config := configuration.New(configFile) - msgStore := mstore.NewIMAP(config.IMAP()) - inboxProcessor := process.NewInbox(storage.NewRemoteRepository(msgStore)) - - result, err := inboxProcessor.Process() - if err != nil { - logger.WithErr(err).Error("unable to process inbox") - os.Exit(1) - } - logger.WithField("result", result).Info("finished processing inbox") -}