Skip to content

Commit

Permalink
feat: support json return format of head commands
Browse files Browse the repository at this point in the history
  • Loading branch information
flywukong committed Nov 22, 2023
1 parent 1e9f056 commit 64fc305
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 6 deletions.
36 changes: 31 additions & 5 deletions cmd/cmd_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ func headObject(ctx *cli.Context) error {
}

fmt.Println("latest object info:")
parseObjectInfo(objectDetail)
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 @@ -126,7 +134,15 @@ func headBucket(ctx *cli.Context) error {
}

fmt.Println("latest bucket info:")
parseBucketInfo(bucketInfo.String())
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 @@ -155,10 +171,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 {
parseGroupByFormat(groupInfo)
} else {
return toCmdErr(fmt.Errorf("invalid format"))
}
}

return nil
}

Expand Down
6 changes: 6 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func main() {
Aliases: []string{"p"},
Usage: "password file for encrypting and decoding the private key",
},
&cli.StringFlag{
Name: formatFlag,
Aliases: []string{"f"},
Value: defaultFormat,
Usage: "return format of plaintxt or json",
},
&cli.StringFlag{
Name: configFlag,
Aliases: []string{"c"},
Expand Down
159 changes: 158 additions & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ const (
unsafeFlag = "unsafe"
unarmoredFlag = "unarmoredHex"
passwordFileFlag = "passwordfile"
formatFlag = "format"
defaultFormat = "plaintxt"
jsonFormat = "json"
homeFlag = "home"
keyStoreFlag = "keystore"
configFlag = "config"
Expand Down Expand Up @@ -127,6 +130,37 @@ var (
TxnOptionWithSyncMode = types.TxOption{Mode: &SyncBroadcastMode}
)

type ObjectInfo struct {
ObjectStatus string `json:"object_status"`
Owner string `json:"owner"`
BucketName string `json:"bucket_name"`
ObjectName string `json:"object_name"`
ID string `json:"id"`
LocalVirtualGroupID int `json:"local_virtual_group_id"`
PayloadSize int `json:"payload_size"`
Visibility string `json:"visibility"`
ContentType string `json:"content_type"`
CreateAt string `json:"create_at"`
Checksums map[string]string `json:"checksums"`
}

type BucketInfo struct {
Owner string `json:"owner"`
BucketName string `json:"bucket_name"`
Visibility string `json:"visibility"`
ID string `json:"id"`
CreateAt string `json:"create_at"`
PaymentAddress string `json:"payment_address"`
GlobalVirtualGroupFamilyID int `json:"global_virtual_group_family_id"`
BucketStatus string `json:"bucket_status"`
}

type GroupInfo struct {
Owner string `json:"owner"`
GroupName string `json:"group_name"`
ID string `json:"id"`
}

type CmdEnumValue struct {
Enum []string
Default string
Expand Down Expand Up @@ -202,8 +236,67 @@ func parseObjectInfo(objectDetail *sdktypes.ObjectDetail) {
}
}

func parseBucketInfo(info string) {
func parseObjectByJsonFormat(objectDetail *sdktypes.ObjectDetail) {
info := objectDetail.ObjectInfo.String()
objectInfo := ObjectInfo{
ObjectStatus: objectDetail.ObjectInfo.ObjectStatus.String(),
Checksums: make(map[string]string),
}
infoStr := strings.Split(info, " ")
checksumID := 0
for _, objInfo := range infoStr {
if strings.Contains(objInfo, "create_at:") {
timeInfo := strings.Split(objInfo, ":")
timestamp, _ := strconv.ParseInt(timeInfo[1], 10, 64)
location, _ := time.LoadLocation("Asia/Shanghai")
t := time.Unix(timestamp, 0).In(location)
objectInfo.CreateAt = t.Format(iso8601DateFormat)
}
if strings.Contains(objInfo, "checksums:") {
hashInfo := strings.Split(objInfo, ":")
objectInfo.Checksums["checksum["+strconv.Itoa(checksumID)+"]"] = hex.EncodeToString([]byte(hashInfo[1]))
checksumID++
}
if strings.Contains(objInfo, "status") {
continue
}
keyValue := strings.Split(objInfo, ":")
if len(keyValue) == 2 {
switch keyValue[0] {
case "owner":
objectInfo.Owner = keyValue[1]
case "bucket_name":
objectInfo.BucketName = keyValue[1]
case "object_name":
objectInfo.ObjectName = keyValue[1]
case "id":
objectInfo.ID = keyValue[1]
case "local_virtual_group_id":
groupID, _ := strconv.Atoi(keyValue[1])
objectInfo.LocalVirtualGroupID = groupID
case "payload_size":
payloadSize, _ := strconv.Atoi(keyValue[1])
objectInfo.PayloadSize = payloadSize
case "visibility":
objectInfo.Visibility = keyValue[1]
case "content_type":
objectInfo.ContentType = keyValue[1]
default:
continue
}
}
}
jsonData, err := json.Marshal(objectInfo)
if err != nil {
fmt.Println("Error marshalling to JSON:", err)
return
}
fmt.Println(string(jsonData))
}

func parseBucketInfo(info *storageTypes.BucketInfo) {
fmt.Println("bucket_status:", info.BucketStatus.String())
infoStr := strings.Split(info.String(), " ")
for _, bucketInfo := range infoStr {
if strings.Contains(bucketInfo, "create_at:") {
timeInfo := strings.Split(bucketInfo, ":")
Expand All @@ -216,6 +309,70 @@ func parseBucketInfo(info string) {
}
}

func parseBucketByJsonFormat(info *storageTypes.BucketInfo) {
infoStr := strings.Split(info.String(), " ")
bucketInfo := BucketInfo{}
bucketInfo.BucketStatus = info.BucketStatus.String()
for _, entry := range infoStr {
keyValue := strings.Split(entry, ":")
if len(keyValue) == 2 {
key := strings.TrimSpace(keyValue[0])
value := strings.TrimSpace(keyValue[1])
switch key {
case "owner":
bucketInfo.Owner = value
case "bucket_name":
bucketInfo.BucketName = value
case "visibility":
bucketInfo.Visibility = value
case "id":
bucketInfo.ID = value
case "create_at":
bucketInfo.CreateAt = value
case "payment_address":
bucketInfo.PaymentAddress = value
case "global_virtual_group_family_id":
id, _ := strconv.Atoi(value)
bucketInfo.GlobalVirtualGroupFamilyID = id
}
}
}

jsonData, err := json.Marshal(bucketInfo)
if err != nil {
fmt.Println("Error marshalling to JSON:", err)
return
}
fmt.Println(string(jsonData))
}

func parseGroupByFormat(info *storageTypes.GroupInfo) {
infoStr := strings.Split(info.String(), " ")
groupInfo := GroupInfo{}
for _, entry := range infoStr {
keyValue := strings.Split(entry, ":")
if len(keyValue) == 2 {
key := strings.TrimSpace(keyValue[0])
value := strings.TrimSpace(keyValue[1])
switch key {
case "owner":
groupInfo.Owner = value
case "group_name":
groupInfo.GroupName = value
case "id":
groupInfo.ID = value
}
}
}

jsonData, err := json.Marshal(groupInfo)
if err != nil {
fmt.Println("Error marshalling to JSON:", err)
return
}
fmt.Println(string(jsonData))
}

func getBucketNameByUrl(ctx *cli.Context) (string, error) {
if ctx.NArg() < 1 {
return "", errors.New("the args should be more than one")
Expand Down

0 comments on commit 64fc305

Please sign in to comment.