Skip to content

Commit

Permalink
Simplify sync rules if strip is a suffix of dest
Browse files Browse the repository at this point in the history
Signed-off-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com>
  • Loading branch information
corneliusweig authored and dgageot committed Jun 19, 2019
1 parent 18301e2 commit 14bf34e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
30 changes: 28 additions & 2 deletions pkg/skaffold/schema/v1beta9/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 += "/"
}

if strings.HasSuffix(dest, strip) {
simpleDest = strings.TrimSuffix(dest, strings.TrimPrefix(strip, "/"))
simpleStrip = ""
if simpleDest == "" {
simpleDest = "."
}
}

return simpleDest, simpleStrip
}
15 changes: 6 additions & 9 deletions pkg/skaffold/schema/v1beta9/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 14bf34e

Please sign in to comment.