Skip to content

Commit

Permalink
Merge pull request #9 from owlinux1000/colorized_output
Browse files Browse the repository at this point in the history
Colorized output (Close #2)
  • Loading branch information
owlinux1000 committed Oct 1, 2023
2 parents 488b801 + 05e334f commit bcac69f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Usage:

Flags:
-h, --help help for gcstree
-n, --no-color disable colorized outputs
-v, --version show the gcstree version
```

Expand Down
8 changes: 7 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ var rootCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
gcsTree, err := internal.NewGCSTree(ctx, client, bucket)

withColorized, _ := cmd.Flags().GetBool("no-color")
option := internal.PrintOption{
WithColorized: !withColorized,
}
gcsTree, err := internal.NewGCSTree(ctx, client, bucket, &option)
if err != nil {
log.Fatal(err)
}
Expand All @@ -54,4 +59,5 @@ func Execute() {

func init() {
rootCmd.Flags().BoolP("version", "v", false, "show the gcstree version")
rootCmd.Flags().BoolP("no-color", "n", false, "disable colorized outputs")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21.1
require (
cloud.google.com/go/storage v1.33.0
github.com/ddddddO/gtree v1.9.11
github.com/fatih/color v1.15.0
github.com/spf13/cobra v1.7.0
google.golang.org/api v0.143.0
)
Expand All @@ -14,7 +15,6 @@ require (
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
Expand Down
18 changes: 13 additions & 5 deletions internal/gcstree.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ type GCSTree struct {
folder string
client *storage.Client
counter *counter
option *PrintOption
}

func NewGCSTree(ctx context.Context, client *storage.Client, bucket string) (*GCSTree, error) {
type PrintOption struct {
WithColorized bool
WithSize bool
}

func NewGCSTree(ctx context.Context, client *storage.Client, bucket string, option *PrintOption) (*GCSTree, error) {

folder := ""
if strings.Contains(bucket, "/") {
Expand All @@ -31,13 +37,15 @@ func NewGCSTree(ctx context.Context, client *storage.Client, bucket string) (*GC
folder: folder,
client: client,
counter: newCounter(),
option: option,
}, nil
}

func (g *GCSTree) GetObjectAttrList(ctx context.Context) ([]*storage.ObjectAttrs, error) {
bkt := g.client.Bucket(g.bucket)
query := &storage.Query{Prefix: g.folder}
var names []*storage.ObjectAttrs
var objectAttrs []*storage.ObjectAttrs

it := bkt.Objects(ctx, query)
for {
attrs, err := it.Next()
Expand All @@ -48,9 +56,9 @@ func (g *GCSTree) GetObjectAttrList(ctx context.Context) ([]*storage.ObjectAttrs
return nil, err
}
g.counter.count(attrs.Name)
names = append(names, attrs)
objectAttrs = append(objectAttrs, attrs)
}
return names, nil
return objectAttrs, nil
}

func (g *GCSTree) String() (string, error) {
Expand All @@ -59,7 +67,7 @@ func (g *GCSTree) String() (string, error) {
if err != nil {
return "", err
}
treeResult, err := tree(g.bucket, objList)
treeResult, err := tree(g.bucket, objList, g.option)
if err != nil {
return "", err
}
Expand Down
13 changes: 12 additions & 1 deletion internal/tree.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package internal

import (
"path/filepath"
"strings"

"cloud.google.com/go/storage"
"github.com/ddddddO/gtree"
"github.com/fatih/color"
)

// ref: https://github.com/ddddddO/gtree#the-program-below-converts-the-result-of-find-into-a-tree
func tree(bucket string, objList []*storage.ObjectAttrs) (string, error) {
func tree(bucket string, objList []*storage.ObjectAttrs, option *PrintOption) (string, error) {
if option.WithColorized {
bucket = color.CyanString("%s", bucket)
}
root := gtree.NewRoot(bucket)
node := root
for _, obj := range objList {
_, file := filepath.Split(obj.Name)
for _, s := range strings.Split(obj.Name, "/") {
if s == "" {
continue
}
if option.WithColorized {
if s != file {
s = color.BlueString("%s", s)
}
}
tmp := node.Add(s)
node = tmp
}
Expand Down
6 changes: 5 additions & 1 deletion internal/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func TestTree(t *testing.T) {
Size: 6,
})
bucket := "test"
got, err := tree(bucket, objList)
option := PrintOption{
WithColorized: false,
WithSize: false,
}
got, err := tree(bucket, objList, &option)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit bcac69f

Please sign in to comment.