From 84e552823741fbc6ebb441c538b70ee1bb504845 Mon Sep 17 00:00:00 2001 From: Mathieu Frenette Date: Tue, 22 Jun 2021 19:25:08 -0400 Subject: [PATCH] Add --dry-run and --verbose flags --- src/cmd/run/run.go | 43 ++++++++++++++++++++++++++++++++------ src/internal/docker/cli.go | 34 +++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/src/cmd/run/run.go b/src/cmd/run/run.go index 8e9b451..6d2056f 100644 --- a/src/cmd/run/run.go +++ b/src/cmd/run/run.go @@ -33,13 +33,17 @@ func New() *cobra.Command { cmd.Flags().BoolVar(options.Remove, "rm", false, "remove container upon exit") cmd.Flags().BoolVar(&options.Reset, "reset", false, "remove previous container before starting a fresh one") + cmd.Flags().BoolVarP(&options.Verbose, "verbose", "v", false, "output detailed execution information to stderr") + cmd.Flags().BoolVar(&options.DryRun, "dry-run", false, "output docker command to stdout instead of executing it") return cmd } type Options struct { - Remove *bool - Reset bool + Remove *bool + Reset bool + Verbose bool + DryRun bool } func run(ctx context.Context, names []string, options Options) error { @@ -70,26 +74,53 @@ func run(ctx context.Context, names []string, options Options) error { logging.Log("using image: %s", yeyContext.Image) } + if options.Verbose { + fmt.Fprintf(os.Stderr, "context:\n--\n%v--\n", yeyContext) + } + + // Container name containerName := yey.ContainerName(contexts.Path, yeyContext) + if options.Verbose { + fmt.Fprintf(os.Stderr, "container: %s\n", containerName) + } + // Reset if options.Reset { + if options.Verbose { + fmt.Fprintf(os.Stderr, "removing container first") + } if err := docker.Remove(ctx, containerName); err != nil { return fmt.Errorf("failed to remove container %q: %w", containerName, err) } } + var runOptions []docker.RunOption + + // Working directory workDir, err := getContainerWorkDir(yeyContext) if err != nil { return err } - - var runOptions []docker.RunOption if workDir != "" { runOptions = append(runOptions, docker.WithWorkDir(workDir)) } + if options.Verbose { + fmt.Fprintf(os.Stderr, "working directory: %s", workDir) + } - if err := ShowBanner(yeyContext.Name); err != nil { - return err + // Dry-run/verbose + runOptions = append(runOptions, + docker.WithDryRun(options.DryRun), + docker.WithVerbose(options.Verbose)) + + // Banner + if options.Verbose { + fmt.Fprintln(os.Stderr) + } + if !options.DryRun { + if err := ShowBanner(yeyContext.Name); err != nil { + return err + } } return docker.Start(ctx, yeyContext, containerName, runOptions...) diff --git a/src/internal/docker/cli.go b/src/internal/docker/cli.go index fd9fb7c..a0a37ce 100644 --- a/src/internal/docker/cli.go +++ b/src/internal/docker/cli.go @@ -16,6 +16,8 @@ import ( type runOptions struct { workDir string + verbose bool + dryRun bool } type RunOption func(*runOptions) @@ -26,6 +28,18 @@ func WithWorkDir(wd string) RunOption { } } +func WithVerbose(value bool) RunOption { + return func(ro *runOptions) { + ro.verbose = value + } +} + +func WithDryRun(value bool) RunOption { + return func(ro *runOptions) { + ro.dryRun = value + } +} + func Start(ctx context.Context, yeyCtx yey.Context, containerName string, opts ...RunOption) error { var options runOptions for _, opt := range opts { @@ -42,7 +56,7 @@ func Start(ctx context.Context, yeyCtx yey.Context, containerName string, opts . case "": return runContainer(ctx, yeyCtx, containerName, options) case "exited": - return startContainer(ctx, containerName) + return startContainer(ctx, containerName, options) case "running": return execContainer(ctx, containerName, yeyCtx.Cmd, options) default: @@ -205,11 +219,21 @@ func runContainer(ctx context.Context, yeyCtx yey.Context, containerName string, args = append(args, yeyCtx.Image) args = append(args, yeyCtx.Cmd...) + if options.dryRun { + fmt.Printf("docker %s\n", strings.Join(args, " ")) + return nil + } + return attachStdPipes(exec.CommandContext(ctx, "docker", args...)).Run() } -func startContainer(ctx context.Context, containerName string) error { - return attachStdPipes(exec.CommandContext(ctx, "docker", "start", "-i", containerName)).Run() +func startContainer(ctx context.Context, containerName string, options runOptions) error { + args := []string{"start", "-i", containerName} + if options.dryRun { + fmt.Printf("docker %s\n", strings.Join(args, " ")) + return nil + } + return attachStdPipes(exec.CommandContext(ctx, "docker", args...)).Run() } func execContainer(ctx context.Context, containerName string, cmd []string, options runOptions) error { @@ -219,6 +243,10 @@ func execContainer(ctx context.Context, containerName string, cmd []string, opti } args = append(args, containerName) args = append(args, cmd...) + if options.dryRun { + fmt.Printf("docker %s\n", strings.Join(args, " ")) + return nil + } return attachStdPipes(exec.CommandContext(ctx, "docker", args...)).Run() }