Skip to content

Commit

Permalink
workdir suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed May 30, 2021
1 parent 1fd116a commit 3a4a83f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ func run(ctx context.Context, name string, options Options) error {
return err
}

return docker.Start(ctx, yeyContext, containerName, workDir)
var runOptions []docker.RunOption
if workDir != "" {
runOptions = append(runOptions, docker.WithWorkdir(workDir))
}

return docker.Start(ctx, yeyContext, containerName, runOptions...)
}

func getContainerWorkDir(yeyContext yey.Context) (string, error) {
Expand Down
23 changes: 16 additions & 7 deletions src/internal/contextFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,10 @@ func parseContextFile(dir string, data []byte) (Contexts, error) {

if dir != "" {
var err error
contexts.Context, err = resolveContextPaths(dir, contexts.Context)
contexts, err = resolveContextsPaths(dir, contexts)
if err != nil {
return Contexts{}, err
}
for name, context := range contexts.Named {
contexts.Named[name], err = resolveContextPaths(dir, context)
if err != nil {
return Contexts{}, err
}
}
}

if ctxFile.Parent != "" {
Expand Down Expand Up @@ -152,6 +146,21 @@ func LoadContexts() (Contexts, error) {
return contexts, nil
}

func resolveContextsPaths(dir string, contexts Contexts) (Contexts, error) {
var err error
contexts.Context, err = resolveContextPaths(dir, contexts.Context)
if err != nil {
return Contexts{}, err
}
for name, context := range contexts.Named {
contexts.Named[name], err = resolveContextPaths(dir, context)
if err != nil {
return Contexts{}, err
}
}
return contexts, nil
}

func resolveContextPaths(dir string, context Context) (Context, error) {
clone := context.Clone()

Expand Down
35 changes: 26 additions & 9 deletions src/internal/docker/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,24 @@ import (
"github.com/silphid/yey/src/internal/logging"
)

func Start(ctx context.Context, yeyCtx yey.Context, containerName, workDir string) error {
type runOptions struct {
workdir string
}

type RunOption func(*runOptions)

func WithWorkdir(wd string) RunOption {
return func(ro *runOptions) {
ro.workdir = wd
}
}

func Start(ctx context.Context, yeyCtx yey.Context, containerName string, opts ...RunOption) error {
var options runOptions
for _, opt := range opts {
opt(&options)
}

// Determine whether we need to run or exec container
status, err := getContainerStatus(ctx, containerName)
if err != nil {
Expand All @@ -23,11 +40,11 @@ func Start(ctx context.Context, yeyCtx yey.Context, containerName, workDir strin

switch status {
case "":
return runContainer(ctx, yeyCtx, containerName, workDir)
return runContainer(ctx, yeyCtx, containerName, options)
case "exited":
return startContainer(ctx, containerName)
case "running":
return execContainer(ctx, containerName, workDir, yeyCtx.Cmd)
return execContainer(ctx, containerName, yeyCtx.Cmd, options)
default:
return fmt.Errorf("container %q in unexpected state %q", containerName, status)
}
Expand Down Expand Up @@ -105,7 +122,7 @@ func getContainerStatus(ctx context.Context, name string) (string, error) {
return strings.TrimSpace(string(output)), nil
}

func runContainer(ctx context.Context, yeyCtx yey.Context, containerName, workDir string) error {
func runContainer(ctx context.Context, yeyCtx yey.Context, containerName string, options runOptions) error {
cwd, err := os.Getwd()
if err != nil {
return err
Expand Down Expand Up @@ -141,8 +158,8 @@ func runContainer(ctx context.Context, yeyCtx yey.Context, containerName, workDi
args = append(args, "--rm")
}

if workDir != "" {
args = append(args, "--workdir", workDir)
if options.workdir != "" {
args = append(args, "--workdir", options.workdir)
}

args = append(args, yeyCtx.Image)
Expand All @@ -155,10 +172,10 @@ func startContainer(ctx context.Context, containerName string) error {
return attachStdPipes(exec.CommandContext(ctx, "docker", "start", "-i", containerName)).Run()
}

func execContainer(ctx context.Context, containerName, workDir string, cmd []string) error {
func execContainer(ctx context.Context, containerName string, cmd []string, options runOptions) error {
args := []string{"exec", "-ti"}
if workDir != "" {
args = append(args, "--workdir", workDir)
if options.workdir != "" {
args = append(args, "--workdir", options.workdir)
}
args = append(args, containerName)
args = append(args, cmd...)
Expand Down

0 comments on commit 3a4a83f

Please sign in to comment.