Skip to content

Commit 7eefd11

Browse files
committed
Changes and tests
1 parent fbde487 commit 7eefd11

File tree

4 files changed

+93
-36
lines changed

4 files changed

+93
-36
lines changed

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func main() {
8181
},
8282
cli.StringFlag{
8383
Name: "strip-prefix",
84-
Usage: "strip the prefix from the target",
84+
Usage: "used to add or remove a prefix from the source/target path",
8585
EnvVar: "PLUGIN_STRIP_PREFIX",
8686
},
8787
cli.StringSliceFlag{
@@ -96,7 +96,7 @@ func main() {
9696
},
9797
cli.BoolFlag{
9898
Name: "download",
99-
Usage: "switch to download mode, which will fetch `target`'s files from s3 bucket and place them according to `strip-prefix`",
99+
Usage: "switch to download mode, which will fetch `source`'s files from s3 bucket",
100100
EnvVar: "PLUGIN_DOWNLOAD",
101101
},
102102
cli.BoolFlag{

plugin.go

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -110,44 +110,11 @@ func (p *Plugin) Exec() error {
110110
}
111111

112112
// create the client
113-
conf := &aws.Config{
114-
Region: aws.String(p.Region),
115-
Endpoint: &p.Endpoint,
116-
DisableSSL: aws.Bool(strings.HasPrefix(p.Endpoint, "http://")),
117-
S3ForcePathStyle: aws.Bool(p.PathStyle),
118-
}
119-
120-
if p.Key != "" && p.Secret != "" {
121-
conf.Credentials = credentials.NewStaticCredentials(p.Key, p.Secret, "")
122-
} else if p.AssumeRole != "" {
123-
conf.Credentials = assumeRole(p.AssumeRole, p.AssumeRoleSessionName, p.ExternalID)
124-
} else {
125-
log.Warn("AWS Key and/or Secret not provided (falling back to ec2 instance profile)")
126-
}
127-
128-
var client *s3.S3
129-
sess, err := session.NewSession(conf)
130-
if err != nil {
131-
log.WithError(err).Errorln("could not instantiate session")
132-
return err
133-
}
134-
135-
// If user role ARN is set then assume role here
136-
if len(p.UserRoleArn) > 0 {
137-
confRoleArn := aws.Config{
138-
Region: aws.String(p.Region),
139-
Credentials: stscreds.NewCredentials(sess, p.UserRoleArn),
140-
}
141-
142-
client = s3.New(sess, &confRoleArn)
143-
} else {
144-
client = s3.New(sess)
145-
}
113+
client := p.createS3Client()
146114

147115
// If in download mode, call the downloadS3Objects method
148116
if p.Download {
149117
sourceDir := normalizePath(p.Source)
150-
client := p.createS3Client()
151118

152119
return p.downloadS3Objects(client, sourceDir)
153120
}

plugin_unix_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,48 @@ func TestNormalizePath(t *testing.T) {
8383
}
8484
}
8585
}
86+
87+
func TestResolveSource(t *testing.T) {
88+
tests := []struct {
89+
sourceDir string
90+
source string
91+
stripPrefix string
92+
expected string
93+
}{
94+
// Test case 1
95+
{
96+
sourceDir: "/home/user/documents",
97+
source: "/home/user/documents/file.txt",
98+
stripPrefix: "output-",
99+
expected: "output-file.txt",
100+
},
101+
// Test case 2
102+
{
103+
sourceDir: "assets",
104+
source: "assets/images/logo.png",
105+
stripPrefix: "",
106+
expected: "images/logo.png",
107+
},
108+
// Test case 3
109+
{
110+
sourceDir: "/var/www/html",
111+
source: "/var/www/html/pages/index.html",
112+
stripPrefix: "web",
113+
expected: "webpages/index.html",
114+
},
115+
// Test case 4
116+
{
117+
sourceDir: "dist",
118+
source: "dist/js/app.js",
119+
stripPrefix: "public",
120+
expected: "publicjs/app.js",
121+
},
122+
}
123+
124+
for _, tc := range tests {
125+
result := resolveSource(tc.sourceDir, tc.source, tc.stripPrefix)
126+
if result != tc.expected {
127+
t.Errorf("Expected: %s, Got: %s", tc.expected, result)
128+
}
129+
}
130+
}

plugin_windows_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,48 @@ func TestNormalizePath(t *testing.T) {
9797
}
9898
}
9999
}
100+
101+
func TestResolveSource(t *testing.T) {
102+
tests := []struct {
103+
sourceDir string
104+
source string
105+
stripPrefix string
106+
expected string
107+
}{
108+
// Test case 1
109+
{
110+
sourceDir: "/home/user/documents",
111+
source: "/home/user/documents/file.txt",
112+
stripPrefix: "output-",
113+
expected: "output-file.txt",
114+
},
115+
// Test case 2
116+
{
117+
sourceDir: "assets",
118+
source: "assets/images/logo.png",
119+
stripPrefix: "",
120+
expected: "images/logo.png",
121+
},
122+
// Test case 3
123+
{
124+
sourceDir: "/var/www/html",
125+
source: "/var/www/html/pages/index.html",
126+
stripPrefix: "web",
127+
expected: "webpages/index.html",
128+
},
129+
// Test case 4
130+
{
131+
sourceDir: "dist",
132+
source: "dist/js/app.js",
133+
stripPrefix: "public",
134+
expected: "publicjs/app.js",
135+
},
136+
}
137+
138+
for _, tc := range tests {
139+
result := resolveSource(tc.sourceDir, tc.source, tc.stripPrefix)
140+
if result != tc.expected {
141+
t.Errorf("Expected: %s, Got: %s", tc.expected, result)
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)