Skip to content

Commit

Permalink
Pull latest/unspecified tag images before running
Browse files Browse the repository at this point in the history
  • Loading branch information
silphid committed Jun 28, 2021
1 parent 751843b commit 1b4fb3d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,37 @@ 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 {
return err
}
}

//////

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

var asdf = regexp.MustCompile(`.*`)
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) {
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 1b4fb3d

Please sign in to comment.