Skip to content

Commit

Permalink
206-doublestar-mount Added glob path matching to mounts (initial)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdebyl committed Jul 18, 2022
1 parent bbec507 commit 0b6773b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cloud.google.com/go/storage v1.23.0
github.com/Azure/azure-storage-blob-go v0.15.0
github.com/aws/aws-sdk-go v1.44.55
github.com/bmatcuk/doublestar v1.3.4
github.com/dustin/go-humanize v1.0.0
github.com/go-kit/log v0.2.1
github.com/google/go-cmp v0.5.8
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/aws/aws-sdk-go v1.44.55 h1:h+p61sPEsLOpnQ2mKnGPrIe1MFUKwwA0X5eQYAcjOMU=
github.com/aws/aws-sdk-go v1.44.55/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/bmatcuk/doublestar v1.3.4 h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQrS0=
github.com/bmatcuk/doublestar v1.3.4/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down
24 changes: 23 additions & 1 deletion internal/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/bmatcuk/doublestar"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/meltwater/drone-cache/archive"
Expand Down Expand Up @@ -120,7 +122,27 @@ func (p *Plugin) Exec() error { // nolint: funlen,cyclop
options...,
)

// 4. Select mode
// 4. Glob match mounts if doublestar paths exist
for i, mount := range p.Config.Mount {
if strings.Contains(mount, "**") {
mountLen := len(p.Config.Mount)

// Remove the glob from the original mount list
p.Config.Mount[i] = p.Config.Mount[mountLen-1]
p.Config.Mount = p.Config.Mount[:mountLen-1]

mountGlob, err := doublestar.Glob(mount)
if err != nil {
return fmt.Errorf("glob mount error <%s>, %w", mount, err)
}

for _, match := range mountGlob {
p.Config.Mount = append(p.Config.Mount, match)
}
}
}

// 5. Select mode
if cfg.Rebuild {
if err := c.Rebuild(p.Config.Mount); err != nil {
level.Debug(p.logger).Log("err", fmt.Sprintf("%+v\n", err))
Expand Down
33 changes: 33 additions & 0 deletions internal/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func TestPlugin(t *testing.T) {
},
success: true,
},
{
name: "existing-mount-with-glob-files",
mount: func(name string) []string {
return exampleNestedFileTreeWithGlob(t, name, make([]byte, 1*1024))
},
success: true,
},
{
name: "existing-mount-with-cache-key",
mount: func(name string) []string {
Expand Down Expand Up @@ -312,6 +319,32 @@ func exampleFileTreeWithSymlinks(t *testing.T, name string, content []byte) []st
return []string{file, dir, symDir}
}

func exampleNestedFileTreeWithGlob(t *testing.T, name string, content []byte) []string {
name = sanitize(name)
name1 := fmt.Sprintf("%s1", name)

dir, cleanup := test.CreateTempDir(t, name, testRootMounted)
t.Cleanup(cleanup)

nestedDir, nestedDirClean := test.CreateTempDir(t, name, dir)
t.Cleanup(nestedDirClean)

nestedFile, nestedFileClean := test.CreateTempFile(t, name, content, nestedDir)
t.Cleanup(nestedFileClean)

nestedDir1, nestedDirClean1 := test.CreateTempDir(t, name1, dir)
t.Cleanup(nestedDirClean1)

nestedFile1, nestedFileClean1 := test.CreateTempFile(t, name1, content, nestedDir)
t.Cleanup(nestedFileClean1)

nestedFile2, nestedFileClean2 := test.CreateTempFile(t, name1, content, nestedDir1)
t.Cleanup(nestedFileClean2)

globPath := fmt.Sprintf("%s/**/%s", dir, nestedDir1)
return []string{nestedDir, nestedFile, globPath}
}

// Setup

func setupAzure(t *testing.T, c *Config, name string) {
Expand Down

0 comments on commit 0b6773b

Please sign in to comment.