Skip to content

Commit

Permalink
support passing arguments to the started executable (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo0mer authored Feb 25, 2021
1 parent 7cc2c83 commit 427b290
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The tool supports the following flags for more custom configuration:
* `--glob-exclude, -ge` - Specifies a glob pattern for files and directories that should be excluded. Can be specified multiple times. The pattern works only for a single path segment (e.g. `vendor`, `.DS_Store`, `*_test.go`)
* `--run, -r` - Specifies the folder that includes the `main` package to be run.
* `--cache, -c` - Specifies a pre-build executable to use the first time. This is useful when using `gocrane` with Docker images that have the built executable available to avoid unnecessary initial builds on startup.
* `--args` - Specifies arguments to pass when starting the built executable.

By default `gocrane` excludes the following glob patterns:

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.15

require (
github.com/fsnotify/fsnotify v1.4.9
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/satori/go.uuid v1.2.0
github.com/urfave/cli/v2 v2.3.0
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSY
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
Expand Down
3 changes: 2 additions & 1 deletion internal/crane/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Settings struct {
ExcludeGlobs []string
RunDir string
CachedBuild string
Args []string
ShutdownTimeout time.Duration
}

Expand Down Expand Up @@ -49,7 +50,7 @@ func Run(ctx context.Context, settings Settings) error {
return fmt.Errorf("failed to create builder: %w", err)
}
defer builder.Cleanup()
runner := project.NewRunner()
runner := project.NewRunner(settings.Args)

// Trigger an initial build by faking a change if we don't have
// a cached executable to use.
Expand Down
37 changes: 37 additions & 0 deletions internal/flag/shlex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Package flag provides types to be used as command-line flag arguments with
// the github.com/urfave/cli package.
package flag

import (
"fmt"

"github.com/google/shlex"
)

type ShlexStringSlice []string

func (s *ShlexStringSlice) Set(value string) error {
v, err := shlex.Split(value)
if err != nil {
return err
}
*s = v
return nil
}

func (s *ShlexStringSlice) String() string {
return fmt.Sprintf("%v", []string(*s))
}

func ShlexStrings(val interface{}) []string {
s, ok := val.(*ShlexStringSlice)
if !ok {
return nil
}

if s == nil {
return nil
}

return []string(*s)
}
9 changes: 6 additions & 3 deletions internal/project/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
"syscall"
)

func NewRunner() *Runner {
return &Runner{}
func NewRunner(args []string) *Runner {
return &Runner{
args: args,
}
}

type Runner struct {
args []string
}

func (r *Runner) Run(ctx context.Context, path string) (*Process, error) {
Expand All @@ -22,7 +25,7 @@ func (r *Runner) Run(ctx context.Context, path string) (*Process, error) {
}

runCtx, killFunc := context.WithCancel(ctx)
cmd := exec.CommandContext(runCtx, path)
cmd := exec.CommandContext(runCtx, path, r.args...)
cmd.Stdout = output
cmd.Stderr = output
if err := cmd.Start(); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/mokiat/gocrane/internal/crane"
"github.com/mokiat/gocrane/internal/flag"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -59,6 +60,12 @@ func main() {
Aliases: []string{"c"},
EnvVars: []string{"GOCRANE_CACHE"},
},
&cli.GenericFlag{
Name: "args",
Usage: "arguments to use when running the built executable",
EnvVars: []string{"GOCRANE_ARGS"},
Value: &flag.ShlexStringSlice{},
},
&cli.DurationFlag{
Name: "shutdown-timeout",
Usage: "amount of time to wait for program to exit gracefully",
Expand All @@ -74,6 +81,7 @@ func main() {
ExcludeGlobs: c.StringSlice("glob-exclude"),
RunDir: c.String("run"),
CachedBuild: c.String("cache"),
Args: flag.ShlexStrings(c.Generic("args")),
ShutdownTimeout: c.Duration("shutdown-timeout"),
})
},
Expand Down

0 comments on commit 427b290

Please sign in to comment.