Skip to content

Commit ff1a9db

Browse files
authored
Merge pull request #6 from lixiaojun629/develop
0.1.4
2 parents f6eb1aa + d7c65c3 commit ff1a9db

186 files changed

Lines changed: 1812 additions & 14734 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export VERSION=0.1.3
1+
export VERSION=0.1.4
22

33
.PHONY : build
44
build:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ $ brew update
3333
For windows users, suggest build from the source code which require install Golang first. This also works for Linux and macOS.
3434

3535
```
36-
$ mkdir -p $ GOPATH/src/github.com/ucloud
36+
$ mkdir -p $GOPATH/src/github.com/ucloud
3737
$ cd $GOPATH/src/github.com/ucloud
3838
$ git clone https://github.com/ucloud/ucloud-cli.git
3939
$ cd ucloud-cli
@@ -80,5 +80,5 @@ $ ucloud help
8080
Taking configure globalssh to uhost instance as an example, which will acceleare the instance SSH management efficiency (TCP 22 as default):
8181

8282
```
83-
$ ucloud gssh create --area Washington --target-ip 128.14.225.161
83+
$ ucloud gssh create --location Washington --target-ip 128.14.225.161
8484
```

ansi/code.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Reference https://github.com/sindresorhus/ansi-escapes
2+
package ansi
3+
4+
import (
5+
"fmt"
6+
)
7+
8+
const ESC = "\x1b["
9+
const OSC = "\x1b]"
10+
const BEL = "\x07"
11+
const SEP = ";"
12+
13+
var CursorLeft = fmt.Sprintf("%sG", ESC)
14+
var EraseDown = fmt.Sprintf("%sJ", ESC)
15+
16+
func CursorUp(count int) string {
17+
return fmt.Sprintf("%s%dA", ESC, count)
18+
}

vendor/github.com/ucloud/ucloud-sdk-go/services/client.go renamed to base/client.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,34 @@
1-
package services
1+
package base
22

33
import (
4-
"github.com/ucloud/ucloud-sdk-go/sdk"
5-
"github.com/ucloud/ucloud-sdk-go/sdk/auth"
64
"github.com/ucloud/ucloud-sdk-go/services/pathx"
75
"github.com/ucloud/ucloud-sdk-go/services/uaccount"
86
"github.com/ucloud/ucloud-sdk-go/services/uhost"
97
"github.com/ucloud/ucloud-sdk-go/services/ulb"
108
"github.com/ucloud/ucloud-sdk-go/services/unet"
119
"github.com/ucloud/ucloud-sdk-go/services/vpc"
10+
"github.com/ucloud/ucloud-sdk-go/ucloud"
11+
"github.com/ucloud/ucloud-sdk-go/ucloud/auth"
1212
)
1313

14+
//Client aggregate client for business
1415
type Client struct {
1516
uaccount.UAccountClient
16-
1717
uhost.UHostClient
18-
1918
unet.UNetClient
20-
2119
ulb.ULBClient
22-
2320
vpc.VPCClient
24-
2521
pathx.PathXClient
2622
}
2723

2824
// NewClient will return a aggregate client
29-
func NewClient(config *sdk.Config, credential *auth.Credential) *Client {
25+
func NewClient(config *ucloud.Config, credential *auth.Credential) *Client {
3026
return &Client{
3127
*uaccount.NewClient(config, credential),
32-
3328
*uhost.NewClient(config, credential),
34-
3529
*unet.NewClient(config, credential),
36-
3730
*ulb.NewClient(config, credential),
38-
3931
*vpc.NewClient(config, credential),
40-
4132
*pathx.NewClient(config, credential),
4233
}
4334
}

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

Lines changed: 7 additions & 7 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"
@@ -8,16 +8,15 @@ import (
88
"strings"
99
"time"
1010

11-
"github.com/ucloud/ucloud-sdk-go/sdk"
12-
"github.com/ucloud/ucloud-sdk-go/sdk/auth"
13-
service "github.com/ucloud/ucloud-sdk-go/services"
1411
"github.com/ucloud/ucloud-sdk-go/services/uaccount"
12+
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
13+
"github.com/ucloud/ucloud-sdk-go/ucloud/auth"
1514
)
1615

1716
const configFile = "config.json"
1817

1918
//Version 版本号
20-
const Version = "0.1.3"
19+
const Version = "0.1.4"
2120

2221
//ConfigPath 配置文件路径
2322

@@ -130,7 +129,8 @@ func (p *Config) ListConfig(json bool) error {
130129
if json {
131130
return PrintJSON(tmpConfig)
132131
}
133-
return PrintTable([]Config{tmpConfig}, []string{"PublicKey", "PrivateKey", "Region", "Zone", "ProjectID"})
132+
PrintTable([]Config{tmpConfig}, []string{"PublicKey", "PrivateKey", "Region", "Zone", "ProjectID"})
133+
return nil
134134
}
135135

136136
//ClearConfig 清空配置
@@ -203,5 +203,5 @@ func init() {
203203
SdkClient = sdk.NewClient(ClientConfig, Credential)
204204

205205
//bizClient 用于调用业务接口
206-
BizClient = service.NewClient(ClientConfig, Credential)
206+
BizClient = NewClient(ClientConfig, Credential)
207207
}

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

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

33
import (
44
"bufio"
@@ -13,10 +13,9 @@ import (
1313
"time"
1414
"unicode"
1515

16-
"github.com/ucloud/ucloud-sdk-go/sdk"
17-
uerr "github.com/ucloud/ucloud-sdk-go/sdk/error"
18-
"github.com/ucloud/ucloud-sdk-go/sdk/response"
19-
service "github.com/ucloud/ucloud-sdk-go/services"
16+
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
17+
uerr "github.com/ucloud/ucloud-sdk-go/ucloud/error"
18+
"github.com/ucloud/ucloud-sdk-go/ucloud/response"
2019

2120
"github.com/ucloud/ucloud-cli/model"
2221
)
@@ -34,7 +33,7 @@ var Cxt = model.GetContext(os.Stdout)
3433
var SdkClient *sdk.Client
3534

3635
//BizClient 用于调用业务接口
37-
var BizClient *service.Client
36+
var BizClient *Client
3837

3938
//GetHomePath 获取家目录
4039
func GetHomePath() string {
@@ -136,24 +135,32 @@ func PrintJSON(dataSet interface{}) error {
136135
return nil
137136
}
138137

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

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

159166
func displaySlice(listVal reflect.Value, fieldList []string) {
@@ -180,39 +187,42 @@ func displaySlice(listVal reflect.Value, fieldList []string) {
180187
}
181188
rowList = append(rowList, row)
182189
}
190+
printTable(rowList, fieldList, showFieldMap)
191+
}
183192

193+
func printTable(rowList []map[string]interface{}, fieldList []string, fieldWidthMap map[string]int) {
184194
for _, field := range fieldList {
185-
tmpl := "%-" + strconv.Itoa(showFieldMap[field]+GAP) + "s"
195+
tmpl := "%-" + strconv.Itoa(fieldWidthMap[field]+GAP) + "s"
186196
fmt.Printf(tmpl, field)
187197
}
188198
fmt.Printf("\n")
189199

190200
for _, row := range rowList {
191201
for _, field := range fieldList {
192202
cutWidth := calcCutWidth(fmt.Sprintf("%v", row[field]))
193-
tmpl := "%-" + strconv.Itoa(showFieldMap[field]-cutWidth+GAP) + "v"
203+
tmpl := "%-" + strconv.Itoa(fieldWidthMap[field]-cutWidth+GAP) + "v"
194204
fmt.Printf(tmpl, row[field])
195205
}
196206
fmt.Printf("\n")
197207
}
198208
}
199209

200210
func calcCutWidth(text string) int {
201-
set := []*unicode.RangeTable{unicode.Han}
211+
set := []*unicode.RangeTable{unicode.Han, unicode.Punct}
202212
width := 0
203213
for _, r := range text {
204-
if unicode.IsOneOf(set, r) {
214+
if unicode.IsOneOf(set, r) && r > unicode.MaxLatin1 {
205215
width++
206216
}
207217
}
208218
return width
209219
}
210220

211221
func calcWidth(text string) int {
212-
set := []*unicode.RangeTable{unicode.Han}
222+
set := []*unicode.RangeTable{unicode.Han, unicode.Punct}
213223
width := 0
214224
for _, r := range text {
215-
if unicode.IsOneOf(set, r) {
225+
if unicode.IsOneOf(set, r) && r > unicode.MaxLatin1 {
216226
width += 2
217227
} else {
218228
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
}

cmd/completion.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ 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
3030
func NewCmdCompletion() *cobra.Command {
3131
var completionCmd = &cobra.Command{
3232
Use: "completion",
33-
Short: "Turn on auto completion according to the prompt",
34-
Long: "Turn on auto completion according to the prompt",
33+
Short: "Print the description of how to enable auto completion",
34+
Long: "Print the description of how to enable auto completion",
3535
Run: func(cmd *cobra.Command, args []string) {
3636
shell, ok := os.LookupEnv("SHELL")
3737
if ok {

cmd/configure.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
package cmd
1616

1717
import (
18-
"fmt"
1918
"reflect"
20-
"strings"
19+
20+
"github.com/ucloud/ucloud-cli/ux"
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
@@ -46,16 +46,12 @@ func NewCmdInit() *cobra.Command {
4646
Run: func(cmd *cobra.Command, args []string) {
4747
Cxt.Println(configDesc)
4848
if len(config.PrivateKey) != 0 && len(config.PublicKey) != 0 {
49-
Cxt.Printf("Your have already configured public-key and private-key. Do you want to overwrite it? (y/n):")
50-
var overwrite string
51-
_, err := fmt.Scanf("%s\n", &overwrite)
49+
confirm, err := ux.Prompt("Your have already configured public-key and private-key. Do you want to overwrite it? (y/n):")
5250
if err != nil {
5351
Cxt.Println(err)
5452
return
5553
}
56-
overwrite = strings.Trim(overwrite, " ")
57-
overwrite = strings.ToLower(overwrite)
58-
if overwrite != "yes" && overwrite != "y" {
54+
if confirm {
5955
printHello()
6056
return
6157
}
@@ -110,6 +106,43 @@ func NewCmdConfig() *cobra.Command {
110106
Long: `Configure UCloud CLI options such as private-key,public-key,default region and default project-id.`,
111107
Example: "ucloud config list; ucloud config --region cn-bj2",
112108
Run: func(cmd *cobra.Command, args []string) {
109+
if cfg.Region != "" || cfg.Zone != "" {
110+
regionMap, err := fetchRegion()
111+
if err != nil {
112+
HandleError(err)
113+
return
114+
}
115+
116+
region := cfg.Region
117+
if region == "" {
118+
region = config.Region
119+
}
120+
121+
zones, ok := regionMap[region]
122+
if !ok {
123+
Cxt.Printf("Error, region[%s] not exist! See 'ucloud region'\n", region)
124+
return
125+
}
126+
127+
zone := cfg.Zone
128+
if zone == "" {
129+
zone = config.Zone
130+
}
131+
132+
if zone != "" {
133+
zoneExist := false
134+
for _, zone := range zones {
135+
if zone == cfg.Zone {
136+
zoneExist = true
137+
}
138+
}
139+
if !zoneExist {
140+
Cxt.Printf("Error, zone[%s] not exist in region[%s]! See 'ucloud config list' and 'ucloud region'\n", zone, region)
141+
return
142+
}
143+
}
144+
}
145+
113146
tmpCfgVal := reflect.ValueOf(cfg)
114147
configVal := reflect.ValueOf(config).Elem()
115148
changed := false

cmd/eip.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import (
1919
"strconv"
2020
"time"
2121

22-
"github.com/ucloud/ucloud-sdk-go/sdk"
23-
2422
"github.com/spf13/cobra"
25-
. "github.com/ucloud/ucloud-cli/util"
23+
24+
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
25+
26+
. "github.com/ucloud/ucloud-cli/base"
2627
)
2728

2829
//NewCmdEIP ucloud eip
@@ -133,9 +134,9 @@ func NewCmdEIPAllocate() *cobra.Command {
133134
req.Name = cmd.Flags().String("name", "EIP", "Name of your EIP.")
134135
req.Remark = cmd.Flags().String("remark", "", "Remark of your EIP.")
135136
req.CouponId = cmd.Flags().String("coupon-id", "", "Coupon ID, The Coupon can deducte part of the payment")
136-
cmd.Flags().SetFlagValues("line", []string{"BGP", "International"})
137-
cmd.Flags().SetFlagValues("charge-mode", []string{"Bandwidth", "Traffic", "ShareBandwidth"})
138-
cmd.Flags().SetFlagValues("charge-type", []string{"Month", "Year", "Dynamic", "Trial"})
137+
cmd.Flags().SetFlagValues("line", "BGP", "International")
138+
cmd.Flags().SetFlagValues("charge-mode", "Bandwidth", "Traffic", "ShareBandwidth")
139+
cmd.Flags().SetFlagValues("charge-type", "Month", "Year", "Dynamic", "Trial")
139140
cmd.MarkFlagRequired("line")
140141
cmd.MarkFlagRequired("bandwidth")
141142
return cmd

0 commit comments

Comments
 (0)