108 lines
2.2 KiB
Go
108 lines
2.2 KiB
Go
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func NewUpdate(main []string, fields map[string]string) (Command, error) {
|
|
return &Update{
|
|
localIDRepo: localIDRepo,
|
|
taskRepo: taskRepo,
|
|
syncRepo: syncRepo,
|
|
argSet: &ArgSet{
|
|
Flags: map[string]Flag{
|
|
FieldTitle: &FlagString{},
|
|
FieldDate: &FlagDate{},
|
|
FieldTime: &FlagTime{},
|
|
FieldDuration: &FlagDuration{},
|
|
FieldRecurrer: &FlagRecurrer{},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (update *Update) Execute(main []string, flags map[string]string) error {
|
|
if len(main) < 2 || main[0] != "update" {
|
|
return ErrWrongCommand
|
|
}
|
|
localID, err := strconv.Atoi(main[1])
|
|
if err != nil {
|
|
return fmt.Errorf("not a local id: %v", main[1])
|
|
}
|
|
update.localID = localID
|
|
main = main[2:]
|
|
|
|
as := update.argSet
|
|
as.Main = strings.Join(main, " ")
|
|
for k := range as.Flags {
|
|
v, ok := flags[k]
|
|
if !ok {
|
|
continue
|
|
}
|
|
if err := as.Set(k, v); err != nil {
|
|
return fmt.Errorf("could not set %s: %v", k, err)
|
|
}
|
|
}
|
|
update.argSet = as
|
|
|
|
return update.do()
|
|
}
|
|
|
|
func (update *Update) do() error {
|
|
as := update.argSet
|
|
var id string
|
|
idMap, err := update.localIDRepo.FindAll()
|
|
if err != nil {
|
|
return fmt.Errorf("could not get local ids: %v", err)
|
|
}
|
|
for tid, lid := range idMap {
|
|
if update.localID == lid {
|
|
id = tid
|
|
}
|
|
}
|
|
if id == "" {
|
|
return fmt.Errorf("could not find local id")
|
|
}
|
|
|
|
tsk, err := update.taskRepo.Find(id)
|
|
if err != nil {
|
|
return fmt.Errorf("could not find task")
|
|
}
|
|
|
|
if as.Main != "" {
|
|
tsk.Title = as.Main
|
|
}
|
|
if as.IsSet(FieldDate) {
|
|
tsk.Date = as.GetDate(FieldDate)
|
|
}
|
|
if as.IsSet(FieldTime) {
|
|
tsk.Time = as.GetTime(FieldTime)
|
|
}
|
|
if as.IsSet(FieldDuration) {
|
|
tsk.Duration = as.GetDuration(FieldDuration)
|
|
}
|
|
if as.IsSet(FieldRecurrer) {
|
|
tsk.Recurrer = as.GetRecurrer(FieldRecurrer)
|
|
}
|
|
|
|
if !tsk.Valid() {
|
|
return fmt.Errorf("task is unvalid")
|
|
}
|
|
|
|
if err := update.taskRepo.Store(tsk); err != nil {
|
|
return fmt.Errorf("could not store task: %v", err)
|
|
}
|
|
|
|
it, err := tsk.Item()
|
|
if err != nil {
|
|
return fmt.Errorf("could not convert task to sync item: %v", err)
|
|
}
|
|
if err := update.syncRepo.Store(it); err != nil {
|
|
return fmt.Errorf("could not store sync item: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|