From c4271047d45e592f97b6b2bd14ad386f8830a8f2 Mon Sep 17 00:00:00 2001 From: ddddddO Date: Sun, 1 Oct 2023 02:47:28 +0900 Subject: [PATCH 1/6] =?UTF-8?q?=E3=83=87=E3=82=A3=E3=83=AC=E3=82=AF?= =?UTF-8?q?=E3=83=88=E3=83=AA=E6=95=B0=E3=81=8C=E5=90=88=E3=82=8F=E3=81=AA?= =?UTF-8?q?=E3=81=84=E3=83=91=E3=82=BF=E3=83=BC=E3=83=B3=E3=81=8C=E3=81=82?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E3=81=9F=E3=82=81=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/counter.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/counter.go b/internal/counter.go index 54a2285..d23daed 100644 --- a/internal/counter.go +++ b/internal/counter.go @@ -3,6 +3,7 @@ package internal import ( "fmt" "path/filepath" + "strings" ) type counter struct { @@ -19,8 +20,11 @@ func newCounter() *counter { func (c *counter) count(path string) { dir, file := filepath.Split(path) + splited := strings.Split(dir, "/") if len(dir) > 0 { - c.dirs[dir] = struct{}{} + for i := range splited { + c.dirs[filepath.Join(splited[:i+1]...)] = struct{}{} + } } if len(file) > 0 { c.files[path] = struct{}{} From 40ec868adf6dff9cca105bd7ff7d5d3b05e347e5 Mon Sep 17 00:00:00 2001 From: ddddddO Date: Sun, 1 Oct 2023 03:13:51 +0900 Subject: [PATCH 2/6] go fmt --- internal/gcstree.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/gcstree.go b/internal/gcstree.go index 7c46cb6..6faff41 100644 --- a/internal/gcstree.go +++ b/internal/gcstree.go @@ -3,6 +3,7 @@ package internal import ( "context" "fmt" + "log" "strings" "cloud.google.com/go/storage" @@ -51,6 +52,9 @@ func (g *GCSTree) GetObjectList(ctx context.Context) ([]string, error) { if err != nil { return nil, err } + + log.Println("Debug:") + fmt.Println(attrs.Name) g.counter.count(attrs.Name) names = append(names, attrs.Name) } From 7a7c11943e1ffe3055e3862e119c1c88d7925bf0 Mon Sep 17 00:00:00 2001 From: ddddddO Date: Sun, 1 Oct 2023 03:14:26 +0900 Subject: [PATCH 3/6] =?UTF-8?q?test=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF?= =?UTF-8?q?=E3=82=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/counter_test.go | 55 ++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/internal/counter_test.go b/internal/counter_test.go index 817f7db..70e5753 100644 --- a/internal/counter_test.go +++ b/internal/counter_test.go @@ -5,26 +5,43 @@ import ( ) func TestCounter_Summary(t *testing.T) { - paths := []string{ - "empty_directory/", - "a.txt", - "a.tgz", - "source/a.tgz", - "source/b.tgz", - "source/c.tgz", - "tmp_directory/", - "tmp_directory/a.png", - "tmp_directory/empty_directory/", - "tmp_directory/source/a.tgz", + tests := []struct { + name string + paths []string + want string + }{ + { + name: "pattern1", + paths: []string{ + "empty_directory/", + "a.txt", + "a.tgz", + "source/a.tgz", + "source/b.tgz", + "source/c.tgz", + "tmp_directory/", + "tmp_directory/a.png", + "tmp_directory/empty_directory/", + "tmp_directory/source/a.tgz", + }, + want: "5 directories, 7 files", + }, } - want := "5 directories, 7 files" - c := newCounter() - for _, p := range paths { - c.count(p) - } - got := c.summary() - if got != want { - t.Errorf("\ngot: \n%s\nwant: \n%s", got, want) + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + c := newCounter() + for _, p := range tt.paths { + c.count(p) + } + got := c.summary() + + if got != tt.want { + t.Errorf("\ngot: \n%s\nwant: \n%s", got, tt.want) + } + }) } } From 55db89ed139e1bf5adc84ad2095d8ede4045879a Mon Sep 17 00:00:00 2001 From: ddddddO Date: Sun, 1 Oct 2023 03:17:52 +0900 Subject: [PATCH 4/6] =?UTF-8?q?https://github.com/owlinux1000/gcstree/pull?= =?UTF-8?q?/6=20=E3=81=AE=E3=83=90=E3=82=B0=E5=AF=BE=E5=BF=9C=E3=81=AE?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B1=E3=83=BC=E3=82=B9=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/counter_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/counter_test.go b/internal/counter_test.go index 70e5753..627bde9 100644 --- a/internal/counter_test.go +++ b/internal/counter_test.go @@ -26,6 +26,14 @@ func TestCounter_Summary(t *testing.T) { }, want: "5 directories, 7 files", }, + { + name: "pattern2", + paths: []string{ + "skillset-visualizer/terraform/state/default.tfstate", + "standard/module/structure/default.tfstate", + }, + want: "6 directories, 2 files", + }, } for _, tt := range tests { From a4cae0e14fc77790e349a19aca8382617284b0d8 Mon Sep 17 00:00:00 2001 From: ddddddO Date: Sun, 1 Oct 2023 03:19:21 +0900 Subject: [PATCH 5/6] =?UTF-8?q?debug=E6=99=82=E3=81=AE=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gcstree.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/internal/gcstree.go b/internal/gcstree.go index 6faff41..7c46cb6 100644 --- a/internal/gcstree.go +++ b/internal/gcstree.go @@ -3,7 +3,6 @@ package internal import ( "context" "fmt" - "log" "strings" "cloud.google.com/go/storage" @@ -52,9 +51,6 @@ func (g *GCSTree) GetObjectList(ctx context.Context) ([]string, error) { if err != nil { return nil, err } - - log.Println("Debug:") - fmt.Println(attrs.Name) g.counter.count(attrs.Name) names = append(names, attrs.Name) } From 74d3be0c04cebe1c87740d37078c1a6908e4c4f6 Mon Sep 17 00:00:00 2001 From: ddddddO Date: Sun, 1 Oct 2023 03:39:57 +0900 Subject: [PATCH 6/6] nits --- internal/counter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/counter.go b/internal/counter.go index d23daed..3f6318d 100644 --- a/internal/counter.go +++ b/internal/counter.go @@ -20,8 +20,8 @@ func newCounter() *counter { func (c *counter) count(path string) { dir, file := filepath.Split(path) - splited := strings.Split(dir, "/") if len(dir) > 0 { + splited := strings.Split(dir, "/") for i := range splited { c.dirs[filepath.Join(splited[:i+1]...)] = struct{}{} }