This commit is contained in:
Erik Winter 2024-11-04 09:49:41 +01:00
parent f19567f8ff
commit b1734fcfbd
2 changed files with 29 additions and 77 deletions

View File

@ -1,54 +1,30 @@
package command package command
import ( import (
"errors"
"fmt" "fmt"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/urfave/cli/v2"
"go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage" "go-mod.ewintr.nl/planner/plan/storage"
) )
var ( type AddCmd struct {
ErrInvalidArg = errors.New("invalid argument") localIDRepo storage.LocalID
) eventRepo storage.Event
syncRepo storage.Sync
var AddCmd = &Command{
Name: "add",
Description: "Add a new event",
Flags: []*Flag{
&Flag{
Name: "name",
Short: "n",
Description: "The event that will happen",
Required: true,
},
&Flag{
Name: "on",
Short: "o",
Description: "The date, in YYYY-MM-DD format",
Required: true,
},
&Flag{
Name: "at",
Short: "a",
Description: "The time, in HH:MM format. If omitted, the event will last the whole day",
},
&Flag{
Name: "for",
Short: "f",
Description: "The duration, in show format (e.g. 1h30m)",
},
},
} }
func NewAddCmd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync) *cli.Command { func NewAddCmd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync) Command {
AddCmd.Action = func(cCtx *cli.Context) error { return &AddCmd{
return Add(localRepo, eventRepo, syncRepo, cCtx.String("name"), cCtx.String("on"), cCtx.String("at"), cCtx.String("for")) localIDRepo: localRepo,
eventRepo: eventRepo,
syncRepo: syncRepo,
} }
return AddCmd }
func (add *AddCmd) Do(args []string) (bool, error) {
return false, nil
} }
func Add(localIDRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync, nameStr, onStr, atStr, frStr string) error { func Add(localIDRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync, nameStr, onStr, atStr, frStr string) error {

View File

@ -1,56 +1,32 @@
package command package command
import ( import (
"strconv" "errors"
"fmt"
) )
type CommandType string var (
ErrInvalidArg = errors.New("invalid argument")
const (
Single CommandType = "single"
Collection CommandType = "collection"
) )
type Flag struct { type Command interface {
Name string Do(args []string) (bool, error)
Short string
Description string
Required bool
}
type Command struct {
Name string
Description string
Flags []*Flag
Action func([]*Flag) error
Default bool
} }
type CLI struct { type CLI struct {
CMDS []*Command Commands []Command
} }
func (cli *CLI) Run(args []string) *Command { func (cli *CLI) Run(args []string) error {
if len(args) == 0 { for _, c := range cli.Commands {
args = []string{"list"} worked, err := c.Do(args)
if err != nil {
return err
} }
if worked {
id, err := strconv.Atoi(args[0]) return nil
if err == nil {
args = args[1:]
}
if len(args) == 0 {
args = []string{"show"}
}
for _, c := range cli.CMDS {
if c.Name == name {
return c
} }
} }
// flags := make([]*Flag, 0, len(args)) return fmt.Errorf("could not find matchin command")
// for _, arg := range args {
// }
} }