Skip to content

Commit fcde8c3

Browse files
committed
rename util to base && refactor PrintTable
1 parent 9dcb175 commit fcde8c3

13 files changed

Lines changed: 56 additions & 45 deletions

File tree

util/cli.go renamed to base/cli.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package base
22

33
import (
44
"bufio"
@@ -136,24 +136,32 @@ func PrintJSON(dataSet interface{}) error {
136136
return nil
137137
}
138138

139-
//PrintTable 以表格方式打印数据集合
140-
func PrintTable(dataSet interface{}, fieldList []string) error {
139+
//PrintTableS 简化版表格打印,无需传表头,根据结构体反射解析
140+
func PrintTableS(dataSet interface{}) {
141141
dataSetVal := reflect.ValueOf(dataSet)
142+
fieldNameList := make([]string, 0)
143+
if dataSetVal.Len() > 0 {
144+
elemType := dataSetVal.Index(0).Type()
145+
for i := 0; i < elemType.NumField(); i++ {
146+
fieldNameList = append(fieldNameList, elemType.Field(i).Name)
147+
}
148+
}
149+
if kind := dataSetVal.Kind(); kind == reflect.Slice || kind == reflect.Array {
150+
displaySlice(dataSetVal, fieldNameList)
151+
} else {
152+
panic(fmt.Sprintf("Internal error, PrintTableS expect array or slice, accept %T", dataSet))
153+
}
154+
}
142155

156+
//PrintTable 以表格方式打印数据集合
157+
func PrintTable(dataSet interface{}, fieldList []string) {
158+
dataSetVal := reflect.ValueOf(dataSet)
143159
switch dataSetVal.Kind() {
144160
case reflect.Slice, reflect.Array:
145161
displaySlice(dataSetVal, fieldList)
146-
case reflect.Map:
147-
displayMap(dataSetVal, fieldList)
148162
default:
149-
return fmt.Errorf("PrintTable expect array,slice or map, accept %T", dataSet)
163+
panic(fmt.Sprintf("PrintTable expect array,slice or map, accept %T", dataSet))
150164
}
151-
return nil
152-
}
153-
154-
func displayMap(mapVal reflect.Value, fieldList []string) {
155-
fmt.Println(mapVal, fieldList)
156-
//todo
157165
}
158166

159167
func displaySlice(listVal reflect.Value, fieldList []string) {
@@ -180,39 +188,42 @@ func displaySlice(listVal reflect.Value, fieldList []string) {
180188
}
181189
rowList = append(rowList, row)
182190
}
191+
printTable(rowList, fieldList, showFieldMap)
192+
}
183193

194+
func printTable(rowList []map[string]interface{}, fieldList []string, fieldWidthMap map[string]int) {
184195
for _, field := range fieldList {
185-
tmpl := "%-" + strconv.Itoa(showFieldMap[field]+GAP) + "s"
196+
tmpl := "%-" + strconv.Itoa(fieldWidthMap[field]+GAP) + "s"
186197
fmt.Printf(tmpl, field)
187198
}
188199
fmt.Printf("\n")
189200

190201
for _, row := range rowList {
191202
for _, field := range fieldList {
192203
cutWidth := calcCutWidth(fmt.Sprintf("%v", row[field]))
193-
tmpl := "%-" + strconv.Itoa(showFieldMap[field]-cutWidth+GAP) + "v"
204+
tmpl := "%-" + strconv.Itoa(fieldWidthMap[field]-cutWidth+GAP) + "v"
194205
fmt.Printf(tmpl, row[field])
195206
}
196207
fmt.Printf("\n")
197208
}
198209
}
199210

200211
func calcCutWidth(text string) int {
201-
set := []*unicode.RangeTable{unicode.Han}
212+
set := []*unicode.RangeTable{unicode.Han, unicode.Punct}
202213
width := 0
203214
for _, r := range text {
204-
if unicode.IsOneOf(set, r) {
215+
if unicode.IsOneOf(set, r) && r > unicode.MaxLatin1 {
205216
width++
206217
}
207218
}
208219
return width
209220
}
210221

211222
func calcWidth(text string) int {
212-
set := []*unicode.RangeTable{unicode.Han}
223+
set := []*unicode.RangeTable{unicode.Han, unicode.Punct}
213224
width := 0
214225
for _, r := range text {
215-
if unicode.IsOneOf(set, r) {
226+
if unicode.IsOneOf(set, r) && r > unicode.MaxLatin1 {
216227
width += 2
217228
} else {
218229
width++
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package util
1+
package base
22

33
import "testing"
44

55
func TestGetHomePath(t *testing.T) {
66
home := GetHomePath()
77
if home == "" {
8-
t.Errorf("util.GetHomePath(), home shoud not be empty. Got :%q", home)
8+
t.Errorf("base.GetHomePath(), home shoud not be empty. Got :%q", home)
99
}
1010
}

util/config.go renamed to base/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package base
22

33
import (
44
"encoding/json"
@@ -130,7 +130,8 @@ func (p *Config) ListConfig(json bool) error {
130130
if json {
131131
return PrintJSON(tmpConfig)
132132
}
133-
return PrintTable([]Config{tmpConfig}, []string{"PublicKey", "PrivateKey", "Region", "Zone", "ProjectID"})
133+
PrintTable([]Config{tmpConfig}, []string{"PublicKey", "PrivateKey", "Region", "Zone", "ProjectID"})
134+
return nil
134135
}
135136

136137
//ClearConfig 清空配置

cmd/completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"strings"
2424

2525
"github.com/spf13/cobra"
26-
. "github.com/ucloud/ucloud-cli/util"
26+
. "github.com/ucloud/ucloud-cli/base"
2727
)
2828

2929
// NewCmdCompletion ucloud completion

cmd/configure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
"github.com/spf13/cobra"
2323

24-
. "github.com/ucloud/ucloud-cli/util"
24+
. "github.com/ucloud/ucloud-cli/base"
2525
)
2626

2727
var config = ConfigInstance

cmd/eip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/ucloud/ucloud-sdk-go/sdk"
2323

2424
"github.com/spf13/cobra"
25-
. "github.com/ucloud/ucloud-cli/util"
25+
. "github.com/ucloud/ucloud-cli/base"
2626
)
2727

2828
//NewCmdEIP ucloud eip

cmd/globalssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
"github.com/spf13/cobra"
2121

22-
. "github.com/ucloud/ucloud-cli/util"
22+
. "github.com/ucloud/ucloud-cli/base"
2323
)
2424

2525
//NewCmdGssh ucloud gssh

cmd/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
"github.com/spf13/cobra"
2121

22-
. "github.com/ucloud/ucloud-cli/util"
22+
. "github.com/ucloud/ucloud-cli/base"
2323
)
2424

2525
//NewCmdUImage ucloud uimage

cmd/project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
"github.com/ucloud/ucloud-sdk-go/services/uaccount"
2121

22-
. "github.com/ucloud/ucloud-cli/util"
22+
. "github.com/ucloud/ucloud-cli/base"
2323
)
2424

2525
//NewCmdProject ucloud project

cmd/region.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
"github.com/ucloud/ucloud-sdk-go/services/uaccount"
2626

27-
. "github.com/ucloud/ucloud-cli/util"
27+
. "github.com/ucloud/ucloud-cli/base"
2828
)
2929

3030
//NewCmdRegion ucloud region
@@ -74,20 +74,19 @@ func listRegion() error {
7474
}
7575
if resp.RetCode != 0 {
7676
return fmt.Errorf("Something wrong. RetCode:%d, Message:%s", resp.RetCode, resp.Message)
77+
}
78+
regionList := make([]RegionTable, 0)
79+
regionMap := make(map[string][]string)
80+
for _, region := range resp.Regions {
81+
regionMap[region.Region] = append(regionMap[region.Region], region.Zone)
82+
}
83+
for region, zones := range regionMap {
84+
regionList = append(regionList, RegionTable{region, strings.Join(zones, ", ")})
85+
}
86+
if global.json {
87+
PrintJSON(regionList)
7788
} else {
78-
regionList := make([]RegionTable, 0)
79-
regionMap := make(map[string][]string)
80-
for _, region := range resp.Regions {
81-
regionMap[region.Region] = append(regionMap[region.Region], region.Zone)
82-
}
83-
for region, zones := range regionMap {
84-
regionList = append(regionList, RegionTable{region, strings.Join(zones, ", ")})
85-
}
86-
if global.json {
87-
PrintJSON(regionList)
88-
} else {
89-
err = PrintTable(regionList, []string{"Region", "Zones"})
90-
}
89+
PrintTable(regionList, []string{"Region", "Zones"})
9190
}
9291
return err
9392
}

0 commit comments

Comments
 (0)