2024-10-29 07:22:04 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-12-19 12:06:03 +01:00
|
|
|
"strconv"
|
2024-10-29 07:22:04 +01:00
|
|
|
"time"
|
2024-12-01 10:22:47 +01:00
|
|
|
|
|
|
|
"go-mod.ewintr.nl/planner/item"
|
2024-10-29 07:22:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DateFormat = "2006-01-02"
|
|
|
|
TimeFormat = "15:04"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrWrongCommand = errors.New("wrong command")
|
|
|
|
ErrInvalidArg = errors.New("invalid argument")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Flag interface {
|
|
|
|
Set(val string) error
|
|
|
|
IsSet() bool
|
|
|
|
Get() any
|
|
|
|
}
|
|
|
|
|
|
|
|
type FlagString struct {
|
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FlagString) Set(val string) error {
|
|
|
|
fs.Value = val
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FlagString) IsSet() bool {
|
|
|
|
return fs.Value != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FlagString) Get() any {
|
|
|
|
return fs.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
type FlagDate struct {
|
|
|
|
Name string
|
2024-12-19 12:06:03 +01:00
|
|
|
Value item.Date
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
|
|
|
|
2024-12-19 12:06:03 +01:00
|
|
|
func (fd *FlagDate) Set(val string) error {
|
|
|
|
d := item.NewDateFromString(val)
|
|
|
|
if d.IsZero() {
|
2024-10-29 07:22:04 +01:00
|
|
|
return fmt.Errorf("could not parse date: %v", d)
|
|
|
|
}
|
2024-12-19 12:06:03 +01:00
|
|
|
fd.Value = d
|
2024-10-29 07:22:04 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-12-19 12:06:03 +01:00
|
|
|
func (fd *FlagDate) IsSet() bool {
|
|
|
|
return !fd.Value.IsZero()
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
|
|
|
|
2024-12-19 12:06:03 +01:00
|
|
|
func (fd *FlagDate) Get() any {
|
|
|
|
return fd.Value
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type FlagTime struct {
|
|
|
|
Name string
|
2024-12-19 12:06:03 +01:00
|
|
|
Value item.Time
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ft *FlagTime) Set(val string) error {
|
2024-12-19 12:06:03 +01:00
|
|
|
d := item.NewTimeFromString(val)
|
|
|
|
if d.IsZero() {
|
2024-10-29 07:22:04 +01:00
|
|
|
return fmt.Errorf("could not parse date: %v", d)
|
|
|
|
}
|
|
|
|
ft.Value = d
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fd *FlagTime) IsSet() bool {
|
|
|
|
return !fd.Value.IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FlagTime) Get() any {
|
|
|
|
return fs.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
type FlagDuration struct {
|
|
|
|
Name string
|
|
|
|
Value time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fd *FlagDuration) Set(val string) error {
|
|
|
|
dur, err := time.ParseDuration(val)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse duration: %v", err)
|
|
|
|
}
|
|
|
|
fd.Value = dur
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fd *FlagDuration) IsSet() bool {
|
|
|
|
return fd.Value.String() != "0s"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FlagDuration) Get() any {
|
|
|
|
return fs.Value
|
|
|
|
}
|
2024-12-01 10:22:47 +01:00
|
|
|
|
2024-12-19 12:06:03 +01:00
|
|
|
type FlagRecurrer struct {
|
|
|
|
Name string
|
|
|
|
Value item.Recurrer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fr *FlagRecurrer) Set(val string) error {
|
|
|
|
fr.Value = item.NewRecurrer(val)
|
|
|
|
if fr.Value == nil {
|
|
|
|
return fmt.Errorf("not a valid recurrer: %v", val)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fr *FlagRecurrer) IsSet() bool {
|
|
|
|
return fr.Value != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fr *FlagRecurrer) Get() any {
|
|
|
|
return fr.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
type FlagInt struct {
|
2024-12-01 10:22:47 +01:00
|
|
|
Name string
|
2024-12-19 12:06:03 +01:00
|
|
|
Value int
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|
|
|
|
|
2024-12-19 12:06:03 +01:00
|
|
|
func (fi *FlagInt) Set(val string) error {
|
|
|
|
i, err := strconv.Atoi(val)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("not a valid integer: %v", val)
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|
2024-12-19 12:06:03 +01:00
|
|
|
|
|
|
|
fi.Value = i
|
2024-12-01 10:22:47 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-12-19 12:06:03 +01:00
|
|
|
func (fi *FlagInt) IsSet() bool {
|
|
|
|
return fi.Value != 0
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|
|
|
|
|
2024-12-19 12:06:03 +01:00
|
|
|
func (fi *FlagInt) Get() any {
|
|
|
|
return fi.Value
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|