From 33c53e5286da8bb1b76afc145a4f94863568dfd8 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Fri, 9 Jul 2021 13:25:50 +0200 Subject: [PATCH] quick new command --- cmd/cli/command/command.go | 9 ++++++++- cmd/cli/command/new.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 cmd/cli/command/new.go diff --git a/cmd/cli/command/command.go b/cmd/cli/command/command.go index 036c387..7216114 100644 --- a/cmd/cli/command/command.go +++ b/cmd/cli/command/command.go @@ -1,11 +1,16 @@ package command import ( + "errors" "fmt" "git.ewintr.nl/gte/internal/configuration" ) +var ( + ErrInvalidAmountOfArgs = errors.New("invalid amount of args") +) + type Command interface { Do() string } @@ -15,7 +20,7 @@ func Parse(args []string, conf *configuration.Configuration) (Command, error) { return NewEmpty() } - cmd, _ := args[0], args[1:] + cmd, cmdArgs := args[0], args[1:] switch cmd { case "sync": return NewSync(conf) @@ -23,6 +28,8 @@ func Parse(args []string, conf *configuration.Configuration) (Command, error) { return NewToday(conf) case "tomorrow": return NewTomorrow(conf) + case "new": + return NewNew(conf, cmdArgs) default: return NewEmpty() } diff --git a/cmd/cli/command/new.go b/cmd/cli/command/new.go new file mode 100644 index 0000000..90111ad --- /dev/null +++ b/cmd/cli/command/new.go @@ -0,0 +1,35 @@ +package command + +import ( + "git.ewintr.nl/gte/internal/configuration" + "git.ewintr.nl/gte/internal/storage" + "git.ewintr.nl/gte/internal/task" + "git.ewintr.nl/gte/pkg/msend" +) + +// New sends an action to the NEW folder so it can be updated to a real task later +type New struct { + disp *storage.Dispatcher + action string +} + +func NewNew(conf *configuration.Configuration, cmdArgs []string) (*New, error) { + if len(cmdArgs) != 1 { + return &New{}, ErrInvalidAmountOfArgs + } + + disp := storage.NewDispatcher(msend.NewSSLSMTP(conf.SMTP())) + + return &New{ + disp: disp, + action: cmdArgs[0], + }, nil +} + +func (n *New) Do() string { + if err := n.disp.Dispatch(&task.Task{Action: n.action}); err != nil { + return FormatError(err) + } + + return "message sent\n" +}