Skip to content

Commit

Permalink
Refactor to greatly simplify options, global flags, dry-run and docke…
Browse files Browse the repository at this point in the history
…r command execution
  • Loading branch information
silphid committed Jun 24, 2021
1 parent 84e5528 commit ce52e97
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 144 deletions.
12 changes: 4 additions & 8 deletions src/cmd/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// New creates a cobra command
func New() *cobra.Command {
var options options
var options docker.RemoveOptions

cmd := &cobra.Command{
Use: "remove",
Expand All @@ -25,16 +25,12 @@ func New() *cobra.Command {
},
}

cmd.Flags().BoolVarP(&options.force, "force", "f", false, "force removes container")
cmd.Flags().BoolVarP(&options.Force, "force", "f", false, "force removes container")

return cmd
}

type options struct {
force bool
}

func run(ctx context.Context, names []string, options options) error {
func run(ctx context.Context, names []string, options docker.RemoveOptions) error {
contexts, err := yey.LoadContexts()
if err != nil {
return err
Expand All @@ -52,5 +48,5 @@ func run(ctx context.Context, names []string, options options) error {

container := yey.ContainerName(contexts.Path, context)

return docker.Remove(ctx, container, docker.WithForceRemove(options.force))
return docker.Remove(ctx, container, options)
}
10 changes: 5 additions & 5 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package cmd

import (
"github.com/silphid/yey/src/internal/logging"
yey "github.com/silphid/yey/src/internal"
"github.com/spf13/cobra"
)

// NewRoot creates the root cobra command
func NewRoot() *cobra.Command {
c := &cobra.Command{
Use: "yey",
Short: "A DevOps & CI/CD & Kubernetes-oriented general purpose Docker container with CLI launcher",
Long: `A DevOps & CI/CD & Kubernetes-oriented general purpose Docker container with CLI launcher`,
Short: "An interactive, human-friendly docker launcher for dev and devops",
Long: "An interactive, human-friendly docker launcher for dev and devops",
SilenceUsage: true,
}

// var options internal.Options
c.PersistentFlags().BoolVarP(&logging.Verbose, "verbose", "v", false, "display verbose messages")
c.PersistentFlags().BoolVarP(&yey.IsVerbose, "verbose", "v", false, "output verbose messages to stderr")
c.PersistentFlags().BoolVar(&yey.IsDryRun, "dry-run", false, "output docker command to stdout instead of executing it")

return c
}
50 changes: 14 additions & 36 deletions src/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/silphid/yey/src/cmd"
yey "github.com/silphid/yey/src/internal"
"github.com/silphid/yey/src/internal/docker"
"github.com/silphid/yey/src/internal/logging"

"github.com/spf13/cobra"
)
Expand All @@ -33,17 +32,13 @@ 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
Verbose bool
DryRun bool
Remove *bool
Reset bool
}

func run(ctx context.Context, names []string, options Options) error {
Expand All @@ -67,63 +62,46 @@ func run(ctx context.Context, names []string, options Options) error {

if yeyContext.Image == "" {
var err error
yeyContext.Image, err = readAndBuildDockerfile(ctx, yeyContext.Build)
yeyContext.Image, err = readAndBuildDockerfile(ctx, yeyContext.Build, options)
if err != nil {
return fmt.Errorf("failed to build yey context image: %w", err)
}
logging.Log("using image: %s", yeyContext.Image)
yey.Log("using image: %s", yeyContext.Image)
}

if options.Verbose {
fmt.Fprintf(os.Stderr, "context:\n--\n%v--\n", yeyContext)
}
yey.Log("context:\n--\n%v--", yeyContext)

// Container name
containerName := yey.ContainerName(contexts.Path, yeyContext)
if options.Verbose {
fmt.Fprintf(os.Stderr, "container: %s\n", containerName)
}
yey.Log("container: %s", containerName)

// Reset
if options.Reset {
if options.Verbose {
fmt.Fprintf(os.Stderr, "removing container first")
}
if err := docker.Remove(ctx, containerName); err != nil {
yey.Log("removing container first")
if err := docker.Remove(ctx, containerName, docker.RemoveOptions{}); err != nil {
return fmt.Errorf("failed to remove container %q: %w", containerName, err)
}
}

var runOptions []docker.RunOption

// Working directory
var runOptions docker.RunOptions
workDir, err := getContainerWorkDir(yeyContext)
if err != nil {
return err
}
if workDir != "" {
runOptions = append(runOptions, docker.WithWorkDir(workDir))
runOptions.WorkDir = workDir
}
if options.Verbose {
fmt.Fprintf(os.Stderr, "working directory: %s", workDir)
}

// Dry-run/verbose
runOptions = append(runOptions,
docker.WithDryRun(options.DryRun),
docker.WithVerbose(options.Verbose))
yey.Log("working directory: %s", workDir)

// Banner
if options.Verbose {
fmt.Fprintln(os.Stderr)
}
if !options.DryRun {
if !yey.IsDryRun {
if err := ShowBanner(yeyContext.Name); err != nil {
return err
}
}

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

func getContainerWorkDir(yeyContext yey.Context) (string, error) {
Expand All @@ -148,7 +126,7 @@ func getContainerWorkDir(yeyContext yey.Context) (string, error) {
return "", nil
}

func readAndBuildDockerfile(ctx context.Context, build yey.DockerBuild) (string, error) {
func readAndBuildDockerfile(ctx context.Context, build yey.DockerBuild, options Options) (string, error) {
dockerBytes, err := os.ReadFile(build.Dockerfile)
if err != nil {
return "", fmt.Errorf("failed to read dockerfile: %w", err)
Expand Down
12 changes: 4 additions & 8 deletions src/cmd/tidy/tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// New creates a cobra command
func New() *cobra.Command {
var options options
var options docker.RemoveOptions

cmd := &cobra.Command{
Use: "tidy",
Expand All @@ -23,16 +23,12 @@ func New() *cobra.Command {
},
}

cmd.Flags().BoolVarP(&options.force, "force", "f", false, "removes containers forcibly")
cmd.Flags().BoolVarP(&options.Force, "force", "f", false, "removes containers forcibly")

return cmd
}

type options struct {
force bool
}

func run(ctx context.Context, options options) error {
func run(ctx context.Context, options docker.RemoveOptions) error {
contexts, err := yey.LoadContexts()
if err != nil {
return err
Expand Down Expand Up @@ -66,7 +62,7 @@ func run(ctx context.Context, options options) error {
unreferencedContainers = append(unreferencedContainers, container)
}

return docker.RemoveMany(ctx, unreferencedContainers, docker.WithForceRemove(options.force))
return docker.RemoveMany(ctx, unreferencedContainers, options)
}

// forEachPossibleNameCombination calls given callback function with each possible combination of names from all layers
Expand Down
Loading

0 comments on commit ce52e97

Please sign in to comment.