From 84d2b9eb1818334b70c9e24968eaec25b2e258ae Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Tue, 5 Nov 2024 07:21:43 +0100 Subject: [PATCH] wip --- plan/command/add.go | 18 +++++++++++++++--- plan/command/command.go | 28 +++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/plan/command/add.go b/plan/command/add.go index 0e0f330..d97b4cd 100644 --- a/plan/command/add.go +++ b/plan/command/add.go @@ -24,10 +24,14 @@ func NewAddCmd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo stor } func (add *AddCmd) Do(args []string) (bool, error) { - return false, nil -} + 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 diff --git a/plan/command/command.go b/plan/command/command.go index 5199ac9..0b7b3d3 100644 --- a/plan/command/command.go +++ b/plan/command/command.go @@ -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 }