gte/cmd/cli/command/update.go

80 lines
1.7 KiB
Go
Raw Normal View History

2021-07-29 07:01:24 +02:00
package command
import (
"fmt"
"strings"
"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/internal/task"
"git.ewintr.nl/gte/pkg/msend"
)
type Update struct {
updater *process.Update
}
func NewUpdate(localId int, conf *configuration.Configuration, cmdArgs []string) (*Update, error) {
local, err := storage.NewSqlite(conf.Sqlite())
if err != nil {
return &Update{}, err
}
disp := storage.NewDispatcher(msend.NewSSLSMTP(conf.SMTP()))
fields, err := ParseTaskFieldArgs(cmdArgs)
if err != nil {
return &Update{}, err
}
localTask, err := local.FindByLocalId(localId)
2021-07-29 07:01:24 +02:00
if err != nil {
return &Update{}, err
}
updater := process.NewUpdate(local, disp, localTask.Id, fields)
2021-07-29 07:01:24 +02:00
return &Update{
updater: updater,
}, nil
}
func (u *Update) Do() string {
if err := u.updater.Process(); err != nil {
return format.FormatError(err)
}
return "message sent\n"
}
2021-08-20 13:46:56 +02:00
func ParseTaskFieldArgs(args []string) (task.LocalUpdate, error) {
lu := task.LocalUpdate{}
2021-07-29 07:01:24 +02:00
var action []string
for _, f := range args {
split := strings.SplitN(f, ":", 2)
if len(split) == 2 {
switch split[0] {
case "project":
2021-08-20 13:46:56 +02:00
if lu.Project != "" {
return task.LocalUpdate{}, fmt.Errorf("%w: %s", ErrFieldAlreadyUsed, task.FIELD_PROJECT)
2021-07-29 07:01:24 +02:00
}
2021-08-20 13:46:56 +02:00
lu.Project = split[1]
2021-07-29 07:01:24 +02:00
case "due":
2021-08-20 13:46:56 +02:00
if !lu.Due.IsZero() {
return task.LocalUpdate{}, fmt.Errorf("%w: %s", ErrFieldAlreadyUsed, task.FIELD_DUE)
2021-07-29 07:01:24 +02:00
}
2021-08-20 13:46:56 +02:00
lu.Due = task.NewDateFromString(split[1])
2021-07-29 07:01:24 +02:00
}
} else {
action = append(action, f)
}
}
if len(action) > 0 {
2021-08-20 13:46:56 +02:00
lu.Action = strings.Join(action, " ")
2021-07-29 07:01:24 +02:00
}
2021-08-20 13:46:56 +02:00
return lu, nil
2021-07-29 07:01:24 +02:00
}