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...)