quick new command

This commit is contained in:
Erik Winter 2021-07-09 13:25:50 +02:00
parent be904187e1
commit 33c53e5286
2 changed files with 43 additions and 1 deletions

View File

@ -1,11 +1,16 @@
package command package command
import ( import (
"errors"
"fmt" "fmt"
"git.ewintr.nl/gte/internal/configuration" "git.ewintr.nl/gte/internal/configuration"
) )
var (
ErrInvalidAmountOfArgs = errors.New("invalid amount of args")
)
type Command interface { type Command interface {
Do() string Do() string
} }
@ -15,7 +20,7 @@ func Parse(args []string, conf *configuration.Configuration) (Command, error) {
return NewEmpty() return NewEmpty()
} }
cmd, _ := args[0], args[1:] cmd, cmdArgs := args[0], args[1:]
switch cmd { switch cmd {
case "sync": case "sync":
return NewSync(conf) return NewSync(conf)
@ -23,6 +28,8 @@ func Parse(args []string, conf *configuration.Configuration) (Command, error) {
return NewToday(conf) return NewToday(conf)
case "tomorrow": case "tomorrow":
return NewTomorrow(conf) return NewTomorrow(conf)
case "new":
return NewNew(conf, cmdArgs)
default: default:
return NewEmpty() return NewEmpty()
} }

35
cmd/cli/command/new.go Normal file
View File

@ -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"
}