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()))
|
2021-08-22 13:29:04 +02:00
|
|
|
update, err := ParseTaskFieldArgs(cmdArgs)
|
2021-07-29 07:01:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return &Update{}, err
|
|
|
|
}
|
2021-08-20 10:01:35 +02:00
|
|
|
localTask, err := local.FindByLocalId(localId)
|
2021-07-29 07:01:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return &Update{}, err
|
|
|
|
}
|
2021-08-22 13:29:04 +02:00
|
|
|
update.ForVersion = localTask.Version
|
2021-07-29 07:01:24 +02:00
|
|
|
|
2021-08-22 13:29:04 +02:00
|
|
|
updater := process.NewUpdate(local, disp, localTask.Id, update)
|
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-22 13:29:04 +02:00
|
|
|
func ParseTaskFieldArgs(args []string) (*task.LocalUpdate, error) {
|
|
|
|
lu := &task.LocalUpdate{}
|
2021-07-29 07:01:24 +02:00
|
|
|
|
2021-08-22 13:29:04 +02:00
|
|
|
action, fields := []string{}, []string{}
|
2021-07-29 07:01:24 +02:00
|
|
|
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 != "" {
|
2021-08-22 13:29:04 +02:00
|
|
|
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-08-22 13:29:04 +02:00
|
|
|
fields = append(fields, task.FIELD_PROJECT)
|
2021-07-29 07:01:24 +02:00
|
|
|
case "due":
|
2021-08-20 13:46:56 +02:00
|
|
|
if !lu.Due.IsZero() {
|
2021-08-22 13:29:04 +02:00
|
|
|
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-08-22 13:29:04 +02:00
|
|
|
fields = append(fields, task.FIELD_DUE)
|
2021-07-29 07:01:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-08-22 13:29:04 +02:00
|
|
|
if len(f) > 0 {
|
|
|
|
action = append(action, f)
|
|
|
|
}
|
2021-07-29 07:01:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(action) > 0 {
|
2021-08-20 13:46:56 +02:00
|
|
|
lu.Action = strings.Join(action, " ")
|
2021-08-22 13:29:04 +02:00
|
|
|
fields = append(fields, task.FIELD_ACTION)
|
2021-07-29 07:01:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 13:29:04 +02:00
|
|
|
lu.Fields = fields
|
|
|
|
|
2021-08-20 13:46:56 +02:00
|
|
|
return lu, nil
|
2021-07-29 07:01:24 +02:00
|
|
|
}
|