Skip to content

Commit c787f3f

Browse files
authored
[#17] Added --args for specifying arguments to be passed to the application being live-reloaded (#38)
* completes [#17] * added tests
1 parent 355d1f0 commit c787f3f

File tree

11 files changed

+166
-78
lines changed

11 files changed

+166
-78
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ start:
8181
fi
8282
start.linux: compile.linux
8383
@$(MAKE) log.debug MSG="running godev for development..."
84-
@$(CURDIR)/bin/godev--linux-amd64 -vv --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}
84+
@$(CURDIR)/bin/godev--linux-amd64 -vv --args 'hi,there' --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}
8585
start.macos: compile.macos
8686
@$(MAKE) log.debug MSG="running godev for development..."
87-
@$(CURDIR)/bin/godev--darwin-amd64 -vv --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}
87+
@$(CURDIR)/bin/godev--darwin-amd64 -vv --args 'hi,there' --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}
8888
start.windows: compile.windows
8989
@$(MAKE) log.debug MSG="running godev for development..."
90-
@$(CURDIR)/bin/godev--windows-386.exe -vv --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}
90+
@$(CURDIR)/bin/godev--windows-386.exe -vv --args 'hi,there' --watch $(CURDIR) --dir $(CURDIR)/dev ${ARGS}
9191

9292
## installs the dependencies
9393
deps:

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ By default, GoDev will run for live-reload in development. This results in the d
123123

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

213214
#### Configuration
214215

216+
##### `--args`
217+
Specifies the arguments to be passed into the last execution group which should contain the path to your binary.
218+
219+
Default: None
220+
215221
##### `--dir`
216222
Specifies the directory for commands from GoDev to run from.
217223

cli_default_handler.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package main
33
import (
44
"strings"
55

6+
shellquote "github.com/kballard/go-shellquote"
67
"github.com/urfave/cli"
78
)
89

910
func getDefaultFlags() []cli.Flag {
1011
return []cli.Flag{
1112
getFlagBuildOutput(),
13+
getFlagCommandArguments(),
1214
getFlagCommandsDelimiter(),
1315
getFlagEnvVars(),
1416
getFlagExecGroups(),
@@ -25,8 +27,12 @@ func getDefaultFlags() []cli.Flag {
2527

2628
func getDefaultAction(config *Config) cli.ActionFunc {
2729
return func(c *cli.Context) error {
30+
var err error
2831
config.RunDefault = true
2932
config.BuildOutput = c.String("output")
33+
if config.CommandArguments, err = shellquote.Split(c.String("args")); err != nil {
34+
panic(err)
35+
}
3036
config.CommandsDelimiter = c.String("exec-delim")
3137
config.EnvVars = c.StringSlice("env")
3238
config.ExecGroups = c.StringSlice("exec")

cli_default_handler_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func (s *CLIDefaultHandlerTestSuite) SetupTest() {
2929
func (s *CLIDefaultHandlerTestSuite) Test_getDefaultFlags() {
3030
ensureCLIFlags(s.T(),
3131
[]string{
32+
"args",
3233
"dir",
3334
"env",
3435
"exec-delim",

config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
// DefaultBuildOutput - default relative path to watch directory to place built binaries in
1111
const DefaultBuildOutput = "bin/app"
1212

13+
// DefaultCommandArguments - default arguments to pass to commands in the last execution group
14+
const DefaultCommandArguments = ""
15+
1316
// DefaultCommandsDelimiter - default string to split --execs into commands with
1417
const DefaultCommandsDelimiter = ","
1518

@@ -31,6 +34,7 @@ const DefaultRefreshRate = 2 * time.Second
3134
// Config configures the main application entrypoint
3235
type Config struct {
3336
BuildOutput string
37+
CommandArguments ConfigCommaDelimitedString
3438
CommandsDelimiter string
3539
EnvVars ConfigMultiflagString
3640
ExecGroups ConfigMultiflagString

data.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// MAKE CHANGES AT ~/app/data/generate AND RUN make generate TO REGENERATE
55
// THE FOLLOWING FILE
66
//
7-
// GENERATED BY GO:GENERATE AT 2019-03-23 17:36:49.028176925 +0000 UTC m=+0.005074006
7+
// GENERATED BY GO:GENERATE AT 2019-04-01 23:13:50.175213187 +0800 HKT m=+0.029246505
88
//
99
// FILE GENERATED USING ~/app/data/generate.go
1010

@@ -14,7 +14,7 @@ package main
1414
const Version = "0.5.40"
1515

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

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

flags.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ func getFlagBuildOutput() cli.Flag {
1313
}
1414
}
1515

16+
// getFlagCommandArguments provisions --output
17+
func getFlagCommandArguments() cli.Flag {
18+
return cli.StringFlag{
19+
Name: "args",
20+
Usage: "| where <value> is a comma delimited string containing arguments to pass to commands in the final execution group",
21+
Value: DefaultCommandArguments,
22+
}
23+
}
24+
1625
// getFlagCommandsDelimiter provisions --exec-delim
1726
func getFlagCommandsDelimiter() cli.Flag {
1827
return cli.StringFlag{

flags_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ func (s *FlagsTestSuite) Test_getFlagBuildOutput() {
1919
ensureFlag(s.T(), getFlagBuildOutput(), cli.StringFlag{}, `^output.*`)
2020
}
2121

22+
func (s *FlagsTestSuite) Test_getFlagCommandArguments() {
23+
ensureFlag(s.T(), getFlagCommandArguments(), cli.StringFlag{}, `^args`)
24+
}
25+
2226
func (s *FlagsTestSuite) Test_getFlagCommandsDelimiter() {
2327
ensureFlag(s.T(), getFlagCommandsDelimiter(), cli.StringFlag{}, `^exec-delim.*`)
2428
}

main.go

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,32 @@ type GoDev struct {
4646
func (godev *GoDev) Start() {
4747
defer godev.logger.Infof("godev has ended")
4848
godev.logger.Infof("godev has started")
49-
if godev.config.RunVersion || godev.config.RunView {
50-
// do nothing
49+
if godev.config.RunDefault || godev.config.RunTest {
50+
godev.startWatching()
5151
} else if godev.config.RunInit {
5252
godev.initialiseDirectory()
53-
} else {
54-
godev.startWatching()
5553
}
5654
}
5755

5856
func (godev *GoDev) createPipeline() []*ExecutionGroup {
5957
var pipeline []*ExecutionGroup
60-
for _, execGroup := range godev.config.ExecGroups {
58+
for execGroupIndex, execGroup := range godev.config.ExecGroups {
6159
executionGroup := &ExecutionGroup{}
6260
var executionCommands []*Command
6361
commands := strings.Split(execGroup, godev.config.CommandsDelimiter)
6462
for _, command := range commands {
6563
if sections, err := shellquote.Split(command); err != nil {
6664
panic(err)
6765
} else {
66+
arguments := sections[1:]
67+
if execGroupIndex == len(godev.config.ExecGroups)-1 {
68+
arguments = append(arguments, godev.config.CommandArguments...)
69+
}
6870
executionCommands = append(
6971
executionCommands,
7072
InitCommand(&CommandConfig{
7173
Application: sections[0],
72-
Arguments: sections[1:],
74+
Arguments: arguments,
7375
Directory: godev.config.WorkDirectory,
7476
Environment: godev.config.EnvVars,
7577
LogLevel: godev.config.LogLevel,
@@ -91,53 +93,6 @@ func (godev *GoDev) eventHandler(events *[]WatcherEvent) bool {
9193
return true
9294
}
9395

94-
func (godev *GoDev) startWatching() {
95-
godev.logUniversalConfigurations()
96-
godev.logWatchModeConfigurations()
97-
godev.initialiseWatcher()
98-
godev.initialiseRunner()
99-
100-
var wg sync.WaitGroup
101-
godev.watcher.BeginWatch(&wg, godev.eventHandler)
102-
godev.logger.Infof("working dir : '%s'", godev.config.WorkDirectory)
103-
godev.logger.Infof("watching dir: '%s'", godev.config.WatchDirectory)
104-
godev.runner.Trigger()
105-
wg.Wait()
106-
}
107-
108-
func (godev *GoDev) logUniversalConfigurations() {
109-
godev.logger.Debugf("flag - init : %v", godev.config.RunInit)
110-
godev.logger.Debugf("flag - test : %v", godev.config.RunTest)
111-
godev.logger.Debugf("flag - view : %v", godev.config.RunView)
112-
godev.logger.Debugf("watch directory : %s", godev.config.WatchDirectory)
113-
godev.logger.Debugf("work directory : %s", godev.config.WorkDirectory)
114-
godev.logger.Debugf("build output : %s", godev.config.BuildOutput)
115-
}
116-
117-
func (godev *GoDev) logWatchModeConfigurations() {
118-
config := godev.config
119-
logger := godev.logger
120-
logger.Debugf("environment : %v", config.EnvVars)
121-
logger.Debugf("file extensions : %v", config.FileExtensions)
122-
logger.Debugf("ignored names : %v", config.IgnoredNames)
123-
logger.Debugf("refresh interval : %v", config.Rate)
124-
logger.Debugf("execution delim : %s", config.CommandsDelimiter)
125-
logger.Debug("execution groups as follows...")
126-
for egIndex, execGroup := range config.ExecGroups {
127-
logger.Debugf(" %v) %s", egIndex+1, execGroup)
128-
commands := strings.Split(execGroup, config.CommandsDelimiter)
129-
for cIndex, command := range commands {
130-
sections, err := shellquote.Split(command)
131-
if err != nil {
132-
panic(err)
133-
}
134-
app := sections[0]
135-
args := sections[1:]
136-
logger.Debugf(" %v > %s %v", cIndex+1, app, args)
137-
}
138-
}
139-
}
140-
14196
func (godev *GoDev) initialiseInitialisers() []Initialiser {
14297
return []Initialiser{
14398
InitGitInitialiser(&GitInitialiserConfig{
@@ -218,3 +173,53 @@ func (godev *GoDev) initialiseWatcher() {
218173
})
219174
godev.watcher.RecursivelyWatch(godev.config.WatchDirectory)
220175
}
176+
177+
func (godev *GoDev) logUniversalConfigurations() {
178+
godev.logger.Debugf("flag - init : %v", godev.config.RunInit)
179+
godev.logger.Debugf("flag - test : %v", godev.config.RunTest)
180+
godev.logger.Debugf("flag - view : %v", godev.config.RunView)
181+
godev.logger.Debugf("watch directory : %s", godev.config.WatchDirectory)
182+
godev.logger.Debugf("work directory : %s", godev.config.WorkDirectory)
183+
godev.logger.Debugf("build output : %s", godev.config.BuildOutput)
184+
}
185+
186+
func (godev *GoDev) logWatchModeConfigurations() {
187+
config := godev.config
188+
logger := godev.logger
189+
logger.Debugf("environment : %v", config.EnvVars)
190+
logger.Debugf("file extensions : %v", config.FileExtensions)
191+
logger.Debugf("ignored names : %v", config.IgnoredNames)
192+
logger.Debugf("refresh interval : %v", config.Rate)
193+
logger.Debugf("execution delim : %s", config.CommandsDelimiter)
194+
logger.Debug("execution groups as follows...")
195+
for execGroupIndex, execGroup := range config.ExecGroups {
196+
logger.Debugf(" %v) %s", execGroupIndex+1, execGroup)
197+
commands := strings.Split(execGroup, config.CommandsDelimiter)
198+
for commandIndex, command := range commands {
199+
sections, err := shellquote.Split(command)
200+
if err != nil {
201+
panic(err)
202+
}
203+
application := sections[0]
204+
arguments := sections[1:]
205+
if execGroupIndex == len(config.ExecGroups)-1 {
206+
arguments = append(arguments, config.CommandArguments...)
207+
}
208+
logger.Debugf(" %v > %s %v", commandIndex+1, application, arguments)
209+
}
210+
}
211+
}
212+
213+
func (godev *GoDev) startWatching() {
214+
godev.logUniversalConfigurations()
215+
godev.logWatchModeConfigurations()
216+
godev.initialiseWatcher()
217+
godev.initialiseRunner()
218+
219+
var wg sync.WaitGroup
220+
godev.watcher.BeginWatch(&wg, godev.eventHandler)
221+
godev.logger.Infof("working dir : '%s'", godev.config.WorkDirectory)
222+
godev.logger.Infof("watching dir: '%s'", godev.config.WatchDirectory)
223+
godev.runner.Trigger()
224+
wg.Wait()
225+
}

0 commit comments

Comments
 (0)