Skip to content

Commit

Permalink
add --make supported
Browse files Browse the repository at this point in the history
  • Loading branch information
shihyuho committed Sep 4, 2018
1 parent b13f684 commit 327b94f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ $ helm run [flags] COMMAND [ARGS]
```sh
Flags:
--always-pull-image always pull image before running command
-o, --command-owner string github owner of command (default "softleader")
-p, --command-path-base string github path base of command (default "helm")
-r, --command-repo string github repo of command (default "dockerfile")
--command-owner string github owner of command (default "softleader")
--command-path-base string github path base of command (default "helm")
--command-repo string github repo of command (default "dockerfile")
--dos2unix convert FILE from DOS to Unix format (default true)
--entrypoint stringArray the ENTRYPOINT of the image (default [/bin/bash])
-h, --help help for helm
-i, --image string image for running command (default "softleader/helm")
-l, --local command store on local storage, not on github
-m, --make executed via GNU make utility, not bash
--rm automatically remove the container when it exits (default true)
```

Expand Down
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ func main() {
},
}
f := cmd.Flags()
f.StringVarP(&runCmd.image, "image", "i", "softleader/helm", "image for running command")
f.BoolVarP(&runCmd.alwaysPullImage, "always-pull-image", "", false, "always pull image before running command")
f.BoolVarP(&runCmd.rm, "rm", "", true, "automatically remove the container when it exits")
f.StringArrayVarP(&runCmd.entryPoint, "entrypoint", "", []string{"/bin/bash"}, "the ENTRYPOINT of the image")
f.BoolVarP(&runCmd.local, "local", "l", false, "command store on local storage, not on github")
f.BoolVarP(&runCmd.make, "make", "m", false, "executed via GNU make utility, not bash")
f.BoolVarP(&runCmd.dos2unix, "dos2unix", "", true, "convert FILE from DOS to Unix format")
f.StringVarP(&runCmd.commandOwner, "command-owner", "o", commandOwner, "github owner of command")
f.StringVarP(&runCmd.commandRepo, "command-repo", "r", commandRepo, "github repo of command")
f.StringVarP(&runCmd.commandPathBase, "command-path-base", "p", commandPathBase, "github path base of command")
f.StringVarP(&runCmd.commandOwner, "command-owner", "", commandOwner, "github owner of command")
f.StringVarP(&runCmd.commandRepo, "command-repo", "", commandRepo, "github repo of command")
f.StringVarP(&runCmd.commandPathBase, "command-path-base", "", commandPathBase, "github path base of command")

if err := cmd.Execute(); err != nil {
os.Exit(1)
Expand Down
21 changes: 14 additions & 7 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
commandRepo = "dockerfile"
commandPathBase = "helm"
workDir = "/data"
image = "softleader/helm"
entrypoint = "/bin/bash"
)

type runCmd struct {
Expand All @@ -31,12 +33,11 @@ type runCmd struct {
commandPathBase string
command string
args []string
image string
alwaysPullImage bool
rm bool
entryPoint []string
local bool
dos2unix bool
make bool
}

func (cmd *runCmd) run() error {
Expand All @@ -48,7 +49,7 @@ func (cmd *runCmd) run() error {
}

if cmd.alwaysPullImage {
_, err = cli.ImagePull(ctx, cmd.image, types.ImagePullOptions{})
_, err = cli.ImagePull(ctx, image, types.ImagePullOptions{})
if err != nil {
return err
}
Expand All @@ -74,8 +75,8 @@ func (cmd *runCmd) run() error {

resp, err := cli.ContainerCreate(ctx,
&container.Config{
Image: cmd.image,
Entrypoint: cmd.entryPoint,
Image: image,
Entrypoint: []string{entrypoint},
WorkingDir: workDir,
Cmd: cmd.cmd(),
}, &container.HostConfig{
Expand Down Expand Up @@ -118,15 +119,21 @@ func (cmd *runCmd) run() error {
}

func (cmd *runCmd) cmd() strslice.StrSlice {
if cmd.dos2unix {
if cmd.make {
c := []string{fmt.Sprintf("make -f ./%s", cmd.command)}
c = append(c, cmd.args...)
return []string{"-c", strings.Join(c, " ")}
} else if cmd.dos2unix {
c := []string{fmt.Sprintf("cat ./%s | dos2unix | bash", cmd.command)}
if len(cmd.args) > 0 {
c = append(c, "-s")
c = append(c, cmd.args...)
}
return []string{"-c", strings.Join(c, " ")}
} else {
return append([]string{"./" + cmd.command}, cmd.args...)
c := []string{fmt.Sprintf("./%s", cmd.command)}
c = append(c, cmd.args...)
return c
}
}

Expand Down
47 changes: 40 additions & 7 deletions run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ func TestRun(t *testing.T) {
defer os.RemoveAll(tmp)

runCmd := runCmd{
dos2unix: true,
command: path.Join(strings.Replace(tmp, wd+"/", "", -1), "hello"),
args: []string{"Matt"},
image: "softleader/helm",
pwd: wd,
entryPoint: []string{"/bin/bash"},
local: true,
dos2unix: true,
command: path.Join(strings.Replace(tmp, wd+"/", "", -1), "hello"),
args: []string{"Matt"},
pwd: wd,
local: true,
}

ioutil.WriteFile(path.Join(tmp, "hello"), []byte(`
Expand All @@ -39,6 +37,41 @@ func TestRun(t *testing.T) {
}
}

func TestRun_Make(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Error(err)
}
tmp, err := ioutil.TempDir(wd, "helm-run")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(tmp)

runCmd := runCmd{
dos2unix: true,
command: path.Join(strings.Replace(tmp, wd+"/", "", -1), "hello"),
args: []string{"NAME=Matt"},
pwd: wd,
local: true,
make: true,
}

ioutil.WriteFile(path.Join(tmp, "hello"), []byte(`
NAME := anonymous
all: hello my_name
hello:
echo "hello"
my_name:
echo "$(NAME)"
`), defaultDirectoryPermission)

err = runCmd.run()
if err != nil {
t.Error(err)
}
}

func TestGetCommandContents(t *testing.T) {
//runCmd := runCmd{
// command: "package",
Expand Down

0 comments on commit 327b94f

Please sign in to comment.