Skip to content

Commit d6f79f0

Browse files
committed
Variable name optimized
1 parent a18136d commit d6f79f0

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

plugin.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ type Plugin struct {
103103
// Exec runs the plugin
104104
func (p *Plugin) Exec() error {
105105
// normalize the target URL
106-
p.Target = resolveTargetDir(p.Target)
106+
if p.Download {
107+
p.Source = resolveDir(p.Source)
108+
} else {
109+
p.Target = resolveDir(p.Target)
110+
}
107111

108112
// create the client
109113
conf := &aws.Config{
@@ -141,21 +145,23 @@ func (p *Plugin) Exec() error {
141145
}
142146

143147
if p.Download {
144-
targetDir := strings.TrimPrefix(filepath.ToSlash(p.Target), "/")
148+
// sourceDir := strings.TrimPrefix(filepath.ToSlash(p.Source), "/")
149+
sourceDir := normalizePath(p.Source)
150+
145151
log.WithFields(log.Fields{
146152
"bucket": p.Bucket,
147-
"dir": targetDir,
153+
"dir": sourceDir,
148154
}).Info("Listing S3 directory")
149155

150156
list, err := client.ListObjectsV2(&s3.ListObjectsV2Input{
151157
Bucket: &p.Bucket,
152-
Prefix: &targetDir,
158+
Prefix: &sourceDir,
153159
})
154160
if err != nil {
155161
log.WithFields(log.Fields{
156162
"error": err,
157163
"bucket": p.Bucket,
158-
"dir": targetDir,
164+
"dir": sourceDir,
159165
}).Error("Cannot list S3 directory")
160166
return err
161167
}
@@ -183,13 +189,13 @@ func (p *Plugin) Exec() error {
183189
return err
184190
}
185191

186-
source := resolveSource(targetDir, *item.Key, p.StripPrefix)
192+
target := resolveSource(sourceDir, *item.Key, p.StripPrefix)
187193

188-
f, err := os.Create(source)
194+
f, err := os.Create(target)
189195
if err != nil {
190196
log.WithFields(log.Fields{
191197
"error": err,
192-
"file": source,
198+
"file": target,
193199
}).Error("Problem opening file for writing")
194200
return err
195201
}
@@ -199,7 +205,7 @@ func (p *Plugin) Exec() error {
199205
if err != nil {
200206
log.WithFields(log.Fields{
201207
"error": err,
202-
"file": source,
208+
"file": target,
203209
}).Error("Failed to write file")
204210
return err
205211
}
@@ -424,7 +430,12 @@ func isDir(source string, matches []string) bool {
424430
return false
425431
}
426432

427-
func resolveTargetDir(s string) string {
433+
func resolveDir(s string) string {
428434
res := strings.TrimPrefix(filepath.ToSlash(s), "/")
429435
return res
430436
}
437+
438+
// normalizePath converts the path to a forward slash format and trims the prefix.
439+
func normalizePath(source string) string {
440+
return strings.TrimPrefix(filepath.ToSlash(source), "/")
441+
}

plugin_unix_test.go

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

48-
func TestResolveTargetDir(t *testing.T) {
48+
func TestResolveDir(t *testing.T) {
4949
tests := []struct {
5050
input string
5151
expected string
@@ -73,7 +73,7 @@ func TestResolveTargetDir(t *testing.T) {
7373
}
7474

7575
for _, tc := range tests {
76-
result := resolveTargetDir(tc.input)
76+
result := resolveDir(tc.input)
7777
if result != tc.expected {
7878
t.Errorf("Expected: %s, Got: %s", tc.expected, result)
7979
}

0 commit comments

Comments
 (0)