Skip to content

Commit

Permalink
feature(docker): added working context flag
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsuguimoto committed Aug 3, 2023
1 parent e53435f commit acaac61
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
14 changes: 13 additions & 1 deletion commands/docker.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package commands

import (
"fmt"
"kool-dev/kool/core/builder"
"kool-dev/kool/core/environment"
"os"
"path"
"runtime"

"github.com/spf13/cobra"
Expand All @@ -15,6 +17,7 @@ type KoolDockerFlags struct {
Volumes []string
Publish []string
Network []string
Context string
}

// KoolDocker holds handlers and functions to implement the docker command logic
Expand All @@ -39,7 +42,7 @@ func AddKoolDocker(root *cobra.Command) {
func NewKoolDocker() *KoolDocker {
return &KoolDocker{
*newDefaultKoolService(),
&KoolDockerFlags{[]string{}, []string{}, []string{}, []string{}},
&KoolDockerFlags{[]string{}, []string{}, []string{}, []string{}, ""},
environment.NewEnvStorage(),
builder.NewCommand("docker", "run", "--init", "--rm", "-w", "/app", "-i"),
}
Expand All @@ -49,6 +52,14 @@ func NewKoolDocker() *KoolDocker {
func (d *KoolDocker) Execute(args []string) (err error) {
workDir, _ := os.Getwd()

if context := d.Flags.Context; context != "" {
workDir = path.Join(workDir, context)
if _, err = os.Stat(workDir); err != nil {
err = fmt.Errorf("please enter a valid context directory")
return
}
}

if d.Shell().IsTerminal() {
d.dockerRun.AppendArgs("-t")
}
Expand Down Expand Up @@ -107,6 +118,7 @@ the [COMMAND] to provide optional arguments required by the COMMAND.`,
cmd.Flags().StringArrayVarP(&docker.Flags.Volumes, "volume", "v", []string{}, "Bind mount a volume.")
cmd.Flags().StringArrayVarP(&docker.Flags.Publish, "publish", "p", []string{}, "Publish a container's port(s) to the host.")
cmd.Flags().StringArrayVarP(&docker.Flags.Network, "network", "n", []string{}, "Connect a container to a network.")
cmd.Flags().StringVarP(&docker.Flags.Context, "context", "c", "", "Working directory context.")

//After a non-flag arg, stop parsing flags
cmd.Flags().SetInterspersed(false)
Expand Down
41 changes: 39 additions & 2 deletions commands/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"kool-dev/kool/core/environment"
"kool-dev/kool/core/shell"
"os"
"path"
"runtime"
"testing"
)

func newFakeKoolDocker() *KoolDocker {
return &KoolDocker{
*(newDefaultKoolService().Fake()),
&KoolDockerFlags{[]string{}, []string{}, []string{}, []string{}},
&KoolDockerFlags{[]string{}, []string{}, []string{}, []string{}, ""},
environment.NewFakeEnvStorage(),
&builder.FakeCommand{MockCmd: "docker"},
}
Expand All @@ -23,7 +24,7 @@ func newFakeKoolDocker() *KoolDocker {
func newFailedFakeKoolDocker() *KoolDocker {
return &KoolDocker{
*(newDefaultKoolService().Fake()),
&KoolDockerFlags{[]string{}, []string{}, []string{}, []string{}},
&KoolDockerFlags{[]string{}, []string{}, []string{}, []string{}, ""},
environment.NewFakeEnvStorage(),
&builder.FakeCommand{MockCmd: "docker", MockInteractiveError: errors.New("error docker")},
}
Expand All @@ -50,6 +51,10 @@ func TestNewKoolDocker(t *testing.T) {
if len(k.Flags.Publish) > 0 {
t.Errorf("bad default value for Publish flag on default KoolDocker instance")
}

if k.Flags.Context != "" {
t.Errorf("bad default value for Context flag on default KoolDocker instance")
}
}

if _, ok := k.dockerRun.(*builder.DefaultCommand); !ok {
Expand Down Expand Up @@ -269,3 +274,35 @@ func TestNonTerminalNewDockerCommand(t *testing.T) {
t.Errorf("bad arguments to KoolDocker.dockerRun Command on non terminal environment")
}
}

func TestContextFlagNewDockerCommand(t *testing.T) {
f := newFakeKoolDocker()
f.shell.(*shell.FakeShell).MockIsTerminal = false
cmd := NewDockerCommand(f)

cmd.SetArgs([]string{"--context=context_test", "image"})

workDir, _ := os.Getwd()
workDir = path.Join(workDir, "context_test")
_ = os.MkdirAll(workDir, 0755)

if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error executing docker command; error: %v", err)
}

argsAppend := f.dockerRun.(*builder.FakeCommand).ArgsAppend

if argsAppend[0] != "--volume" || argsAppend[1] != workDir+":/app:delegated" {
t.Errorf("bad arguments to KoolDocker.dockerRun Command with context flag: %v", argsAppend)
}
}

func TestInvalidContextFlagNewDockerCommand(t *testing.T) {
f := newFakeKoolDocker()
f.shell.(*shell.FakeShell).MockIsTerminal = false
cmd := NewDockerCommand(f)

cmd.SetArgs([]string{"--context=invalid_context", "image"})

assertExecGotError(t, cmd, "please enter a valid context directory")
}
1 change: 1 addition & 0 deletions docs/4-Commands/kool-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ kool docker [OPTIONS] IMAGE [COMMAND] [--] [ARG...]
### Options

```
-c, --context string Working directory context.
-e, --env stringArray Environment variables.
-h, --help help for docker
-n, --network stringArray Connect a container to a network.
Expand Down

0 comments on commit acaac61

Please sign in to comment.