Skip to content

Commit

Permalink
further progress on parsing/execution of commands
Browse files Browse the repository at this point in the history
  • Loading branch information
elsesiy committed Jun 29, 2019
1 parent 0c92bff commit 72008fc
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 29 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Jonas-Taha El Sesiy & Julian Fahrer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Donner

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.
:boom: 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.

## Examples

Expand Down
93 changes: 68 additions & 25 deletions donner.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,56 @@
package main

import (
"errors"
"fmt"
"github.com/urfave/cli"
"io/ioutil"
"log"
"os"

"github.com/urfave/cli"
"os/exec"
)

// ErrUndefinedCommand is thrown if a command specified can't be found in the yaml definition
var ErrUndefinedCommand = errors.New("the command you're trying to run doesn't exist in the yaml definition")
// ErrMissingCommand is thrown if no handler for execution is provided
var ErrMissingCommand = errors.New("no command for execution specified")

func main() {
app := cli.NewApp()
app.Name = "Donner"
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.`
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",
Flags: []cli.Flag{
cli.BoolFlag{Name: "strict,s", Usage: "enable strict mode"},
cli.BoolFlag{Name: "fallback,f", Usage: "fallback to local commands"},
},
Name: "run",
Aliases: []string{"r"},
Usage: "run a command",
SkipFlagParsing: true,
Action: func(c *cli.Context) error {
command := []string{"docker-compose", "exec", "app"}
command = append(command, c.Args()...)

// TODO handle 'yaml' case
dat, err := ioutil.ReadFile(".donner.yml")
if err != nil {
return err
}

res, err := ParseFile(dat)
cfg, err := ParseFile(dat)
if err != nil {
return err
}

// TODO dispatch os call
fmt.Printf("%+v", res)

return nil
return ExecCommand(cfg, c.Args())
},
},
{
Name: "aliases",
Aliases: []string{"a"},
Flags: []cli.Flag{
cli.BoolFlag{Name: "strict,s", Usage: "enable strict mode"},
cli.BoolFlag{Name: "fallback,f", Usage: "fallback to local commands"},
},
Usage: "generate aliases",
Usage: "generate aliases",
Action: func(c *cli.Context) error {
return nil
},
Expand All @@ -64,3 +62,48 @@ func main() {
log.Fatal(err)
}
}

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

// check if command specified exists in parsed file
command, ok := cfg.Commands[params[0]]
if !ok {
return ErrUndefinedCommand
}

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

// construct os call
cliArgs := []string{execHandler.Argument}

if designatedHandler.Remove {
cliArgs = append(cliArgs, "--rm")
}

if designatedHandler.Image != "" {
cliArgs = append(cliArgs, designatedHandler.Image)
}

for _, p := range params {
cliArgs = append(cliArgs, p)
}

cmd := exec.Command(execHandler.BaseCommand, cliArgs...)

out, err := cmd.CombinedOutput()
if err != nil {
return err
}

// TODO structured output?
fmt.Println(string(out))

return nil
}
31 changes: 31 additions & 0 deletions donner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestExecCommand(t *testing.T) {
tests := map[string]struct {
config *Cfg
params []string
expErr error
}{
"missing command": {
config: &Cfg{Strategies: map[string]Strategy{}},
expErr: ErrMissingCommand,
},
"undefined command": {
config: &Cfg{Strategies: map[string]Strategy{"test": {Handler: "test"}}, Commands: map[string]Command{"golang": "exec"}},
params: []string{"invalid"},
expErr: ErrUndefinedCommand,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
err := ExecCommand(test.config, test.params)
assert.EqualError(t, err, test.expErr.Error())
})
}
}
34 changes: 32 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package main

import "gopkg.in/yaml.v2"
import (
"errors"
"gopkg.in/yaml.v2"
)

// ErrInvalidHandler is thrown if any handler that is unknown to the program is specified
var ErrInvalidHandler = errors.New("configuration specifies unknown handler")

// Current handler implementations
var availableHandlers = map[string]ExecHandler{"docker_compose_run": {"docker-compose", "run"}, "docker_compose_exec": {"docker-compose", "exec"}, "docker_run": {"docker", "run"}}

// ExecHandler is the desired OS exec
type ExecHandler struct {
BaseCommand string
Argument string
}

// Cfg is the uber object in our YAML file
type Cfg struct {
Expand All @@ -14,6 +29,17 @@ type Strategy struct {
Handler string
Service string
Remove bool
Image string
}

// Validate checks whether a strategy specifies only valid handlers
func (s *Strategy) Validate() error {
_, ok := availableHandlers[s.Handler]
if !ok {
return ErrInvalidHandler
}

return nil
}

// Command is an alias for string to properly reflect the yaml definition
Expand All @@ -28,7 +54,11 @@ func ParseFile(file []byte) (*Cfg, error) {
return nil, err
}

// TODO validate config
for _, strat := range cfg.Strategies {
if err := strat.Validate(); err != nil {
return nil, err
}
}

return &cfg, nil
}
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ strategies:
service: app
`

func TestParsing(t *testing.T) {
func TestParseFile(t *testing.T) {
// TODO add many more tests
tests := map[string]struct {
input string
Expand Down

0 comments on commit 72008fc

Please sign in to comment.