Skip to content

Commit

Permalink
Merge pull request #138 from justenwalker/gh128_refactor-app-state
Browse files Browse the repository at this point in the history
Refactor: Config to Application State
  • Loading branch information
tgross committed May 2, 2016
2 parents 94a1729 + 8bbd8f3 commit 7c8c42f
Show file tree
Hide file tree
Showing 29 changed files with 930 additions and 654 deletions.
5 changes: 5 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions backends/backends.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package backends

import (
"encoding/json"
"fmt"
"os/exec"
"time"
Expand All @@ -13,21 +12,21 @@ import (

// Backend represents a command to execute when another application changes
type Backend struct {
Name string `json:"name"`
Poll int `json:"poll"` // time in seconds
OnChangeExec json.RawMessage `json:"onChange"`
Tag string `json:"tag,omitempty"`
Name string `mapstructure:"name"`
Poll int `mapstructure:"poll"` // time in seconds
OnChangeExec interface{} `mapstructure:"onChange"`
Tag string `mapstructure:"tag"`
discoveryService discovery.DiscoveryService
lastState interface{}
onChangeCmd *exec.Cmd
}

func NewBackends(raw json.RawMessage, disc discovery.DiscoveryService) ([]*Backend, error) {
func NewBackends(raw []interface{}, disc discovery.DiscoveryService) ([]*Backend, error) {
if raw == nil {
return []*Backend{}, nil
}
backends := make([]*Backend, 0)
if err := json.Unmarshal(raw, &backends); err != nil {
if err := utils.DecodeRaw(raw, &backends); err != nil {
return nil, fmt.Errorf("Backend configuration error: %v", err)
}
for _, b := range backends {
Expand Down
8 changes: 7 additions & 1 deletion backends/backends_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backends

import (
"encoding/json"
"os/exec"
"reflect"
"testing"
Expand Down Expand Up @@ -41,7 +42,12 @@ func TestBackendsParse(t *testing.T) {
}
]`)

if backends, err := NewBackends(jsonFragment, nil); err != nil {
var raw []interface{}
if err := json.Unmarshal(jsonFragment, &raw); err != nil {
t.Fatalf("Unexpected error decoding JSON: %v", err)
}

if backends, err := NewBackends(raw, nil); err != nil {
t.Fatalf("Could not parse backends JSON: %s", err)
} else {
validateCommandParsed(t, "onChange", backends[0].onChangeCmd,
Expand Down
42 changes: 42 additions & 0 deletions config/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package config

import (
"fmt"
"os/exec"

log "github.com/Sirupsen/logrus"
"github.com/joyent/containerpilot/utils"
)

// ParsePreStart ...
func (cfg *Config) ParsePreStart() (*exec.Cmd, error) {
// onStart has been deprecated for preStart. Remove in 2.0
if cfg.PreStart != nil && cfg.OnStart != nil {
log.Warnf("The onStart option has been deprecated in favor of preStart. ContainerPilot will use only the preStart option provided")
}

// alias the onStart behavior to preStart
if cfg.PreStart == nil && cfg.OnStart != nil {
log.Warnf("The onStart option has been deprecated in favor of preStart. ContainerPilot will use the onStart option as a preStart")
cfg.PreStart = cfg.OnStart
}
return parseCommand("preStart", cfg.PreStart)
}

// ParsePreStop ...
func (cfg *Config) ParsePreStop() (*exec.Cmd, error) {
return parseCommand("preStop", cfg.PreStop)
}

// ParsePostStop ...
func (cfg *Config) ParsePostStop() (*exec.Cmd, error) {
return parseCommand("postStop", cfg.PostStop)
}

func parseCommand(name string, args interface{}) (*exec.Cmd, error) {
cmd, err := utils.ParseCommandArgs(args)
if err != nil {
return nil, fmt.Errorf("Could not parse `%s`: %s", name, err)
}
return cmd, nil
}
Loading

0 comments on commit 7c8c42f

Please sign in to comment.