Skip to content

Commit

Permalink
fix: s3 dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Dec 7, 2023
1 parent 5efbd32 commit bbde8d8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ require (
github.com/aws/aws-sdk-go v1.42.17
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down Expand Up @@ -567,6 +569,7 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
Expand Down
12 changes: 10 additions & 2 deletions internal/drivers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,19 @@ func (d *S3Driver) ListDirs(path string) ([]string, error) {
return res, err
}

return s3ItemsToFolders(path, items), err
}

func s3ItemsToFolders(path string, items []string) []string {
if !strings.HasSuffix(path, "/") {
path += "/"
}
res := []string{}
for _, i := range items {
res = append(res, strings.Split(strings.Replace(i, path, "", 1), "/")[0])
res = append(res, strings.Split(strings.TrimPrefix(i, path), "/")[0])
}

return removeDuplicateStr(res), err
return removeDuplicateStr(res)
}

func removeDuplicateStr(strSlice []string) []string {
Expand Down
66 changes: 66 additions & 0 deletions internal/drivers/s3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package drivers

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_s3ItemsToFolders(t *testing.T) {
type args struct {
path string
items []string
}
tests := []struct {
name string
args args
wantRes []string
}{
{
name: "should return empty list",
args: args{
path: "test",
items: []string{},
},
wantRes: []string{},
},
{
name: "should return list with one item",
args: args{
path: "test",
items: []string{"test/1"},
},
wantRes: []string{"1"},
},
{
name: "should return list with two items",
args: args{
path: "test",
items: []string{"test/1", "test/2"},
},
wantRes: []string{"1", "2"},
},
{
name: "trim subdirs",
args: args{
path: "test",
items: []string{"test/1/2/3"},
},
wantRes: []string{"1"},
},
{
name: "remove duplicate subdirs",
args: args{
path: "test",
items: []string{"test/1/2/3", "test/1/2/4"},
},
wantRes: []string{"1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotRes := s3ItemsToFolders(tt.args.path, tt.args.items)
assert.Equal(t, tt.wantRes, gotRes)
})
}
}

0 comments on commit bbde8d8

Please sign in to comment.