From 1b4fb3def8c8d1606bcc1411aefc849fc392177b Mon Sep 17 00:00:00 2001 From: Mathieu Frenette Date: Sun, 27 Jun 2021 21:43:08 -0400 Subject: [PATCH] Pull latest/unspecified tag images before running --- src/cmd/run/run.go | 21 +++++++++++++++++---- src/cmd/run/run_test.go | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/cmd/run/run_test.go diff --git a/src/cmd/run/run.go b/src/cmd/run/run.go index 5cac02a..50be366 100644 --- a/src/cmd/run/run.go +++ b/src/cmd/run/run.go @@ -97,6 +97,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 { @@ -104,17 +110,24 @@ func run(ctx context.Context, names []string, options Options) error { } } - ////// - 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) { diff --git a/src/cmd/run/run_test.go b/src/cmd/run/run_test.go new file mode 100644 index 0000000..12e8779 --- /dev/null +++ b/src/cmd/run/run_test.go @@ -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")) +}