Skip to content

Commit

Permalink
[#17] Added --args for specifying arguments to be passed to the app…
Browse files Browse the repository at this point in the history
…lication being live-reloaded (#38)

* completes [#17]
* added tests
  • Loading branch information
zephinzer committed Apr 1, 2019
1 parent 355d1f0 commit c787f3f
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 78 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ start:
fi
start.linux: compile.linux
@$(MAKE) log.debug MSG="running godev for development..."
@$(CURDIR)/bin/godev--linux-amd64 -vv --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}
@$(CURDIR)/bin/godev--linux-amd64 -vv --args 'hi,there' --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}
start.macos: compile.macos
@$(MAKE) log.debug MSG="running godev for development..."
@$(CURDIR)/bin/godev--darwin-amd64 -vv --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}
@$(CURDIR)/bin/godev--darwin-amd64 -vv --args 'hi,there' --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}
start.windows: compile.windows
@$(MAKE) log.debug MSG="running godev for development..."
@$(CURDIR)/bin/godev--windows-386.exe -vv --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}
@$(CURDIR)/bin/godev--windows-386.exe -vv --args 'hi,there' --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}

## installs the dependencies
deps:
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ By default, GoDev will run for live-reload in development. This results in the d

| Flag | Description |
| --- | --- |
| [`--args`](#--args) | Specifies arguments to pass into commands of the final execution group (the application being live-reloaded) |
| [`--dir`](#--dir) | Specifies the working directory |
| [`--env`](#--env) | Specifies an environment variable |
| [`--exec`](#--exec) | Specifies comma-delimited commands |
Expand Down Expand Up @@ -212,6 +213,11 @@ Tells GoDev to keep completely quiet. Only panic level logs are printed before G

#### Configuration

##### `--args`
Specifies the arguments to be passed into the last execution group which should contain the path to your binary.

Default: None

##### `--dir`
Specifies the directory for commands from GoDev to run from.

Expand Down
6 changes: 6 additions & 0 deletions cli_default_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package main
import (
"strings"

shellquote "github.com/kballard/go-shellquote"
"github.com/urfave/cli"
)

func getDefaultFlags() []cli.Flag {
return []cli.Flag{
getFlagBuildOutput(),
getFlagCommandArguments(),
getFlagCommandsDelimiter(),
getFlagEnvVars(),
getFlagExecGroups(),
Expand All @@ -25,8 +27,12 @@ func getDefaultFlags() []cli.Flag {

func getDefaultAction(config *Config) cli.ActionFunc {
return func(c *cli.Context) error {
var err error
config.RunDefault = true
config.BuildOutput = c.String("output")
if config.CommandArguments, err = shellquote.Split(c.String("args")); err != nil {
panic(err)
}
config.CommandsDelimiter = c.String("exec-delim")
config.EnvVars = c.StringSlice("env")
config.ExecGroups = c.StringSlice("exec")
Expand Down
1 change: 1 addition & 0 deletions cli_default_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (s *CLIDefaultHandlerTestSuite) SetupTest() {
func (s *CLIDefaultHandlerTestSuite) Test_getDefaultFlags() {
ensureCLIFlags(s.T(),
[]string{
"args",
"dir",
"env",
"exec-delim",
Expand Down
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
// DefaultBuildOutput - default relative path to watch directory to place built binaries in
const DefaultBuildOutput = "bin/app"

// DefaultCommandArguments - default arguments to pass to commands in the last execution group
const DefaultCommandArguments = ""

// DefaultCommandsDelimiter - default string to split --execs into commands with
const DefaultCommandsDelimiter = ","

Expand All @@ -31,6 +34,7 @@ const DefaultRefreshRate = 2 * time.Second
// Config configures the main application entrypoint
type Config struct {
BuildOutput string
CommandArguments ConfigCommaDelimitedString
CommandsDelimiter string
EnvVars ConfigMultiflagString
ExecGroups ConfigMultiflagString
Expand Down
6 changes: 3 additions & 3 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// MAKE CHANGES AT ~/app/data/generate AND RUN make generate TO REGENERATE
// THE FOLLOWING FILE
//
// GENERATED BY GO:GENERATE AT 2019-03-23 17:36:49.028176925 +0000 UTC m=+0.005074006
// GENERATED BY GO:GENERATE AT 2019-04-01 23:13:50.175213187 +0800 HKT m=+0.029246505
//
// FILE GENERATED USING ~/app/data/generate.go

Expand All @@ -14,7 +14,7 @@ package main
const Version = "0.5.40"

// Commit is used by godev for reporting the version when installed via 'go get'
const Commit = "48be2b5"
const Commit = "6444d53"

// DataDockerfile defines the 'Dockerfile' contents when --init is used
// hash:fc3c6491cb0d101ae17e2e68aec4714f
Expand Down Expand Up @@ -249,7 +249,7 @@ const DataGoDotMod = `module app
// MAKE CHANGES AT ~/app/data/generate AND RUN make generate TO REGENERATE
// THE FOLLOWING FILE
//
// GENERATED BY GO:GENERATE AT 2019-03-23 17:36:49.028176925 +0000 UTC m=+0.005074006
// GENERATED BY GO:GENERATE AT 2019-04-01 23:13:50.175213187 +0800 HKT m=+0.029246505
//
// FILE GENERATED USING ~/app/data/generate.go

Expand Down
9 changes: 9 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ func getFlagBuildOutput() cli.Flag {
}
}

// getFlagCommandArguments provisions --output
func getFlagCommandArguments() cli.Flag {
return cli.StringFlag{
Name: "args",
Usage: "| where <value> is a comma delimited string containing arguments to pass to commands in the final execution group",
Value: DefaultCommandArguments,
}
}

// getFlagCommandsDelimiter provisions --exec-delim
func getFlagCommandsDelimiter() cli.Flag {
return cli.StringFlag{
Expand Down
4 changes: 4 additions & 0 deletions flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func (s *FlagsTestSuite) Test_getFlagBuildOutput() {
ensureFlag(s.T(), getFlagBuildOutput(), cli.StringFlag{}, `^output.*`)
}

func (s *FlagsTestSuite) Test_getFlagCommandArguments() {
ensureFlag(s.T(), getFlagCommandArguments(), cli.StringFlag{}, `^args`)
}

func (s *FlagsTestSuite) Test_getFlagCommandsDelimiter() {
ensureFlag(s.T(), getFlagCommandsDelimiter(), cli.StringFlag{}, `^exec-delim.*`)
}
Expand Down
111 changes: 58 additions & 53 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,32 @@ type GoDev struct {
func (godev *GoDev) Start() {
defer godev.logger.Infof("godev has ended")
godev.logger.Infof("godev has started")
if godev.config.RunVersion || godev.config.RunView {
// do nothing
if godev.config.RunDefault || godev.config.RunTest {
godev.startWatching()
} else if godev.config.RunInit {
godev.initialiseDirectory()
} else {
godev.startWatching()
}
}

func (godev *GoDev) createPipeline() []*ExecutionGroup {
var pipeline []*ExecutionGroup
for _, execGroup := range godev.config.ExecGroups {
for execGroupIndex, execGroup := range godev.config.ExecGroups {
executionGroup := &ExecutionGroup{}
var executionCommands []*Command
commands := strings.Split(execGroup, godev.config.CommandsDelimiter)
for _, command := range commands {
if sections, err := shellquote.Split(command); err != nil {
panic(err)
} else {
arguments := sections[1:]
if execGroupIndex == len(godev.config.ExecGroups)-1 {
arguments = append(arguments, godev.config.CommandArguments...)
}
executionCommands = append(
executionCommands,
InitCommand(&CommandConfig{
Application: sections[0],
Arguments: sections[1:],
Arguments: arguments,
Directory: godev.config.WorkDirectory,
Environment: godev.config.EnvVars,
LogLevel: godev.config.LogLevel,
Expand All @@ -91,53 +93,6 @@ func (godev *GoDev) eventHandler(events *[]WatcherEvent) bool {
return true
}

func (godev *GoDev) startWatching() {
godev.logUniversalConfigurations()
godev.logWatchModeConfigurations()
godev.initialiseWatcher()
godev.initialiseRunner()

var wg sync.WaitGroup
godev.watcher.BeginWatch(&wg, godev.eventHandler)
godev.logger.Infof("working dir : '%s'", godev.config.WorkDirectory)
godev.logger.Infof("watching dir: '%s'", godev.config.WatchDirectory)
godev.runner.Trigger()
wg.Wait()
}

func (godev *GoDev) logUniversalConfigurations() {
godev.logger.Debugf("flag - init : %v", godev.config.RunInit)
godev.logger.Debugf("flag - test : %v", godev.config.RunTest)
godev.logger.Debugf("flag - view : %v", godev.config.RunView)
godev.logger.Debugf("watch directory : %s", godev.config.WatchDirectory)
godev.logger.Debugf("work directory : %s", godev.config.WorkDirectory)
godev.logger.Debugf("build output : %s", godev.config.BuildOutput)
}

func (godev *GoDev) logWatchModeConfigurations() {
config := godev.config
logger := godev.logger
logger.Debugf("environment : %v", config.EnvVars)
logger.Debugf("file extensions : %v", config.FileExtensions)
logger.Debugf("ignored names : %v", config.IgnoredNames)
logger.Debugf("refresh interval : %v", config.Rate)
logger.Debugf("execution delim : %s", config.CommandsDelimiter)
logger.Debug("execution groups as follows...")
for egIndex, execGroup := range config.ExecGroups {
logger.Debugf(" %v) %s", egIndex+1, execGroup)
commands := strings.Split(execGroup, config.CommandsDelimiter)
for cIndex, command := range commands {
sections, err := shellquote.Split(command)
if err != nil {
panic(err)
}
app := sections[0]
args := sections[1:]
logger.Debugf(" %v > %s %v", cIndex+1, app, args)
}
}
}

func (godev *GoDev) initialiseInitialisers() []Initialiser {
return []Initialiser{
InitGitInitialiser(&GitInitialiserConfig{
Expand Down Expand Up @@ -218,3 +173,53 @@ func (godev *GoDev) initialiseWatcher() {
})
godev.watcher.RecursivelyWatch(godev.config.WatchDirectory)
}

func (godev *GoDev) logUniversalConfigurations() {
godev.logger.Debugf("flag - init : %v", godev.config.RunInit)
godev.logger.Debugf("flag - test : %v", godev.config.RunTest)
godev.logger.Debugf("flag - view : %v", godev.config.RunView)
godev.logger.Debugf("watch directory : %s", godev.config.WatchDirectory)
godev.logger.Debugf("work directory : %s", godev.config.WorkDirectory)
godev.logger.Debugf("build output : %s", godev.config.BuildOutput)
}

func (godev *GoDev) logWatchModeConfigurations() {
config := godev.config
logger := godev.logger
logger.Debugf("environment : %v", config.EnvVars)
logger.Debugf("file extensions : %v", config.FileExtensions)
logger.Debugf("ignored names : %v", config.IgnoredNames)
logger.Debugf("refresh interval : %v", config.Rate)
logger.Debugf("execution delim : %s", config.CommandsDelimiter)
logger.Debug("execution groups as follows...")
for execGroupIndex, execGroup := range config.ExecGroups {
logger.Debugf(" %v) %s", execGroupIndex+1, execGroup)
commands := strings.Split(execGroup, config.CommandsDelimiter)
for commandIndex, command := range commands {
sections, err := shellquote.Split(command)
if err != nil {
panic(err)
}
application := sections[0]
arguments := sections[1:]
if execGroupIndex == len(config.ExecGroups)-1 {
arguments = append(arguments, config.CommandArguments...)
}
logger.Debugf(" %v > %s %v", commandIndex+1, application, arguments)
}
}
}

func (godev *GoDev) startWatching() {
godev.logUniversalConfigurations()
godev.logWatchModeConfigurations()
godev.initialiseWatcher()
godev.initialiseRunner()

var wg sync.WaitGroup
godev.watcher.BeginWatch(&wg, godev.eventHandler)
godev.logger.Infof("working dir : '%s'", godev.config.WorkDirectory)
godev.logger.Infof("watching dir: '%s'", godev.config.WatchDirectory)
godev.runner.Trigger()
wg.Wait()
}
Loading

0 comments on commit c787f3f

Please sign in to comment.