Skip to content

Commit eb872b4

Browse files
committed
update: Support for colorized outputs
1 parent 488b801 commit eb872b4

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

Diff for: cmd/root.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ var rootCmd = &cobra.Command{
3333
if err != nil {
3434
log.Fatal(err)
3535
}
36-
gcsTree, err := internal.NewGCSTree(ctx, client, bucket)
36+
37+
option := internal.PrintOption{
38+
WithColorized: true,
39+
WithSize: false,
40+
}
41+
gcsTree, err := internal.NewGCSTree(ctx, client, bucket, &option)
3742
if err != nil {
3843
log.Fatal(err)
3944
}

Diff for: go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.21.1
55
require (
66
cloud.google.com/go/storage v1.33.0
77
github.com/ddddddO/gtree v1.9.11
8+
github.com/fatih/color v1.15.0
89
github.com/spf13/cobra v1.7.0
910
google.golang.org/api v0.143.0
1011
)
@@ -14,7 +15,6 @@ require (
1415
cloud.google.com/go/compute v1.23.0 // indirect
1516
cloud.google.com/go/compute/metadata v0.2.3 // indirect
1617
cloud.google.com/go/iam v1.1.1 // indirect
17-
github.com/fatih/color v1.15.0 // indirect
1818
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
1919
github.com/golang/protobuf v1.5.3 // indirect
2020
github.com/google/go-cmp v0.5.9 // indirect

Diff for: internal/gcstree.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,21 @@ type GCSTree struct {
1616
folder string
1717
client *storage.Client
1818
counter *counter
19+
option *PrintOption
1920
}
2021

21-
func NewGCSTree(ctx context.Context, client *storage.Client, bucket string) (*GCSTree, error) {
22+
type PrintOption struct {
23+
WithColorized bool
24+
WithSize bool
25+
}
26+
27+
type ObjectAttrs struct {
28+
Name string
29+
Size int64
30+
isFile bool
31+
}
32+
33+
func NewGCSTree(ctx context.Context, client *storage.Client, bucket string, option *PrintOption) (*GCSTree, error) {
2234

2335
folder := ""
2436
if strings.Contains(bucket, "/") {
@@ -31,13 +43,15 @@ func NewGCSTree(ctx context.Context, client *storage.Client, bucket string) (*GC
3143
folder: folder,
3244
client: client,
3345
counter: newCounter(),
46+
option: option,
3447
}, nil
3548
}
3649

3750
func (g *GCSTree) GetObjectAttrList(ctx context.Context) ([]*storage.ObjectAttrs, error) {
3851
bkt := g.client.Bucket(g.bucket)
3952
query := &storage.Query{Prefix: g.folder}
40-
var names []*storage.ObjectAttrs
53+
var objectAttrs []*storage.ObjectAttrs
54+
4155
it := bkt.Objects(ctx, query)
4256
for {
4357
attrs, err := it.Next()
@@ -48,9 +62,9 @@ func (g *GCSTree) GetObjectAttrList(ctx context.Context) ([]*storage.ObjectAttrs
4862
return nil, err
4963
}
5064
g.counter.count(attrs.Name)
51-
names = append(names, attrs)
65+
objectAttrs = append(objectAttrs, attrs)
5266
}
53-
return names, nil
67+
return objectAttrs, nil
5468
}
5569

5670
func (g *GCSTree) String() (string, error) {
@@ -59,7 +73,7 @@ func (g *GCSTree) String() (string, error) {
5973
if err != nil {
6074
return "", err
6175
}
62-
treeResult, err := tree(g.bucket, objList)
76+
treeResult, err := tree(g.bucket, objList, g.option)
6377
if err != nil {
6478
return "", err
6579
}

Diff for: internal/tree.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
package internal
22

33
import (
4+
"path/filepath"
45
"strings"
56

67
"cloud.google.com/go/storage"
78
"github.com/ddddddO/gtree"
9+
"github.com/fatih/color"
810
)
911

1012
// ref: https://github.com/ddddddO/gtree#the-program-below-converts-the-result-of-find-into-a-tree
11-
func tree(bucket string, objList []*storage.ObjectAttrs) (string, error) {
13+
func tree(bucket string, objList []*storage.ObjectAttrs, option *PrintOption) (string, error) {
14+
if option.WithColorized {
15+
bucket = color.BlueString("%s", bucket)
16+
}
1217
root := gtree.NewRoot(bucket)
1318
node := root
1419
for _, obj := range objList {
20+
_, file := filepath.Split(obj.Name)
1521
for _, s := range strings.Split(obj.Name, "/") {
1622
if s == "" {
1723
continue
1824
}
25+
if option.WithColorized {
26+
if s == file {
27+
s = color.GreenString("%s", s)
28+
} else {
29+
s = color.BlueString("%s", s)
30+
}
31+
}
1932
tmp := node.Add(s)
2033
node = tmp
2134
}

Diff for: internal/tree_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ func TestTree(t *testing.T) {
2121
Size: 6,
2222
})
2323
bucket := "test"
24-
got, err := tree(bucket, objList)
24+
option := PrintOption{
25+
WithColorized: false,
26+
WithSize: false,
27+
}
28+
got, err := tree(bucket, objList, &option)
2529
if err != nil {
2630
t.Fatal(err)
2731
}

0 commit comments

Comments
 (0)