From 9495ab917f94e6dc5896dbc52491338447437664 Mon Sep 17 00:00:00 2001 From: Michael Russell Date: Sat, 24 Nov 2018 13:53:04 +0100 Subject: [PATCH] Add option to specify custom docker arguments --- README.md | 2 +- lope.go | 10 ++++++++++ lope_integration_test.go | 13 +++++++++++++ lope_test.go | 22 +++++++++++++++++----- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index eee5f92..1b38a6c 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,6 @@ cp $GOPATH/github.com/Crazybus/lope/lope /usr/local/bin ### Planned -* Add option to specify custom docker flags * Get vagrant/virtualbox combo working * Make sure all images/names are unique so multiple lopes can be run at the same time * If using addMount add all .dot directories instead of mounting them @@ -225,3 +224,4 @@ cp $GOPATH/github.com/Crazybus/lope/lope /usr/local/bin * Mount the docker socket into the container * Automated ssh agent forwarding for OSX. https://github.com/uber-common/docker-ssh-agent-forward * Run as current user and group when bind mounting +* Add option to specify custom docker flags diff --git a/lope.go b/lope.go index d30c747..997caee 100644 --- a/lope.go +++ b/lope.go @@ -287,6 +287,13 @@ func (l *lope) addUserAndGroup() { } func (l *lope) runParams() { + + for _, a := range extraArgs { + for _, s := range strings.Split(a, " ") { + l.params = append(l.params, s) + } + } + l.params = append(l.params, l.cfg.image, "-c", strings.Join(l.cfg.cmd, " ")) } @@ -424,6 +431,7 @@ func (i *flagArray) Set(value string) error { var instructions flagArray var mountPaths flagArray +var extraArgs flagArray func main() { @@ -446,6 +454,8 @@ func main() { flag.Var(&mountPaths, "path", "Paths that will be mounted from the users home directory into lope. Path will be ignored if it isn't accessible. Can be specified multiple times") + flag.Var(&extraArgs, "arg", "Extra docker run arguments which will be appended to the docker run command. Can be specified multiple times") + noMount := flag.Bool("noMount", false, "Disable mounting the current working directory into the image") addMount := flag.Bool("addMount", false, "Setting this will add the directory into the image instead of mounting it") diff --git a/lope_integration_test.go b/lope_integration_test.go index 04efdb2..4e596ed 100644 --- a/lope_integration_test.go +++ b/lope_integration_test.go @@ -102,6 +102,19 @@ func TestLopeCli(t *testing.T) { "README.md", }, }, + { + "Run a command with an extra docker arg", + []string{ + "-noTty", + "-arg", + "--hostname=crazybus", + "alpine", + "hostname", + }, + []string{ + "crazybus", + }, + }, } for _, test := range tests { diff --git a/lope_test.go b/lope_test.go index 2a27ec5..982ce26 100644 --- a/lope_test.go +++ b/lope_test.go @@ -3,6 +3,7 @@ package main import ( "fmt" "net/url" + "reflect" "strings" "testing" ) @@ -40,20 +41,30 @@ func TestRunParams(t *testing.T) { var tests = []struct { description string cmd []string + extra []string image string - want string + want []string }{ { "Run command with image and cmd", []string{"command"}, + []string{}, "imageName", - "imageName -c command", + []string{"imageName", "-c", "command"}, }, { "Run command with image and multiple cmd args", []string{"command", "-arg"}, + []string{}, "imageName", - "imageName -c command -arg", + []string{"imageName", "-c", "command -arg"}, + }, + { + "Run command with an extra arg", + []string{"command", "-arg"}, + []string{"--ulimit 10"}, + "imageName", + []string{"--ulimit", "10", "imageName", "-c", "command -arg"}, }, } @@ -62,12 +73,13 @@ func TestRunParams(t *testing.T) { l.params = make([]string, 0) l.cfg.cmd = test.cmd l.cfg.image = test.image + extraArgs = test.extra l.runParams() - got := strings.Join(l.params, " ") + got := l.params want := test.want - if got != want { + if !reflect.DeepEqual(got, want) { t.Errorf("got %q want %q", got, want) } })