This commit is contained in:
Erik Winter 2024-11-05 07:21:43 +01:00
parent b1734fcfbd
commit 84d2b9eb18
2 changed files with 42 additions and 4 deletions

View File

@ -24,10 +24,14 @@ func NewAddCmd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo stor
}
func (add *AddCmd) Do(args []string) (bool, error) {
if len(args) == 0 || args[0] != "add" {
return false, nil
}
title, flags, err := ParseArgs(args[1:])
if err != nil {
return false, err
}
func Add(localIDRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync, nameStr, onStr, atStr, frStr string) error {
if nameStr == "" {
return fmt.Errorf("%w: name is required", ErrInvalidArg)
}
@ -40,6 +44,14 @@ func Add(localIDRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.
if atStr == "" && frStr == "" {
frStr = "24h"
}
if err := add.Action(title, flags); err != nil {
return false, err
}
return true, nil
}
func (add *AddCmd) Action(nameStr, onStr, atStr, frStr string) error {
startFormat := "2006-01-02"
startStr := onStr

View File

@ -3,6 +3,7 @@ package command
import (
"errors"
"fmt"
"strings"
)
var (
@ -28,5 +29,30 @@ func (cli *CLI) Run(args []string) error {
}
}
return fmt.Errorf("could not find matchin command")
return fmt.Errorf("could not find matching command")
}
func ParseArgs(args []string) (string, map[string]string, error) {
flags := make(map[string]string)
rem := make([]string, 0)
var inRem bool
for i := 0; i < len(args); i++ {
if strings.HasPrefix(args[i], "-") {
inRem = false
if i+1 >= len(args) {
return "", nil, fmt.Errorf("flag wihout value")
}
flags[strings.TrimPrefix(args[i], "-")] = args[i+1]
i++
continue
}
if !inRem && len(rem) > 0 {
return "", nil, fmt.Errorf("two rems")
}
inRem = true
rem = append(rem, args[i])
}
return strings.Join(rem, " "), flags, nil
}