Skip to content

Commit

Permalink
Merge pull request #31 from silphid/pull-before-run
Browse files Browse the repository at this point in the history
Pull latest/unspecified tag images before running
  • Loading branch information
silphid authored Jun 30, 2021
2 parents c135835 + 1b4fb3d commit d01f89a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/silphid/yey/src/cmd"
Expand Down Expand Up @@ -32,13 +33,15 @@ 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().BoolVar(&options.Pull, "pull", false, "force pulling image from registry before running")

return cmd
}

type Options struct {
Remove *bool
Reset bool
Pull bool
}

func run(ctx context.Context, names []string, options Options) error {
Expand Down Expand Up @@ -104,6 +107,12 @@ func run(ctx context.Context, names []string, options Options) error {
}
yey.Log("working directory: %s", workDir)

// Pull image first?
if options.Pull || shouldPull(yeyContext.Image) {
yey.Log("Pulling %s", yeyContext.Image)
docker.Pull(ctx, yeyContext.Image)
}

// Banner
if !yey.IsDryRun {
if err := ShowBanner(yeyContext.Name); err != nil {
Expand All @@ -114,6 +123,23 @@ func run(ctx context.Context, names []string, options Options) error {
return docker.Run(ctx, yeyContext, containerName, runOptions)
}

var tagRegex = regexp.MustCompile(`.*/.*:(.*)`)

func getTagFromImageName(imageName string) string {
groups := tagRegex.FindStringSubmatch(imageName)
if groups == nil {
return ""
}
return groups[1]
}

// shouldPull returns whether image should be pulled before running it.
// It returns true when image tag is `latest` or when it is not specified.
func shouldPull(imageName string) bool {
tag := getTagFromImageName(imageName)
return tag == "" || tag == "latest"
}

func getContainerWorkDir(yeyContext yey.Context) (string, error) {
workDir, err := os.Getwd()
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions src/cmd/run/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package run

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetTagFromImageName(t *testing.T) {
assert.Equal(t, "", getTagFromImageName(""))
assert.Equal(t, "", getTagFromImageName("gcr.io/project-1a2b3c4d5e/abcdef/image"))
assert.Equal(t, "latest", getTagFromImageName("gcr.io/project-1a2b3c4d5e/abcdef/image:latest"))
assert.Equal(t, "0.123.4", getTagFromImageName("gcr.io/project-1a2b3c4d5e/abcdef/image:0.123.4"))
}

func TestShouldPull(t *testing.T) {
assert.Equal(t, true, shouldPull(""))
assert.Equal(t, true, shouldPull("gcr.io/project-1a2b3c4d5e/abcdef/image"))
assert.Equal(t, true, shouldPull("gcr.io/project-1a2b3c4d5e/abcdef/image:latest"))
assert.Equal(t, false, shouldPull("gcr.io/project-1a2b3c4d5e/abcdef/image:0.123.4"))
}

0 comments on commit d01f89a

Please sign in to comment.