command.go (771B)
1 package command 2 3 import ( 4 "fmt" 5 "time" 6 7 "code.dwrz.net/src/pkg/dqs/store" 8 ) 9 10 type command struct { 11 execute func(args []string, date time.Time, store *store.Store) error 12 13 description string 14 help func() string 15 name string 16 } 17 18 func (c command) Execute(p Parameters) error { 19 if err := p.Validate(); err != nil { 20 return err 21 } 22 23 return c.execute(p.Args, p.Date, p.Store) 24 } 25 26 var commands []*command 27 28 func init() { 29 commands = []*command{ 30 Add, 31 BodyFat, 32 Config, 33 Delete, 34 Entry, 35 Export, 36 Help, 37 Note, 38 Remove, 39 Report, 40 User, 41 Weight, 42 } 43 } 44 45 func Match(name string) (*command, error) { 46 for _, command := range commands { 47 if name == command.name { 48 return command, nil 49 } 50 } 51 52 return nil, fmt.Errorf("unrecognized command: %s", name) 53 }