Skip to content

Commit

Permalink
Remove unused code and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jfahrer committed Jul 10, 2019
1 parent c042e3f commit b482635
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 74 deletions.
6 changes: 3 additions & 3 deletions donner.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func execCommand(cfg *Cfg, command []string, strict bool) error {
return ErrMissingCommand
}

wrapper, err := cfg.GetHandlerFor(command[0], strict)
handler, err := cfg.GetHandlerFor(command[0], strict)
if err != nil {
return err
}
wrappedCommand := wrapper.WrapCommand(command)
wrappedCommand := handler.WrapCommand(command)

cmd := exec.Command(wrappedCommand[0], wrappedCommand[1:]...)
cmd.Stdout = os.Stdout
Expand Down Expand Up @@ -104,7 +104,7 @@ func readConfig() (*Cfg, error) {
return nil, err
}

cfg, err := generateConfig(dat)
cfg, err := GenerateConfig(dat)
if err != nil {
return nil, err
}
Expand Down
42 changes: 27 additions & 15 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package main

import "github.com/mitchellh/mapstructure"

// DockerRunHandler wraps a command with `docker container run`
type DockerRunHandler struct {
Remove bool
Image string
}

func (handler *DockerRunHandler) Validate() error {
return nil
}

// WrapCommand performs the actual wrapping of the command
func (handler *DockerRunHandler) WrapCommand(command []string) []string {
wrappedCommand := []string{"docker", "container", "run"}

Expand All @@ -25,28 +23,32 @@ func (handler *DockerRunHandler) WrapCommand(command []string) []string {
return append(wrappedCommand, command...)
}

func (handler *DockerRunHandler) validate() error {
// TODO: Perform some actual validations
return nil
}

// InitDockerRunHandler generates a DockerRunHandler
func InitDockerRunHandler(settings map[string]interface{}) (Handler, error) {
var handler *DockerRunHandler
if err := mapstructure.Decode(settings, &handler); err != nil {
return handler, err
}

if err := handler.Validate(); err != nil {
if err := handler.validate(); err != nil {
return handler, err
}

return handler, nil
}

// ComposeRunHandler wraps a command with `docker-compose exec`
type ComposeRunHandler struct {
Remove bool
Service string
}

func (handler *ComposeRunHandler) Validate() error {
return nil
}

// WrapCommand performs the actual wrapping of the command
func (handler *ComposeRunHandler) WrapCommand(command []string) []string {
wrappedCommand := []string{"docker-compose", "run"}

Expand All @@ -61,27 +63,31 @@ func (handler *ComposeRunHandler) WrapCommand(command []string) []string {
return append(wrappedCommand, command...)
}

func (handler *ComposeRunHandler) validate() error {
// TODO: Perform some actual validations
return nil
}

// InitComposeRunHandler generates a ComposeRunHandler
func InitComposeRunHandler(settings map[string]interface{}) (Handler, error) {
var handler *ComposeRunHandler
if err := mapstructure.Decode(settings, &handler); err != nil {
return handler, err
}

if err := handler.Validate(); err != nil {
if err := handler.validate(); err != nil {
return handler, err
}

return handler, nil
}

// ComposeExecHandler wraps a command with `docker-compose run`
type ComposeExecHandler struct {
Service string
}

func (handler *ComposeExecHandler) Validate() error {
return nil
}

// WrapCommand performs the actual wrapping of the command
func (handler *ComposeExecHandler) WrapCommand(command []string) []string {
wrappedCommand := []string{"docker-compose", "run"}

Expand All @@ -92,13 +98,19 @@ func (handler *ComposeExecHandler) WrapCommand(command []string) []string {
return append(wrappedCommand, command...)
}

func (handler *ComposeExecHandler) validate() error {
// TODO: Perform some actual validations
return nil
}

// InitComposeExecHandler generates a ComposeExecHandler
func InitComposeExecHandler(settings map[string]interface{}) (Handler, error) {
var handler *ComposeExecHandler
if err := mapstructure.Decode(settings, &handler); err != nil {
return handler, err
}

if err := handler.Validate(); err != nil {
if err := handler.validate(); err != nil {
return handler, err
}

Expand Down
66 changes: 31 additions & 35 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,26 @@ type Cfg struct {
defaultHandler Handler
}

func (cfg *Cfg) GetHandlerFor(command string, strictMode bool) (CommandWrapper, error) {
// Handler is the interface around the individual handler implementations
type Handler interface {
WrapCommand([]string) []string
}

// GenerateConfig is the main entry point from which we generate the config
func GenerateConfig(file []byte) (*Cfg, error) {
yamlConfig, error := parseYaml(file)
if error != nil {
return nil, error
}

cfg := &Cfg{}
error = cfg.configFromYaml(yamlConfig)

return cfg, error
}

// GetHandlerFor will try to find a handler for the specified command
func (cfg *Cfg) GetHandlerFor(command string, strictMode bool) (Handler, error) {
if handler, ok := cfg.commands[command]; ok {
return handler, nil
} else if strictMode {
Expand All @@ -46,15 +65,6 @@ func (cfg *Cfg) GetHandlerFor(command string, strictMode bool) (CommandWrapper,
return nil, ErrUndefinedCommand
}

type CommandWrapper interface {
WrapCommand([]string) []string
}

type Handler interface {
WrapCommand([]string) []string
Validate() error
}

// ListCommands allows for retrieval of all defined commands in a config
func (cfg *Cfg) ListCommands() []string {
list := make([]string, 0, len(cfg.commands))
Expand All @@ -64,24 +74,6 @@ func (cfg *Cfg) ListCommands() []string {
return list
}

// Validate checks whether the configuration specifies all mandatory properties
func (cfg *Cfg) validate() error {
return nil
}

// TODO This should be the main entry point in which we generate the config from the yaml
func generateConfig(file []byte) (*Cfg, error) {
yamlConfig, error := parseYaml(file)
if error != nil {
return nil, error
}

cfg := &Cfg{}
error = cfg.configFromYaml(yamlConfig)

return cfg, error
}

func (cfg *Cfg) configFromYaml(yaml *yamlCfg) error {
if len(yaml.Strategies) == 0 {
return ErrNoStrategiesSpecified
Expand All @@ -95,9 +87,7 @@ func (cfg *Cfg) configFromYaml(yaml *yamlCfg) error {
cfg.commands = make(map[string]Handler)

for name, settings := range yaml.Strategies {
if handler, err := generateHandler(settings); err == nil {
cfg.handler[name] = handler
} else {
if err := cfg.generateHandler(name, settings); err != nil {
return err
}
}
Expand All @@ -121,18 +111,24 @@ func (cfg *Cfg) configFromYaml(yaml *yamlCfg) error {
return nil
}

func generateHandler(settings map[string]interface{}) (Handler, error) {
func (cfg *Cfg) generateHandler(name string, settings map[string]interface{}) error {
var handlerFactory func(map[string]interface{}) (Handler, error)
if handlerName, ok := settings["handler"].(string); ok {
handlerFactory, ok = handlerFactories[handlerName]
if !ok {
return nil, ErrInvalidHandler
return ErrInvalidHandler
}
} else {
return nil, ErrNoHandlerDefined
return ErrNoHandlerDefined
}

delete(settings, "handler")

return handlerFactory(settings)
if handler, err := handlerFactory(settings); err == nil {
cfg.handler[name] = handler
} else {
return err
}

return nil
}
24 changes: 3 additions & 21 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,17 @@ import (
"github.com/stretchr/testify/assert"
)

// func TestGenerationErrors {
// tests := map[string]struct {
// input string
// exp *Cfg
// expErr error
// }{
// "yaml without commands": {input: missingCommandsYaml, expErr: ErrNoCommandsSpecified},
// "yaml without strategies": {input: missingStrategiesYml, expErr: ErrNoStrategiesSpecified},
// }

// for name, test := range tests {
// t.Run(name, func(t *testing.T) {
// _, err := parseYaml([]byte(test.input))
// assert.Error(t, err, test.expErr.Error())
// })
// }
// }

func TestListCommands(t *testing.T) {
cfg, err := generateConfig([]byte(fullYaml))
cfg, err := GenerateConfig([]byte(fullYaml))
assert.NoError(t, err)
assert.ElementsMatch(t, cfg.ListCommands(), []string{"ls", "bundle"})
}

func TestGetHandlerFor(t *testing.T) {
var handler CommandWrapper
var handler Handler
var err error

cfg, err := generateConfig([]byte(fullYaml))
cfg, err := GenerateConfig([]byte(fullYaml))
assert.NoError(t, err)

// Without strict mode for a known command
Expand Down

0 comments on commit b482635

Please sign in to comment.