Skip to content

Commit

Permalink
feat: support format flag to support json content for the head comman…
Browse files Browse the repository at this point in the history
…ds (#103)

* fix: fix status info of head object

* feat: support json return format of head commands

* fix: update dependency of go-sdk
  • Loading branch information
flywukong authored Nov 23, 2023
1 parent 1b40884 commit a860429
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 62 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.0.2
FEATURES
* [#101](https://github.com/bnb-chain/greenfield-cmd/pull/101) support parsing content type when uploading files
* [#102](https://github.com/bnb-chain/greenfield-cmd/pull/102) support automatically create folder structure in bucket when uploading folder
* [#103](https://github.com/bnb-chain/greenfield-cmd/pull/103) support format flag to support json content for the head bucket/object/group commands

## v1.0.1
BUGFIX
* [#99](https://github.com/bnb-chain/greenfield-cmd/pull/99) fix put bucket policy with object actions and help info
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ To obtain the latest release, please visit the following URL: https://github.com
git clone https://github.com/bnb-chain/greenfield-cmd.git
cd greenfield-cmd
# Find the latest release here: https://github.com/bnb-chain/greenfield-cmd/releases
git checkout -b branch-name v1.0.1
git checkout -b branch-name v1.0.2
make build
cd build
./gnfd-cmd -h
Expand Down
70 changes: 65 additions & 5 deletions cmd/cmd_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ func cmdHeadObj() *cli.Command {
send headObject txn to chain and fetch object info on greenfield chain
Examples:
$ gnfd-cmd object head gnfd://bucket-name/object-name`,
Flags: []cli.Flag{
&cli.GenericFlag{
Name: formatFlag,
Aliases: []string{"f"},
Value: &CmdEnumValue{
Enum: []string{defaultFormat, jsonFormat},
Default: defaultFormat,
},
Usage: "set format of the return content of plaintxt or json",
},
},
}
}

Expand All @@ -33,6 +44,17 @@ func cmdHeadBucket() *cli.Command {
send headBucket txn to chain and fetch bucket info on greenfield chain
Examples:
$ gnfd-cmd bucket head gnfd://bucket-name`,
Flags: []cli.Flag{
&cli.GenericFlag{
Name: formatFlag,
Aliases: []string{"f"},
Value: &CmdEnumValue{
Enum: []string{defaultFormat, jsonFormat},
Default: defaultFormat,
},
Usage: "set format of the return content of plaintxt or json",
},
},
}
}

Expand All @@ -53,6 +75,15 @@ $ gnfd-cmd group head --groupOwner group-name`,
Value: "",
Usage: "need set the owner address if you are not the owner of the group",
},
&cli.GenericFlag{
Name: formatFlag,
Aliases: []string{"f"},
Value: &CmdEnumValue{
Enum: []string{defaultFormat, jsonFormat},
Default: defaultFormat,
},
Usage: "set format of the return content of plaintxt or json",
},
},
}
}
Expand Down Expand Up @@ -99,7 +130,17 @@ func headObject(ctx *cli.Context) error {
fmt.Println("no such object")
return nil
}
parseChainInfo(objectDetail.ObjectInfo.String(), false)

fmt.Println("latest object info:")
if format := ctx.String(formatFlag); format != "" {
if format == defaultFormat {
parseObjectInfo(objectDetail)
} else if format == jsonFormat {
parseObjectByJsonFormat(objectDetail)
} else {
return toCmdErr(fmt.Errorf("invalid format"))
}
}
return nil
}

Expand All @@ -123,7 +164,16 @@ func headBucket(ctx *cli.Context) error {
return nil
}

parseChainInfo(bucketInfo.String(), true)
fmt.Println("latest bucket info:")
if format := ctx.String(formatFlag); format != "" {
if format == defaultFormat {
parseBucketInfo(bucketInfo)
} else if format == jsonFormat {
parseBucketByJsonFormat(bucketInfo)
} else {
return toCmdErr(fmt.Errorf("invalid format"))
}
}
return nil
}

Expand Down Expand Up @@ -152,10 +202,20 @@ func headGroup(ctx *cli.Context) error {
return nil
}

infoStr := strings.Split(groupInfo.String(), " ")
for _, info := range infoStr {
fmt.Println(info)
fmt.Println("latest group info:")
if format := ctx.String(formatFlag); format != "" {
if format == defaultFormat {
infoStr := strings.Split(groupInfo.String(), " ")
for _, info := range infoStr {
fmt.Println(info)
}
} else if format == jsonFormat {
parseGroupByJson(groupInfo)
} else {
return toCmdErr(fmt.Errorf("invalid format"))
}
}

return nil
}

Expand Down
23 changes: 20 additions & 3 deletions cmd/cmd_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,17 +372,34 @@ func uploadFolder(urlInfo string, ctx *cli.Context,
if !fileInfo.IsDir() {
return errors.New("failed to parse folder path with recursive flag")
}

baseDir := filepath.Base(folderName)

fileInfos := make([]os.FileInfo, 0)
filePaths := make([]string, 0)
objectNames := make([]string, 0)
listFolderErr := filepath.Walk(folderName, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
fileInfos = append(fileInfos, info)
// use the base dir to construct object name of the file
index := strings.Index(path, baseDir)
if index == notFound {
return nil
}
objectNames = append(objectNames, path[index:])
filePaths = append(filePaths, path)
} else {
fmt.Println("creating folder:", path)
if createFolderErr := uploadFile(bucketName, path+"/", path, urlInfo, ctx, gnfdClient, true, false, 0); createFolderErr != nil {
// use the base dir to construct object name of the sub-folder
index := strings.Index(path, baseDir)
if index == notFound {
return nil
}
subFolderName := path[index:] + "/"
fmt.Println("creating folder:", subFolderName)
if createFolderErr := uploadFile(bucketName, subFolderName, path, urlInfo, ctx, gnfdClient, true, false, 0); createFolderErr != nil {
return toCmdErr(createFolderErr)
}

}
return nil
})
Expand All @@ -392,7 +409,7 @@ func uploadFolder(urlInfo string, ctx *cli.Context,
}
// upload folder
for id, info := range fileInfos {
if uploadErr := uploadFile(bucketName, filePaths[id], filePaths[id], urlInfo, ctx, gnfdClient, false, false, info.Size()); uploadErr != nil {
if uploadErr := uploadFile(bucketName, objectNames[id], filePaths[id], urlInfo, ctx, gnfdClient, false, false, info.Size()); uploadErr != nil {
fmt.Printf("failed to upload object: %s, error:%v \n", filePaths[id], uploadErr)
}
}
Expand Down
Loading

0 comments on commit a860429

Please sign in to comment.