Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
log "github.com/sirupsen/logrus"
)

var errSkip = fmt.Errorf("skip")

// Plugin defines the S3 plugin parameters.
type Plugin struct {
Endpoint string
Expand Down Expand Up @@ -167,9 +169,16 @@ func (p *Plugin) Exec() error {
anyMatched := false

for _, match := range matches {
// skip directories
if isDir(match, matches) {
continue
// check directories and fail if directory without glob pattern
if err := isDir(match, matches); err != nil {
if err == errSkip {
continue
}
log.WithFields(log.Fields{
"error": err,
"match": match,
}).Error("Directory specified without glob pattern")
return err
}

// Preview stripping (wildcard for absolute patterns, literal for relative patterns)
Expand Down Expand Up @@ -411,11 +420,11 @@ func resolveSource(sourceDir, source, stripPrefix string) string {
return stripPrefix + path
}

// checks if the source path is a dir
func isDir(source string, matches []string) bool {
// checks if the source path is a dir and returns error if directory found without glob patterns
func isDir(source string, matches []string) error {
stat, err := os.Stat(source)
if err != nil {
return true // should never happen
return errSkip // skip non-existent files
}
if stat.IsDir() {
count := 0
Expand All @@ -425,11 +434,11 @@ func isDir(source string, matches []string) bool {
}
}
if count <= 1 {
log.Warnf("Skipping '%s' since it is a directory. Please use correct glob expression if this is unexpected.", source)
return fmt.Errorf("directory '%s' specified without glob pattern. Use a pattern like '%s/*' or '%s/**' to upload directory contents", source, source, source)
}
return true
return errSkip // skip directory but allow its children to be processed
}
return false
return nil
}

// normalizePath converts the path to a forward slash format and trims the prefix.
Expand Down
91 changes: 91 additions & 0 deletions plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"os"
"path/filepath"
"strings"
"testing"
)

func TestIsDir(t *testing.T) {
// Create temporary directory for testing
tmpDir := t.TempDir()
testDir := filepath.Join(tmpDir, "testdir")
testFile := filepath.Join(tmpDir, "testfile.txt")

// Create a test directory
err := os.Mkdir(testDir, 0755)
if err != nil {
t.Fatalf("Failed to create test directory: %v", err)
}

// Create a test file
file, err := os.Create(testFile)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
file.Close()

tests := []struct {
name string
source string
matches []string
expectError bool
expectSkip bool
errorContains string
}{
{
name: "file should not error",
source: testFile,
matches: []string{testFile},
expectError: false,
expectSkip: false,
},
{
name: "directory without glob should error",
source: testDir,
matches: []string{testDir},
expectError: true,
expectSkip: false,
errorContains: "specified without glob pattern",
},
{
name: "directory with glob pattern should skip",
source: testDir,
matches: []string{testDir + "/file1.txt", testDir + "/file2.txt"},
expectError: false,
expectSkip: true,
},
{
name: "non-existent path should skip",
source: "/non/existent/path",
matches: []string{},
expectError: false,
expectSkip: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := isDir(tc.source, tc.matches)

if tc.expectError {
if err == nil {
t.Errorf("Expected error but got none")
} else if err == errSkip {
t.Errorf("Expected fatal error but got skip error")
} else if tc.errorContains != "" && !strings.Contains(err.Error(), tc.errorContains) {
t.Errorf("Expected error to contain '%s', but got: %v", tc.errorContains, err)
}
} else if tc.expectSkip {
if err != errSkip {
t.Errorf("Expected skip error but got: %v", err)
}
} else {
if err != nil {
t.Errorf("Expected no error but got: %v", err)
}
}
})
}
}
1 change: 1 addition & 0 deletions plugin_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,4 @@ func TestResolveSource(t *testing.T) {
}
}
}