Skip to content

Commit 56cdc0b

Browse files
committed
Minor changes
1 parent b8c5bf4 commit 56cdc0b

File tree

2 files changed

+39
-86
lines changed

2 files changed

+39
-86
lines changed

plugin.go

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/aws/aws-sdk-go/service/sts"
1818
"github.com/mattn/go-zglob"
1919
log "github.com/sirupsen/logrus"
20-
"golang.org/x/sync/errgroup"
2120
)
2221

2322
// Plugin defines the S3 plugin parameters.
@@ -104,9 +103,9 @@ type Plugin struct {
104103
func (p *Plugin) Exec() error {
105104
// normalize the target URL
106105
if p.Download {
107-
p.Source = resolveDir(p.Source)
106+
p.Source = normalizePath(p.Source)
108107
} else {
109-
p.Target = resolveDir(p.Target)
108+
p.Target = normalizePath(p.Target)
110109
}
111110

112111
// create the client
@@ -165,58 +164,52 @@ func (p *Plugin) Exec() error {
165164
return err
166165
}
167166

168-
g := errgroup.Group{}
169-
170167
for _, item := range list.Contents {
171168
log.WithFields(log.Fields{
172169
"bucket": p.Bucket,
173170
"key": *item.Key,
174171
}).Info("Getting S3 object")
175172

176-
item := item
177-
g.Go(func() error {
178-
obj, err := client.GetObject(&s3.GetObjectInput{
179-
Bucket: &p.Bucket,
180-
Key: item.Key,
181-
})
182-
if err != nil {
183-
log.WithFields(log.Fields{
184-
"error": err,
185-
"bucket": p.Bucket,
186-
"key": *item.Key,
187-
}).Error("Cannot get S3 object")
188-
return err
189-
}
190-
191-
// resolveSource takes a target directory, a target path, and a prefix to strip,
192-
// and returns a resolved source path by removing the targetDir from the target
193-
// and appending the stripPrefix.
194-
target := resolveSource(sourceDir, *item.Key, p.StripPrefix)
195-
196-
f, err := os.Create(target)
197-
if err != nil {
198-
log.WithFields(log.Fields{
199-
"error": err,
200-
"file": target,
201-
}).Error("Failed to create file")
202-
return err
203-
}
204-
defer f.Close()
205-
206-
_, err = io.Copy(f, obj.Body)
207-
if err != nil {
208-
log.WithFields(log.Fields{
209-
"error": err,
210-
"file": target,
211-
}).Error("Failed to write file")
212-
return err
213-
}
214-
215-
return nil
173+
obj, err := client.GetObject(&s3.GetObjectInput{
174+
Bucket: &p.Bucket,
175+
Key: item.Key,
216176
})
177+
if err != nil {
178+
log.WithFields(log.Fields{
179+
"error": err,
180+
"bucket": p.Bucket,
181+
"key": *item.Key,
182+
}).Error("Cannot get S3 object")
183+
return err
184+
}
185+
defer obj.Body.Close()
186+
187+
// resolveSource takes a target directory, a target path, and a prefix to strip,
188+
// and returns a resolved source path by removing the targetDir from the target
189+
// and appending the stripPrefix.
190+
target := resolveSource(sourceDir, *item.Key, p.StripPrefix)
191+
192+
f, err := os.Create(target)
193+
if err != nil {
194+
log.WithFields(log.Fields{
195+
"error": err,
196+
"file": target,
197+
}).Error("Failed to create file")
198+
return err
199+
}
200+
defer f.Close()
201+
202+
_, err = io.Copy(f, obj.Body)
203+
if err != nil {
204+
log.WithFields(log.Fields{
205+
"error": err,
206+
"file": target,
207+
}).Error("Failed to write file")
208+
return err
209+
}
217210
}
218211

219-
return g.Wait()
212+
return nil
220213
}
221214

222215
// find the bucket
@@ -435,11 +428,6 @@ func isDir(source string, matches []string) bool {
435428
return false
436429
}
437430

438-
func resolveDir(s string) string {
439-
res := strings.TrimPrefix(filepath.ToSlash(s), "/")
440-
return res
441-
}
442-
443431
// normalizePath converts the path to a forward slash format and trims the prefix.
444432
func normalizePath(source string) string {
445433
return strings.TrimPrefix(filepath.ToSlash(source), "/")

plugin_unix_test.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,41 +45,6 @@ func TestResolveUnixKey(t *testing.T) {
4545
}
4646
}
4747

48-
func TestResolveDir(t *testing.T) {
49-
tests := []struct {
50-
input string
51-
expected string
52-
}{
53-
{
54-
input: "example-string",
55-
expected: "example-string",
56-
},
57-
{
58-
input: "/path/to/file",
59-
expected: "path/to/file",
60-
},
61-
{
62-
input: "12345",
63-
expected: "12345",
64-
},
65-
{
66-
input: "/root/directory",
67-
expected: "root/directory",
68-
},
69-
{
70-
input: "no_slash",
71-
expected: "no_slash",
72-
},
73-
}
74-
75-
for _, tc := range tests {
76-
result := resolveDir(tc.input)
77-
if result != tc.expected {
78-
t.Errorf("Expected: %s, Got: %s", tc.expected, result)
79-
}
80-
}
81-
}
82-
8348
func TestNormalizePath(t *testing.T) {
8449
tests := []struct {
8550
input string

0 commit comments

Comments
 (0)