Skip to content

Commit

Permalink
Use a better glob library
Browse files Browse the repository at this point in the history
  • Loading branch information
DrJosh9000 committed Sep 5, 2023
1 parent d4ba3a1 commit 7ffbf95
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
36 changes: 22 additions & 14 deletions agent/artifact_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"context"
"crypto/sha1"
"crypto/sha256"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"

"github.com/DrJosh9000/zzglob"
"github.com/buildkite/agent/v3/api"
"github.com/buildkite/agent/v3/internal/artifact"
"github.com/buildkite/agent/v3/internal/experiments"
Expand All @@ -22,7 +23,6 @@ import (
"github.com/buildkite/agent/v3/pool"
"github.com/buildkite/roko"
"github.com/dustin/go-humanize"
zglob "github.com/mattn/go-zglob"
)

const (
Expand Down Expand Up @@ -125,19 +125,27 @@ func (a *ArtifactUploader) Collect(ctx context.Context) (artifacts []*api.Artifa

a.logger.Debug("Searching for %s", globPath)

// Resolve the globs (with * and ** in them), if it's a non-globbed path and doesn't exists
// then we will get the ErrNotExist that is handled below
globfunc := zglob.Glob
if a.conf.GlobResolveFollowSymlinks {
// Follow symbolic links for files & directories while expanding globs
globfunc = zglob.GlobFollowSymlinks
// Resolve the globs (with * and ** in them)
pattern, err := zzglob.Parse(globPath)
if err != nil {
return nil, fmt.Errorf("invalid glob pattern: %w", err)
}
files, err := globfunc(globPath)
if errors.Is(err, os.ErrNotExist) {
a.logger.Info("File not found: %s", globPath)
continue
} else if err != nil {
return nil, fmt.Errorf("resolving glob: %w", err)
var files []string
walkDirFunc := func(path string, d fs.DirEntry, err error) error {
if err != nil {
a.logger.Warn("Couldn't walk path %s", path)
return nil
}
if d != nil && d.IsDir() {
a.logger.Warn("Glob pattern %s matched a directory %s", globPath, path)
return nil
}
files = append(files, path)
return nil
}
err = pattern.Glob(walkDirFunc, zzglob.TraverseSymlinks(a.conf.GlobResolveFollowSymlinks))
if err != nil {
return nil, fmt.Errorf("globbing pattern: %w", err)
}

// Process each glob match into an api.Artifact
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.1
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.1.0
github.com/DataDog/datadog-go/v5 v5.3.0
github.com/DrJosh9000/zzglob v0.0.15
github.com/aws/aws-sdk-go v1.44.334
github.com/brunoscheufler/aws-ecs-metadata-go v0.0.0-20220812150832-b6b31c6eeeaf
github.com/buildkite/bintest/v3 v3.1.1
Expand All @@ -22,7 +23,6 @@ require (
github.com/google/go-cmp v0.5.9
github.com/google/go-querystring v1.1.0
github.com/lestrrat-go/jwx/v2 v2.0.12
github.com/mattn/go-zglob v0.0.4
github.com/mitchellh/go-homedir v1.1.0
github.com/oleiade/reflections v1.0.1
github.com/opentracing/opentracing-go v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/DataDog/go-tuf v1.0.1-0.5.2/go.mod h1:zBcq6f654iVqmkk8n2Cx81E1JnNTMOA
github.com/DataDog/gostackparse v0.5.0 h1:jb72P6GFHPHz2W0onsN51cS3FkaMDcjb0QzgxxA4gDk=
github.com/DataDog/sketches-go v1.2.1 h1:qTBzWLnZ3kM2kw39ymh6rMcnN+5VULwFs++lEYUUsro=
github.com/DataDog/sketches-go v1.2.1/go.mod h1:1xYmPLY1So10AwxV6MJV0J53XVH+WL9Ad1KetxVivVI=
github.com/DrJosh9000/zzglob v0.0.15 h1:LoukPRH8OHn6hs3wAzoz8VoszQBAe8R5H6SmGiNhkqU=
github.com/DrJosh9000/zzglob v0.0.15/go.mod h1:+iLI/qvROFsS1A/jl+B8MVYNu/0cjveYOJqU37O8fcs=
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
Expand Down Expand Up @@ -182,8 +184,6 @@ github.com/lestrrat-go/jwx/v2 v2.0.12/go.mod h1:Mq4KN1mM7bp+5z/W5HS8aCNs5RKZ911G
github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
github.com/mattn/go-zglob v0.0.4 h1:LQi2iOm0/fGgu80AioIJ/1j9w9Oh+9DZ39J4VAGzHQM=
github.com/mattn/go-zglob v0.0.4/go.mod h1:MxxjyoXXnMxfIpxTK2GAkw1w8glPsQILx3N5wrKakiY=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/oleiade/reflections v1.0.1 h1:D1XO3LVEYroYskEsoSiGItp9RUxG6jWnCVvrqH0HHQM=
Expand Down

0 comments on commit 7ffbf95

Please sign in to comment.