Skip to content

Commit b2b85cf

Browse files
committed
update ucloud-sdk-go to 0.4.0
1 parent 12895ae commit b2b85cf

213 files changed

Lines changed: 6776 additions & 2369 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.

cmd/completion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ func NewCmdCompletion() *cobra.Command {
5959
shell, ok := os.LookupEnv("SHELL")
6060
if ok {
6161
if strings.HasSuffix(shell, "bash") {
62-
bashCompletion(cmd.Parent())
62+
bashCompletion(cmd)
6363
} else if strings.HasSuffix(shell, "zsh") {
64-
zshCompletion(cmd.Parent())
64+
zshCompletion(cmd)
6565
} else {
6666
fmt.Println("Unknow shell: %", shell)
6767
}

cmd/configure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func NewCmdConfig() *cobra.Command {
116116
//NewCmdConfigList ucloud config ls
117117
func NewCmdConfigList() *cobra.Command {
118118
var configListCmd = &cobra.Command{
119-
Use: "ls",
119+
Use: "list",
120120
Short: "list all settings",
121121
Long: `list all settings`,
122122
Run: func(cmd *cobra.Command, args []string) {

cmd/eip.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func NewCmdEIPList() *cobra.Command {
4646
Long: `List all EIP instances`,
4747
Example: "ucloud eip ls",
4848
Run: func(cmd *cobra.Command, args []string) {
49-
bindGlobalParam(req)
5049
resp, err := BizClient.DescribeEIP(req)
5150
if err != nil {
5251
fmt.Println("Error:", err)
@@ -65,6 +64,8 @@ func NewCmdEIPList() *cobra.Command {
6564
}
6665
},
6766
}
67+
cmd.Flags().StringVar(&req.Region, "region", ConfigInstance.Region, "Assign region(override default region of your config)")
68+
cmd.Flags().StringVar(&req.ProjectId, "project-id", ConfigInstance.ProjectID, "Assign project-id(override default projec-id of your config)")
6869
return cmd
6970
}
7071

@@ -77,7 +78,6 @@ func NewCmdEIPAllocate() *cobra.Command {
7778
Long: "Allocate EIP",
7879
Example: "ucloud eip allocate --line Bgp --bandwidth 2",
7980
Run: func(cmd *cobra.Command, args []string) {
80-
bindGlobalParam(eipAllocateReq)
8181
resp, err := BizClient.AllocateEIP(eipAllocateReq)
8282
if err != nil {
8383
fmt.Println(err)
@@ -96,6 +96,8 @@ func NewCmdEIPAllocate() *cobra.Command {
9696
},
9797
}
9898
cmd.Flags().SortFlags = false
99+
cmd.Flags().StringVar(&eipAllocateReq.Region, "region", ConfigInstance.Region, "Assign region(override default region of your config)")
100+
cmd.Flags().StringVar(&eipAllocateReq.ProjectId, "project-id", ConfigInstance.ProjectID, "Assign project-id(override default projec-id of your config)")
99101
cmd.Flags().StringVar(&eipAllocateReq.OperatorName, "line", "", "Line 'Bgp' or 'International'. 'Bgp' can be set in region cn-sh1,cn-sh2,cn-gd,cn-bj1 and cn-bj2. 'International' can be set in region hk,us-ca,th-bkk,kr-seoul,us-ws,ge-fra,sg,tw-kh and other oversea regions. Required")
100102
cmd.Flags().IntVar(&eipAllocateReq.Bandwidth, "bandwidth", 0, "Bandwidth(Unit:Mbps). When paying by traffic, it ranges from 1 to 200; when paying by bandwidth, it ranges from 1 to 800, and when shared bandwidth is used, its value is 0. Required")
101103
cmd.Flags().StringVar(&eipAllocateReq.PayMode, "pay-mode", "Bandwidth", "pay-mode is an enumeration value. 'Traffic','Bandwidth' or 'ShareBandwidth'")
@@ -119,7 +121,6 @@ func NewCmdEIPBind() *cobra.Command {
119121
Long: "Bind EIP with uhost",
120122
Example: "ucloud eip bind --eip-id eip-xxx --resource-id uhost-xxx",
121123
Run: func(cmd *cobra.Command, args []string) {
122-
bindGlobalParam(eipBindReq)
123124
eipBindReq.ResourceType = "uhost"
124125
resp, err := BizClient.BindEIP(eipBindReq)
125126
if err != nil {
@@ -134,6 +135,8 @@ func NewCmdEIPBind() *cobra.Command {
134135
},
135136
}
136137
eipBindCmd.Flags().SortFlags = false
138+
eipBindCmd.Flags().StringVar(&eipBindReq.Region, "region", ConfigInstance.Region, "Assign region(override default region of your config)")
139+
eipBindCmd.Flags().StringVar(&eipBindReq.ProjectId, "project-id", ConfigInstance.ProjectID, "Assign project-id(override default projec-id of your config)")
137140
eipBindCmd.Flags().StringVar(&eipBindReq.EIPId, "eip-id", "", "EIPId to bind. Required")
138141
eipBindCmd.Flags().StringVar(&eipBindReq.ResourceId, "resource-id", "", "ResourceID , which is the UHostId of uhost. Required")
139142
eipBindCmd.MarkFlagRequired("eip-id")
@@ -151,7 +154,6 @@ func NewCmdEIPUnbind() *cobra.Command {
151154
Long: "Unbind EIP with uhost",
152155
Example: "ucloud eip unbind --eip-id eip-xxx --resource-id uhost-xxx",
153156
Run: func(cmd *cobra.Command, args []string) {
154-
bindGlobalParam(eipUnBindReq)
155157
eipUnBindReq.ResourceType = "uhost"
156158
resp, err := BizClient.UnBindEIP(eipUnBindReq)
157159
if err != nil {
@@ -166,6 +168,8 @@ func NewCmdEIPUnbind() *cobra.Command {
166168
},
167169
}
168170
eipUnBindCmd.Flags().SortFlags = false
171+
eipUnBindCmd.Flags().StringVar(&eipUnBindReq.Region, "region", ConfigInstance.Region, "Assign region(override default region of your config)")
172+
eipUnBindCmd.Flags().StringVar(&eipUnBindReq.ProjectId, "project-id", ConfigInstance.ProjectID, "Assign project-id(override default projec-id of your config)")
169173
eipUnBindCmd.Flags().StringVar(&eipUnBindReq.EIPId, "eip-id", "", "EIPId to unbind. Required")
170174
eipUnBindCmd.Flags().StringVar(&eipUnBindReq.ResourceId, "resource-id", "", "ResourceID , which is the UHostId of uhost. Required")
171175
eipUnBindCmd.MarkFlagRequired("eip-id")
@@ -177,15 +181,14 @@ func NewCmdEIPUnbind() *cobra.Command {
177181
//NewCmdEIPRelease ucloud eip release
178182
func NewCmdEIPRelease() *cobra.Command {
179183
var ids []string
184+
var eipReleaseReq = BizClient.NewReleaseEIPRequest()
180185
var eipReleaseCmd = &cobra.Command{
181186
Use: "release",
182187
Short: "Release EIP",
183188
Long: "Release EIP",
184189
Example: "ucloud eip release --eip-id eip-xx1 --eip-id eip-xx2",
185190
Run: func(cmd *cobra.Command, args []string) {
186191
for _, id := range ids {
187-
var eipReleaseReq = BizClient.NewReleaseEIPRequest()
188-
bindGlobalParam(eipReleaseReq)
189192
eipReleaseReq.EIPId = id
190193
resp, err := BizClient.ReleaseEIP(eipReleaseReq)
191194
if err != nil {
@@ -194,12 +197,14 @@ func NewCmdEIPRelease() *cobra.Command {
194197
if resp.RetCode == 0 {
195198
fmt.Printf("EIP: %v released \n", eipReleaseReq.EIPId)
196199
} else {
197-
fmt.Printf("Something wrong. RetCode:%d, Message: %s \n", resp.RetCode, resp.Message)
200+
HandleBizError(resp)
198201
}
199202
}
200203
}
201204
},
202205
}
206+
eipReleaseCmd.Flags().StringVar(&eipReleaseReq.Region, "region", ConfigInstance.Region, "Assign region(override default region of your config)")
207+
eipReleaseCmd.Flags().StringVar(&eipReleaseReq.ProjectId, "project-id", ConfigInstance.ProjectID, "Assign project-id(override default projec-id of your config)")
203208
eipReleaseCmd.Flags().StringArrayVarP(&ids, "eip-id", "", make([]string, 0), "EIPId of the EIP you want to release. Required")
204209
eipReleaseCmd.MarkFlagRequired("eip-id")
205210
eipReleaseCmd.MarkFlagRequired("bandwidth")

cmd/globalssh.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ func NewCmdGssh() *cobra.Command {
4040
func NewCmdGsshList() *cobra.Command {
4141
req := BizClient.NewDescribeGlobalSSHInstanceRequest()
4242
var cmd = &cobra.Command{
43-
Use: "ls",
43+
Use: "list",
4444
Short: "List all GlobalSSH instances",
4545
Long: `List all GlobalSSH instances`,
4646
Example: "ucloud gssh ls",
4747
Run: func(cmd *cobra.Command, args []string) {
48-
bindGlobalParam(req)
4948
resp, err := BizClient.DescribeGlobalSSHInstance(req)
5049
if err != nil {
5150
fmt.Println("Error", err)
@@ -60,6 +59,8 @@ func NewCmdGsshList() *cobra.Command {
6059
}
6160
},
6261
}
62+
cmd.Flags().StringVar(&req.Region, "region", ConfigInstance.Region, "Assign region(override default region of your config)")
63+
cmd.Flags().StringVar(&req.ProjectId, "project-id", ConfigInstance.ProjectID, "Assign project-id(override default projec-id of your config)")
6364
return cmd
6465
}
6566

@@ -72,7 +73,6 @@ func NewCmdGsshCreate() *cobra.Command {
7273
Long: "Create GlobalSSH instance",
7374
Example: "ucloud gssh create --area Washington --target-ip 8.8.8.8",
7475
Run: func(cmd *cobra.Command, args []string) {
75-
bindGlobalParam(gsshCreateReq)
7676
var areaMap = map[string]string{
7777
"LosAngeles": "洛杉矶",
7878
"Singapore": "新加坡",
@@ -111,6 +111,8 @@ func NewCmdGsshCreate() *cobra.Command {
111111
},
112112
}
113113
cmd.Flags().SortFlags = false
114+
cmd.Flags().StringVar(&gsshCreateReq.Region, "region", ConfigInstance.Region, "Assign region(override default region of your config)")
115+
cmd.Flags().StringVar(&gsshCreateReq.ProjectId, "project-id", ConfigInstance.ProjectID, "Assign project-id(override default projec-id of your config)")
114116
cmd.Flags().StringVar(&gsshCreateReq.Area, "area", "", "Location of the source server.Only supports six cities,LosAngeles,Singapore,HongKong,Tokyo,Washington,Frankfurt. Required")
115117
cmd.Flags().StringVar(&gsshCreateReq.TargetIP, "target-ip", "", "IP of the source server. Required")
116118
cmd.Flags().StringVar(&gsshCreateReq.Port, "port", "22", "Port of The SSH service between 1 and 65535. Do not use ports such as 80,443.")
@@ -131,13 +133,8 @@ func NewCmdGsshDelete() *cobra.Command {
131133
Long: "Delete GlobalSSH instance",
132134
Example: "ucloud gssh delete --id uga-xx1 --id uga-xx2",
133135
Run: func(cmd *cobra.Command, args []string) {
134-
bindGlobalParam(gsshDeleteReq)
135136
for _, id := range gsshIds {
136137
gsshDeleteReq.InstanceId = id
137-
138-
// if global.projectID != "" {
139-
// gsshDeleteReq.ProjectId = global.projectID
140-
// }
141138
resp, err := BizClient.DeleteGlobalSSHInstance(gsshDeleteReq)
142139
if err != nil {
143140
fmt.Println("Error:", err)
@@ -151,6 +148,8 @@ func NewCmdGsshDelete() *cobra.Command {
151148
}
152149
},
153150
}
151+
cmd.Flags().StringVar(&gsshDeleteReq.Region, "region", ConfigInstance.Region, "Assign region(override default region of your config)")
152+
cmd.Flags().StringVar(&gsshDeleteReq.ProjectId, "project-id", ConfigInstance.ProjectID, "Assign project-id(override default projec-id of your config)")
154153
cmd.Flags().StringArrayVar(&gsshIds, "id", make([]string, 0), "ID of the GlobalSSH instances you want to delete. Multiple values specified by multiple flags. Required")
155154
cmd.MarkFlagRequired("id")
156155
return cmd
@@ -160,14 +159,18 @@ func NewCmdGsshDelete() *cobra.Command {
160159
func NewCmdGsshModify() *cobra.Command {
161160
var gsshModifyPortReq = BizClient.NewModifyGlobalSSHPortRequest()
162161
var gsshModifyRemarkReq = BizClient.NewModifyGlobalSSHRemarkRequest()
162+
var region, project string
163163
var cmd = &cobra.Command{
164-
Use: "modify",
165-
Short: "Modify GlobalSSH instance",
166-
Long: "Modify GlobalSSH instance, including port and remark attribute",
164+
Use: "update",
165+
Short: "Update GlobalSSH instance",
166+
Long: "Update GlobalSSH instance, including port and remark attribute",
167167
Example: "ucloud gssh modify --id uga-xxx --port 22",
168168
Run: func(cmd *cobra.Command, args []string) {
169-
bindGlobalParam(gsshModifyPortReq)
170-
bindGlobalParam(gsshModifyRemarkReq)
169+
gsshModifyPortReq.Region = region
170+
gsshModifyPortReq.ProjectId = project
171+
gsshModifyRemarkReq.Region = region
172+
gsshModifyRemarkReq.ProjectId = project
173+
171174
if gsshModifyPortReq.Port == "" && gsshModifyRemarkReq.Remark == "" {
172175
fmt.Println("port or remark required")
173176
}
@@ -207,6 +210,8 @@ func NewCmdGsshModify() *cobra.Command {
207210
}
208211
},
209212
}
213+
cmd.Flags().StringVar(&region, "region", ConfigInstance.Region, "Assign region(override default region of your config)")
214+
cmd.Flags().StringVar(&project, "project-id", ConfigInstance.ProjectID, "Assign project-id(override default projec-id of your config)")
210215
cmd.Flags().StringVar(&gsshModifyPortReq.Port, "port", "", "Port of SSH service.")
211216
cmd.Flags().StringVar(&gsshModifyRemarkReq.Remark, "remark", "", "Remark of your GlobalSSH.")
212217
cmd.Flags().StringVar(&gsshModifyRemarkReq.InstanceId, "id", "", "InstanceID of your GlobalSSH. Required")

cmd/list.go renamed to cmd/region.go

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"encoding/json"
1919
"fmt"
2020
"io/ioutil"
21+
"strings"
2122

2223
"github.com/spf13/cobra"
2324

@@ -27,31 +28,19 @@ import (
2728
. "github.com/ucloud/ucloud-cli/util"
2829
)
2930

30-
//NewCmdList ucloud ls
31-
func NewCmdList() *cobra.Command {
32-
var listObject string
31+
//NewCmdRegion ucloud ls
32+
func NewCmdRegion() *cobra.Command {
3333
var cmd = &cobra.Command{
34-
Use: "ls",
35-
Short: "List all regions or projects",
36-
Long: "List all regions or projects",
37-
Example: "ucloud ls --object region; ucloud ls --object project",
34+
Use: "region",
35+
Short: "List all region and zone",
36+
Long: "List all region and zone",
37+
Example: "ucloud region",
3838
Run: func(cmd *cobra.Command, args []string) {
39-
switch listObject {
40-
case "region":
41-
if err := listRegion(); err != nil {
42-
fmt.Println(err)
43-
}
44-
case "project":
45-
if err := listProject(); err != nil {
46-
fmt.Println(err)
47-
}
48-
default:
49-
fmt.Println("object should be region or project")
39+
if err := listRegion(); err != nil {
40+
Tracer.Println(err)
5041
}
5142
},
5243
}
53-
cmd.Flags().StringVar(&listObject, "object", "", "Object to list,region or project. Required")
54-
cmd.MarkFlagRequired("object")
5544
return cmd
5645
}
5746

@@ -72,6 +61,12 @@ func getDefaultRegion() (string, error) {
7261
return "", fmt.Errorf("No default region")
7362
}
7463

64+
//RegionTable 为显示region表格创建的类型
65+
type RegionTable struct {
66+
Region string
67+
Zones string
68+
}
69+
7570
func listRegion() error {
7671
req := &uaccount.GetRegionRequest{}
7772
resp, err := BizClient.GetRegion(req)
@@ -80,19 +75,22 @@ func listRegion() error {
8075
}
8176
if resp.RetCode != 0 {
8277
return fmt.Errorf("Something wrong. RetCode:%d, Message:%s", resp.RetCode, resp.Message)
83-
}
84-
var regionMap = map[string]bool{}
85-
var regionList []string
86-
for _, region := range resp.Regions {
87-
if _, ok := regionMap[region.Region]; !ok {
88-
regionList = append(regionList, region.Region)
78+
} else {
79+
regionList := make([]RegionTable, 0)
80+
regionMap := make(map[string][]string)
81+
for _, region := range resp.Regions {
82+
regionMap[region.Region] = append(regionMap[region.Region], region.Zone)
83+
}
84+
for region, zones := range regionMap {
85+
regionList = append(regionList, RegionTable{region, strings.Join(zones, ", ")})
86+
}
87+
if global.json {
88+
PrintJSON(regionList)
89+
} else {
90+
err = PrintTable(regionList, []string{"Region", "Zones"})
8991
}
90-
regionMap[region.Region] = true
91-
}
92-
for index, region := range regionList {
93-
fmt.Printf("[%2d] %s\n", index, region)
9492
}
95-
return nil
93+
return err
9694
}
9795

9896
func getDefaultProject() (string, error) {

cmd/root.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,22 @@ import (
2020

2121
"github.com/spf13/cobra"
2222

23-
"github.com/ucloud/ucloud-sdk-go/sdk/request"
24-
2523
"github.com/ucloud/ucloud-cli/model"
2624
. "github.com/ucloud/ucloud-cli/util"
2725
)
2826

2927
//GlobalFlag 几乎所有接口都需要的参数,例如 region zone projectID
3028
type GlobalFlag struct {
31-
debug bool
29+
debug bool
30+
json bool
31+
version bool
32+
completion bool
33+
config bool
34+
signup bool
3235
}
3336

37+
const version = "0.1.2"
38+
3439
var global GlobalFlag
3540

3641
//NewCmdRoot 创建rootCmd rootCmd represents the base command when called without any subcommands
@@ -40,20 +45,33 @@ func NewCmdRoot() *cobra.Command {
4045
Short: "UCloud CLI v" + version,
4146
Long: `UCloud CLI - manage UCloud resources and developer workflow`,
4247
BashCompletionFunction: "__ucloud_init_completion",
48+
Run: func(cmd *cobra.Command, args []string) {
49+
if global.version {
50+
Tracer.Printf("ucloud cli %s\n", version)
51+
} else if global.completion {
52+
NewCmdCompletion().Run(cmd, args)
53+
} else if global.config {
54+
config.ListConfig()
55+
} else if global.signup {
56+
NewCmdSignup().Run(cmd, args)
57+
} else {
58+
cmd.HelpFunc()(cmd, args)
59+
}
60+
},
4361
}
4462

45-
// cmd.PersistentFlags().StringVarP(&global.region, "region", "r", "", "Assign region(override default region of your config)")
46-
// cmd.PersistentFlags().StringVarP(&global.projectID, "project-id", "p", "", "Assign project-id(override default projec-id of your config)")
4763
cmd.PersistentFlags().BoolVarP(&global.debug, "debug", "d", false, "Running in debug mode")
64+
cmd.PersistentFlags().BoolVarP(&global.json, "json", "j", false, "Print result in JSON format whenever possible")
65+
cmd.Flags().BoolVar(&global.version, "version", false, "Display version")
66+
cmd.Flags().BoolVar(&global.completion, "completion", false, "Create or update completion scripts")
67+
cmd.Flags().BoolVar(&global.config, "config", false, "Display configuration")
68+
cmd.Flags().BoolVar(&global.signup, "signup", false, "Launch UCloud sign up page in browser")
4869

49-
cmd.AddCommand(NewCmdSignup())
5070
cmd.AddCommand(NewCmdConfig())
51-
cmd.AddCommand(NewCmdList())
71+
cmd.AddCommand(NewCmdRegion())
5272
cmd.AddCommand(NewCmdUHost())
5373
cmd.AddCommand(NewCmdEIP())
5474
cmd.AddCommand(NewCmdGssh())
55-
cmd.AddCommand(NewCmdCompletion())
56-
cmd.AddCommand(NewCmdVersion())
5775

5876
return cmd
5977
}
@@ -102,12 +120,3 @@ func initialize(cmd *cobra.Command) {
102120
}
103121
}
104122
}
105-
106-
func bindGlobalParam(req request.Common) {
107-
// if global.region != "" {
108-
// req.SetRegion(global.region)
109-
// }
110-
// if global.projectID != "" {
111-
// req.SetProjectId(global.projectID)
112-
// }
113-
}

0 commit comments

Comments
 (0)