Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separation filtering and formatting #189

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@

### Master

- Add flag `--filter` for filtering result, the value of the flag is [jmespath](http://jmespath.org/) format.
- Add flag `--format` to control the output format, the value can be json or table, the table is not csv format, just like below:

```shell
+------------------------------------------------------+
| DescribeRegions |
+------------------------------------------------------+
| RequestId | A71F2A35-BA66-496F-8B3F-BC19F4268937 |
|+----------------------------------------------------+|
|| Regions ||
|+----------------------------------------------------+|
||+--------------------------------------------------+||
||| Region |||
||+--------------------------------------------------+||
||| LocalName | 华北 1 |||
||| RegionEndpoint | ecs.aliyuncs.com |||
||| RegionId | cn-qingdao |||
||+--------------------------------------------------+||
||+--------------------------------------------------+||
||| Region |||
||+--------------------------------------------------+||
||| LocalName | 华北 2 |||
||| RegionEndpoint | ecs.aliyuncs.com |||
||| RegionId | cn-beijing |||
||+--------------------------------------------------+||
```

- fix: help generating information bug
- Upgrade the ossutil component to version 1.6.6
- Package manager switches to Go Modules
Expand Down
144 changes: 144 additions & 0 deletions humanFormat/fromJSONto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright 1999-2019 Alibaba Group Holding Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package format

import (
"encoding/json"
"fmt"
"reflect"
"sort"
)

type tableEncoder struct {
table *Table
v interface{}
hasRow bool
}

var funcList []func()
var backupFuncList []func()
var count int

// FromJSONFull Convert from json to table format
func FromJSON(data []byte, table *Table) {
e := new(tableEncoder)
e.table = table
var v interface{}
err := json.Unmarshal(data, &v)
if err != nil {
panic(err)
}
e.v = v
funcList = append(funcList, e.marshal)
for {
if len(funcList) == 0 && len(backupFuncList) == 0 {
break
}
switch count % 2 {
case 0:
for _, v := range funcList {
v()
}
funcList = []func(){}
case 1:
for _, v := range backupFuncList {
v()
}
backupFuncList = []func(){}
}
count++
}
}

func (e *tableEncoder) marshal() {
e.regularJSONObject()
deleteEmptyTable(e.table)
}

// deleteEmptyTable Delete empty table from bottom to top
func deleteEmptyTable(table *Table) {
if table.IsEmptyCell() && table.IsEmptySub() {
table.ParentTable().Remove(table)
}
if table.ParentTable() != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前表未删除的时候,不需要判断他的父表是否需要删除

deleteEmptyTable(table.ParentTable())
}
}

func (e *tableEncoder) regularJSONObject() {
switch reflect.ValueOf(e.v).Kind() {
case reflect.Map:
vMap := e.v.(map[string]interface{})
var keySlice []string
for k := range vMap {
keySlice = append(keySlice, k)
}
sort.Strings(keySlice)
for _, k := range keySlice {
v := vMap[k]
reflectValue := reflect.ValueOf(v)
if reflectValue.String() == "" {
e.table.AddRow(fmt.Sprintf("%s\t", k))
}
kind := reflectValue.Kind()
if kind != reflect.Map && kind != reflect.Slice {
e.table.AddRow(fmt.Sprintf("%s\t%v", k, v))
e.hasRow = true
continue
}
subTable := e.table.AddNewTable(e.table.w).AddTitle(k)
subE := new(tableEncoder)
subE.table = subTable
subE.v = v
switch count % 2 {
case 0:
backupFuncList = append(backupFuncList, subE.marshal)
case 1:
funcList = append(funcList, subE.marshal)
}
}
case reflect.Slice:
if len(e.v.([]interface{})) == 0 {
return
}
var title string
if e.table.title != nil {
title = string(*e.table.title)
} else {
title = string(*e.table.ParentTable().title)
}
e.table.ParentTable().Remove(e.table)

e.table = e.table.ParentTable()

for _, v := range e.v.([]interface{}) {
kind := reflect.TypeOf(v).Kind()
if kind != reflect.Map && kind != reflect.Slice {
e.table.AddRow(fmt.Sprintf("%s\t%v", string(title), v))
continue
}
subTable := e.table.AddNewTable(e.table.w).AddTitle(string(title))
subE := new(tableEncoder)
subE.table = subTable
subE.v = v
switch count % 2 {
case 0:
backupFuncList = append(backupFuncList, subE.marshal)
case 1:
funcList = append(funcList, subE.marshal)
}
}
}
}
Loading