From c38782464a5eb1251a9146ab95814b868f72fe43 Mon Sep 17 00:00:00 2001 From: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> Date: Tue, 18 Jun 2019 21:15:06 +0200 Subject: [PATCH] Simplify sync rules if `strip` is a suffix of `dest` Signed-off-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com> --- pkg/skaffold/schema/v1beta9/upgrade.go | 30 +++++++++++++++++++-- pkg/skaffold/schema/v1beta9/upgrade_test.go | 15 +++++------ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/pkg/skaffold/schema/v1beta9/upgrade.go b/pkg/skaffold/schema/v1beta9/upgrade.go index 6cb4d63bd52..8ad00375ca1 100644 --- a/pkg/skaffold/schema/v1beta9/upgrade.go +++ b/pkg/skaffold/schema/v1beta9/upgrade.go @@ -102,16 +102,18 @@ func (config *SkaffoldConfig) convertSyncRules() [][]*next.SyncRule { var syncRule *next.SyncRule switch { case compatibleSimplePattern.MatchString(src): + dest, strip := simplify(dest, compatibleSimplePattern.FindStringSubmatch(src)[1]) syncRule = &next.SyncRule{ Src: src, Dest: dest, - Strip: compatibleSimplePattern.FindStringSubmatch(src)[1], + Strip: strip, } case strings.Contains(src, "***"): + dest, strip := simplify(dest, strings.Split(src, "***")[0]) syncRule = &next.SyncRule{ Src: strings.Replace(src, "***", "**", -1), Dest: dest, - Strip: strings.Split(src, "***")[0], + Strip: strip, } default: // Incompatible patterns contain `**` or glob directories. @@ -135,3 +137,27 @@ func (config *SkaffoldConfig) convertSyncRules() [][]*next.SyncRule { } return newSync } + +// simplify dest and strip, if strip is a suffix of dest modulo a trailing `/`. +func simplify(dest, strip string) (string, string) { + if strip == "" || strip == "/" || dest == "" { + return dest, strip + } + + simpleStrip := strip + simpleDest := dest + + if dest[len(dest)-1] != '/' { + dest = dest + "/" + } + + if strings.HasSuffix(dest, strip) { + simpleDest = strings.TrimSuffix(dest, strings.TrimPrefix(strip, "/")) + simpleStrip = "" + if simpleDest == "" { + simpleDest = "." + } + } + + return simpleDest, simpleStrip +} diff --git a/pkg/skaffold/schema/v1beta9/upgrade_test.go b/pkg/skaffold/schema/v1beta9/upgrade_test.go index 7b356fa4ece..b9f38f78b3f 100644 --- a/pkg/skaffold/schema/v1beta9/upgrade_test.go +++ b/pkg/skaffold/schema/v1beta9/upgrade_test.go @@ -142,7 +142,7 @@ build: artifacts: - image: gcr.io/no-star-1 sync: - '/public/A/B/a.html': /www + '/public/A/B/a.html': /public/A/B - image: gcr.io/no-star-2 sync: '/b.html': /www @@ -151,10 +151,10 @@ build: 'c.html': /www - image: gcr.io/no-star-4 sync: - 'public/A/d.html': /www + 'public/A/d.html': public/A/ - image: gcr.io/single-star-1 sync: - 'public/*': /app + 'public/*': /app/public/ - image: gcr.io/single-star-2 sync: 'main*.js': /app @@ -185,8 +185,7 @@ build: sync: manual: - src: /public/A/B/a.html - dest: /www - strip: /public/A/B/ + dest: / - image: gcr.io/no-star-2 sync: manual: @@ -202,14 +201,12 @@ build: sync: manual: - src: public/A/d.html - dest: /www - strip: public/A/ + dest: . - image: gcr.io/single-star-1 sync: manual: - src: 'public/*' - dest: /app - strip: public/ + dest: /app/ - image: gcr.io/single-star-2 sync: manual: