From 3a4a83fc91e7fa39c0a5c3e32b15cbf83f28844d Mon Sep 17 00:00:00 2001 From: davidmdm Date: Sun, 30 May 2021 01:00:59 -0400 Subject: [PATCH 1/2] workdir suggestions --- src/cmd/run/run.go | 7 ++++++- src/internal/contextFile.go | 23 ++++++++++++++++------- src/internal/docker/cli.go | 35 ++++++++++++++++++++++++++--------- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/cmd/run/run.go b/src/cmd/run/run.go index 255e37c..0c99536 100644 --- a/src/cmd/run/run.go +++ b/src/cmd/run/run.go @@ -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) { diff --git a/src/internal/contextFile.go b/src/internal/contextFile.go index 084e090..103fe46 100644 --- a/src/internal/contextFile.go +++ b/src/internal/contextFile.go @@ -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 != "" { @@ -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() diff --git a/src/internal/docker/cli.go b/src/internal/docker/cli.go index 093e0e3..0471913 100644 --- a/src/internal/docker/cli.go +++ b/src/internal/docker/cli.go @@ -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 { @@ -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) } @@ -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 @@ -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) @@ -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...) From d28d867b9e0e25fc2b80305e014fb0b6651d30a7 Mon Sep 17 00:00:00 2001 From: Mathieu Frenette Date: Tue, 1 Jun 2021 08:41:05 -0400 Subject: [PATCH 2/2] Change workDir capitalization --- src/cmd/run/run.go | 2 +- src/internal/docker/cli.go | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cmd/run/run.go b/src/cmd/run/run.go index 0c99536..01435d9 100644 --- a/src/cmd/run/run.go +++ b/src/cmd/run/run.go @@ -93,7 +93,7 @@ func run(ctx context.Context, name string, options Options) error { var runOptions []docker.RunOption if workDir != "" { - runOptions = append(runOptions, docker.WithWorkdir(workDir)) + runOptions = append(runOptions, docker.WithWorkDir(workDir)) } return docker.Start(ctx, yeyContext, containerName, runOptions...) diff --git a/src/internal/docker/cli.go b/src/internal/docker/cli.go index 0471913..90190e3 100644 --- a/src/internal/docker/cli.go +++ b/src/internal/docker/cli.go @@ -15,14 +15,14 @@ import ( ) type runOptions struct { - workdir string + workDir string } type RunOption func(*runOptions) -func WithWorkdir(wd string) RunOption { +func WithWorkDir(wd string) RunOption { return func(ro *runOptions) { - ro.workdir = wd + ro.workDir = wd } } @@ -158,8 +158,8 @@ func runContainer(ctx context.Context, yeyCtx yey.Context, containerName string, args = append(args, "--rm") } - if options.workdir != "" { - args = append(args, "--workdir", options.workdir) + if options.workDir != "" { + args = append(args, "--workdir", options.workDir) } args = append(args, yeyCtx.Image) @@ -174,8 +174,8 @@ func startContainer(ctx context.Context, containerName string) error { func execContainer(ctx context.Context, containerName string, cmd []string, options runOptions) error { args := []string{"exec", "-ti"} - if options.workdir != "" { - args = append(args, "--workdir", options.workdir) + if options.workDir != "" { + args = append(args, "--workdir", options.workDir) } args = append(args, containerName) args = append(args, cmd...)