Skip to content

Commit

Permalink
Use the default strategy if the command is not defined in the config (#4
Browse files Browse the repository at this point in the history
)

* Use the default strategy if the command is not defined in the config
* Skip argument reordering
This ensures that `donner run --strict ruby -v` works. Without the flag
urfave/cli would interpret the `-v` as part of the flags.
  • Loading branch information
jfahrer committed Jul 10, 2019
1 parent 683d079 commit 7cd3a65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
33 changes: 19 additions & 14 deletions donner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,21 @@ func main() {
app.Usage = `Donner is a generic command wrapper. It let's you define strategies to wrap commands in things like 'docker-compose exec' or 'docker container run'.
This is can come in very handy when developing applications in containers. Donner allows defining a wrapping strategy on a per command basis.
So you don't have to worry which service to use or whether you should use 'docker-compose exec' or 'docker-compose run' when executing a command.`
// TODO implement flags
app.Flags = []cli.Flag{
cli.BoolFlag{Name: "strict,s", Usage: "enable strict mode"},
cli.BoolFlag{Name: "fallback,f", Usage: "fallback to local commands"},
}
app.Commands = []cli.Command{
{
Name: "run",
Aliases: []string{"r"},
Usage: "run a command",
SkipFlagParsing: true,
Name: "run",
Aliases: []string{"r"},
Usage: "run a command",
SkipArgReorder: true,
Flags: []cli.Flag{
cli.BoolFlag{Name: "strict,s", Usage: "enable strict mode"},
},
Action: func(c *cli.Context) error {
cfg, err := readConfig()
if err != nil {
return err
}
return execCommand(cfg, c.Args())
return execCommand(cfg, c.Args(), c.Bool("strict"))
},
},
{
Expand Down Expand Up @@ -69,20 +67,27 @@ func main() {
}

// execCommand dispatches the call to the OS
func execCommand(cfg *Cfg, params []string) error {
func execCommand(cfg *Cfg, params []string, strict bool) error {
if len(params) < 1 {
// TODO show usage?
return ErrMissingCommand
}

var designatedHandler Strategy

// check if command specified exists in parsed file
command, ok := cfg.Commands[params[0]]
if !ok {
return ErrUndefinedCommand
if ok {
designatedHandler = cfg.Strategies[string(command)]
} else {
if strict {
return ErrUndefinedCommand
}

designatedHandler = cfg.GetDefaultStrategy()
}

// extract handler from corresponding strategy
designatedHandler := cfg.Strategies[string(command)]
execHandler := availableHandlers[designatedHandler.Handler]

// construct os call
Expand Down
4 changes: 4 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (s *Strategy) Validate() error {
return nil
}

func (c *Cfg) GetDefaultStrategy() Strategy {
return c.Strategies[c.DefaultStrategy]
}

// Command is an alias for string to properly reflect the yaml definition
type Command string

Expand Down

0 comments on commit 7cd3a65

Please sign in to comment.