33 lines
437 B
Go
33 lines
437 B
Go
package command
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidArg = errors.New("invalid argument")
|
|
)
|
|
|
|
type Command interface {
|
|
Do(args []string) (bool, error)
|
|
}
|
|
|
|
type CLI struct {
|
|
Commands []Command
|
|
}
|
|
|
|
func (cli *CLI) Run(args []string) error {
|
|
for _, c := range cli.Commands {
|
|
worked, err := c.Do(args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if worked {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return fmt.Errorf("could not find matchin command")
|
|
}
|