2021-06-25 09:14:27 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-07-09 13:25:50 +02:00
|
|
|
"errors"
|
2021-07-29 07:01:24 +02:00
|
|
|
"fmt"
|
2021-07-27 07:10:01 +02:00
|
|
|
"strconv"
|
2021-06-25 09:14:27 +02:00
|
|
|
|
|
|
|
"git.ewintr.nl/gte/internal/configuration"
|
2021-07-29 07:01:24 +02:00
|
|
|
"git.ewintr.nl/gte/internal/storage"
|
2021-06-25 09:14:27 +02:00
|
|
|
)
|
|
|
|
|
2021-07-09 13:25:50 +02:00
|
|
|
var (
|
|
|
|
ErrInvalidAmountOfArgs = errors.New("invalid amount of args")
|
2021-07-29 07:01:24 +02:00
|
|
|
ErrCouldNotFindTask = errors.New("could not find task")
|
2021-07-09 13:25:50 +02:00
|
|
|
)
|
|
|
|
|
2021-06-25 09:14:27 +02:00
|
|
|
type Command interface {
|
2021-07-09 09:42:44 +02:00
|
|
|
Do() string
|
2021-07-26 06:59:31 +02:00
|
|
|
Cmd() string
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(args []string, conf *configuration.Configuration) (Command, error) {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return NewEmpty()
|
|
|
|
}
|
2021-07-09 13:25:50 +02:00
|
|
|
cmd, cmdArgs := args[0], args[1:]
|
2021-07-27 07:10:01 +02:00
|
|
|
|
|
|
|
id, err := strconv.Atoi(cmd)
|
|
|
|
if err == nil {
|
|
|
|
return parseTaskCommand(id, cmdArgs, conf)
|
|
|
|
}
|
|
|
|
|
2021-06-25 09:14:27 +02:00
|
|
|
switch cmd {
|
|
|
|
case "sync":
|
|
|
|
return NewSync(conf)
|
|
|
|
case "today":
|
|
|
|
return NewToday(conf)
|
2021-07-09 09:51:37 +02:00
|
|
|
case "tomorrow":
|
|
|
|
return NewTomorrow(conf)
|
2021-08-19 07:06:18 +02:00
|
|
|
case "projects":
|
|
|
|
return NewProjects(conf)
|
2021-08-16 07:09:06 +02:00
|
|
|
case "list":
|
|
|
|
return NewList(conf, cmdArgs)
|
2021-08-16 06:54:45 +02:00
|
|
|
case "add":
|
|
|
|
return NewAdd(conf, cmdArgs)
|
2021-06-25 09:14:27 +02:00
|
|
|
default:
|
|
|
|
return NewEmpty()
|
|
|
|
}
|
|
|
|
}
|
2021-07-27 07:10:01 +02:00
|
|
|
|
|
|
|
func parseTaskCommand(id int, tArgs []string, conf *configuration.Configuration) (Command, error) {
|
|
|
|
if len(tArgs) == 0 {
|
|
|
|
return NewShow(id, conf)
|
|
|
|
}
|
|
|
|
|
2021-07-29 07:01:24 +02:00
|
|
|
cmd, cmdArgs := tArgs[0], tArgs[1:]
|
2021-07-28 07:09:39 +02:00
|
|
|
switch cmd {
|
|
|
|
case "done":
|
2021-07-29 07:01:24 +02:00
|
|
|
fallthrough
|
|
|
|
case "del":
|
2021-07-28 07:09:39 +02:00
|
|
|
return NewDone(id, conf)
|
2021-07-29 07:01:24 +02:00
|
|
|
case "mod":
|
|
|
|
return NewUpdate(id, conf, cmdArgs)
|
2021-07-28 07:09:39 +02:00
|
|
|
default:
|
|
|
|
return NewShow(id, conf)
|
|
|
|
}
|
2021-07-27 07:10:01 +02:00
|
|
|
}
|
2021-07-29 07:01:24 +02:00
|
|
|
|
|
|
|
func findId(id int, local storage.LocalRepository) (string, error) {
|
|
|
|
localIds, err := local.LocalIds()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("%w: %v", ErrCouldNotFindTask, err)
|
|
|
|
}
|
|
|
|
var tId string
|
|
|
|
for remoteId, localId := range localIds {
|
|
|
|
if localId == id {
|
|
|
|
tId = remoteId
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tId == "" {
|
|
|
|
return "", ErrCouldNotFindTask
|
|
|
|
}
|
|
|
|
|
|
|
|
return tId, nil
|
|
|
|
}
|