Skip to content

Commit 12895ae

Browse files
committed
refactor
1 parent 20f8586 commit 12895ae

22 files changed

Lines changed: 596 additions & 319 deletions

File tree

cmd/completion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func getBashVersion() (version string, err error) {
9797
lookupBashVersion := exec.Command("bash", "-version")
9898
out, err := lookupBashVersion.Output()
9999
if err != nil {
100-
context.AppendError(err)
100+
util.Tracer.AppendError(err)
101101
fmt.Println(err)
102102
}
103103

@@ -141,7 +141,7 @@ func zshCompletion(cmd *cobra.Command) {
141141
file, err := os.Create(shellPath)
142142
if err != nil {
143143
fmt.Println(err)
144-
context.AppendError(err)
144+
util.Tracer.AppendError(err)
145145
return
146146
}
147147
defer file.Close()

cmd/configure.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020

2121
"github.com/spf13/cobra"
2222

23-
"github.com/ucloud/ucloud-cli/model"
23+
. "github.com/ucloud/ucloud-cli/util"
2424
)
2525

26-
var config = model.ConfigInstance
26+
var config = ConfigInstance
2727

2828
//NewCmdConfig ucloud config
2929
func NewCmdConfig() *cobra.Command {
@@ -58,22 +58,25 @@ func NewCmdConfig() *cobra.Command {
5858
return
5959
}
6060
}
61+
config.ClearConfig()
62+
ClientConfig.Region = ""
63+
ClientConfig.ProjectId = ""
6164
config.ConfigPublicKey()
6265
config.ConfigPrivateKey()
6366

6467
region, err := getDefaultRegion()
6568
if err != nil {
66-
context.AppendError(err)
67-
fmt.Println(err)
69+
Tracer.Println(err)
70+
return
6871
} else {
6972
config.Region = region
7073
fmt.Printf("Configured default region:%s\n", region)
7174
}
7275

7376
project, err := getDefaultProject()
7477
if err != nil {
75-
context.AppendError(err)
76-
fmt.Println(err)
78+
Tracer.Println(err)
79+
return
7780
} else {
7881
config.ProjectID = project
7982
fmt.Printf("Configured default project:%s\n", project)

cmd/eip.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919

2020
"github.com/spf13/cobra"
21+
. "github.com/ucloud/ucloud-cli/util"
2122
)
2223

2324
//NewCmdEIP ucloud eip
@@ -38,15 +39,15 @@ func NewCmdEIP() *cobra.Command {
3839

3940
//NewCmdEIPList ucloud eip ls
4041
func NewCmdEIPList() *cobra.Command {
41-
req := client.NewDescribeEIPRequest()
42+
req := BizClient.NewDescribeEIPRequest()
4243
var cmd = &cobra.Command{
4344
Use: "ls",
4445
Short: "List all EIP instances",
4546
Long: `List all EIP instances`,
4647
Example: "ucloud eip ls",
4748
Run: func(cmd *cobra.Command, args []string) {
4849
bindGlobalParam(req)
49-
resp, err := client.DescribeEIP(req)
50+
resp, err := BizClient.DescribeEIP(req)
5051
if err != nil {
5152
fmt.Println("Error:", err)
5253
return
@@ -69,15 +70,15 @@ func NewCmdEIPList() *cobra.Command {
6970

7071
//NewCmdEIPAllocate ucloud eip allocate
7172
func NewCmdEIPAllocate() *cobra.Command {
72-
var eipAllocateReq = client.NewAllocateEIPRequest()
73+
var eipAllocateReq = BizClient.NewAllocateEIPRequest()
7374
var cmd = &cobra.Command{
7475
Use: "allocate",
7576
Short: "Allocate EIP",
7677
Long: "Allocate EIP",
7778
Example: "ucloud eip allocate --line Bgp --bandwidth 2",
7879
Run: func(cmd *cobra.Command, args []string) {
7980
bindGlobalParam(eipAllocateReq)
80-
resp, err := client.AllocateEIP(eipAllocateReq)
81+
resp, err := BizClient.AllocateEIP(eipAllocateReq)
8182
if err != nil {
8283
fmt.Println(err)
8384
} else {
@@ -111,7 +112,7 @@ func NewCmdEIPAllocate() *cobra.Command {
111112

112113
//NewCmdEIPBind ucloud eip bind
113114
func NewCmdEIPBind() *cobra.Command {
114-
var eipBindReq = client.NewBindEIPRequest()
115+
var eipBindReq = BizClient.NewBindEIPRequest()
115116
var eipBindCmd = &cobra.Command{
116117
Use: "bind",
117118
Short: "Bind EIP with uhost",
@@ -120,7 +121,7 @@ func NewCmdEIPBind() *cobra.Command {
120121
Run: func(cmd *cobra.Command, args []string) {
121122
bindGlobalParam(eipBindReq)
122123
eipBindReq.ResourceType = "uhost"
123-
resp, err := client.BindEIP(eipBindReq)
124+
resp, err := BizClient.BindEIP(eipBindReq)
124125
if err != nil {
125126
fmt.Println(err)
126127
} else {
@@ -143,7 +144,7 @@ func NewCmdEIPBind() *cobra.Command {
143144
//NewCmdEIPUnbind ucloud eip unbind
144145
func NewCmdEIPUnbind() *cobra.Command {
145146

146-
var eipUnBindReq = client.NewUnBindEIPRequest()
147+
var eipUnBindReq = BizClient.NewUnBindEIPRequest()
147148
var eipUnBindCmd = &cobra.Command{
148149
Use: "unbind",
149150
Short: "Unbind EIP with uhost",
@@ -152,7 +153,7 @@ func NewCmdEIPUnbind() *cobra.Command {
152153
Run: func(cmd *cobra.Command, args []string) {
153154
bindGlobalParam(eipUnBindReq)
154155
eipUnBindReq.ResourceType = "uhost"
155-
resp, err := client.UnBindEIP(eipUnBindReq)
156+
resp, err := BizClient.UnBindEIP(eipUnBindReq)
156157
if err != nil {
157158
fmt.Println(err)
158159
} else {
@@ -183,10 +184,10 @@ func NewCmdEIPRelease() *cobra.Command {
183184
Example: "ucloud eip release --eip-id eip-xx1 --eip-id eip-xx2",
184185
Run: func(cmd *cobra.Command, args []string) {
185186
for _, id := range ids {
186-
var eipReleaseReq = client.NewReleaseEIPRequest()
187+
var eipReleaseReq = BizClient.NewReleaseEIPRequest()
187188
bindGlobalParam(eipReleaseReq)
188189
eipReleaseReq.EIPId = id
189-
resp, err := client.ReleaseEIP(eipReleaseReq)
190+
resp, err := BizClient.ReleaseEIP(eipReleaseReq)
190191
if err != nil {
191192
fmt.Println(err)
192193
} else {

cmd/globalssh.go

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

2121
"github.com/spf13/cobra"
22+
. "github.com/ucloud/ucloud-cli/util"
2223
)
2324

2425
//NewCmdGssh ucloud gssh
@@ -37,15 +38,15 @@ func NewCmdGssh() *cobra.Command {
3738

3839
//NewCmdGsshList ucloud gssh list
3940
func NewCmdGsshList() *cobra.Command {
40-
req := client.NewDescribeGlobalSSHInstanceRequest()
41+
req := BizClient.NewDescribeGlobalSSHInstanceRequest()
4142
var cmd = &cobra.Command{
4243
Use: "ls",
4344
Short: "List all GlobalSSH instances",
4445
Long: `List all GlobalSSH instances`,
4546
Example: "ucloud gssh ls",
4647
Run: func(cmd *cobra.Command, args []string) {
4748
bindGlobalParam(req)
48-
resp, err := client.DescribeGlobalSSHInstance(req)
49+
resp, err := BizClient.DescribeGlobalSSHInstance(req)
4950
if err != nil {
5051
fmt.Println("Error", err)
5152
} else {
@@ -64,7 +65,7 @@ func NewCmdGsshList() *cobra.Command {
6465

6566
//NewCmdGsshCreate ucloud gssh create
6667
func NewCmdGsshCreate() *cobra.Command {
67-
var gsshCreateReq = client.NewCreateGlobalSSHInstanceRequest()
68+
var gsshCreateReq = BizClient.NewCreateGlobalSSHInstanceRequest()
6869
var cmd = &cobra.Command{
6970
Use: "create",
7071
Short: "Create GlobalSSH instance",
@@ -97,7 +98,7 @@ func NewCmdGsshCreate() *cobra.Command {
9798
fmt.Println("Area should be one of LosAngeles,Singapore,HongKong,Tokyo,Washington,Frankfurt.")
9899
return
99100
}
100-
resp, err := client.CreateGlobalSSHInstance(gsshCreateReq)
101+
resp, err := BizClient.CreateGlobalSSHInstance(gsshCreateReq)
101102
if err != nil {
102103
fmt.Println("Error:", err)
103104
} else {
@@ -122,7 +123,7 @@ func NewCmdGsshCreate() *cobra.Command {
122123

123124
//NewCmdGsshDelete ucloud gssh delete
124125
func NewCmdGsshDelete() *cobra.Command {
125-
var gsshDeleteReq = client.NewDeleteGlobalSSHInstanceRequest()
126+
var gsshDeleteReq = BizClient.NewDeleteGlobalSSHInstanceRequest()
126127
var gsshIds []string
127128
var cmd = &cobra.Command{
128129
Use: "delete",
@@ -134,10 +135,10 @@ func NewCmdGsshDelete() *cobra.Command {
134135
for _, id := range gsshIds {
135136
gsshDeleteReq.InstanceId = id
136137

137-
if global.projectID != "" {
138-
gsshDeleteReq.ProjectId = global.projectID
139-
}
140-
resp, err := client.DeleteGlobalSSHInstance(gsshDeleteReq)
138+
// if global.projectID != "" {
139+
// gsshDeleteReq.ProjectId = global.projectID
140+
// }
141+
resp, err := BizClient.DeleteGlobalSSHInstance(gsshDeleteReq)
141142
if err != nil {
142143
fmt.Println("Error:", err)
143144
} else {
@@ -157,8 +158,8 @@ func NewCmdGsshDelete() *cobra.Command {
157158

158159
//NewCmdGsshModify ucloud gssh modify
159160
func NewCmdGsshModify() *cobra.Command {
160-
var gsshModifyPortReq = client.NewModifyGlobalSSHPortRequest()
161-
var gsshModifyRemarkReq = client.NewModifyGlobalSSHRemarkRequest()
161+
var gsshModifyPortReq = BizClient.NewModifyGlobalSSHPortRequest()
162+
var gsshModifyRemarkReq = BizClient.NewModifyGlobalSSHRemarkRequest()
162163
var cmd = &cobra.Command{
163164
Use: "modify",
164165
Short: "Modify GlobalSSH instance",
@@ -181,7 +182,7 @@ func NewCmdGsshModify() *cobra.Command {
181182
return
182183
}
183184
gsshModifyPortReq.InstanceId = gsshModifyRemarkReq.InstanceId
184-
resp, err := client.ModifyGlobalSSHPort(gsshModifyPortReq)
185+
resp, err := BizClient.ModifyGlobalSSHPort(gsshModifyPortReq)
185186
if err != nil {
186187
fmt.Println("Error:", err)
187188
} else {
@@ -193,7 +194,7 @@ func NewCmdGsshModify() *cobra.Command {
193194
}
194195
}
195196
if gsshModifyRemarkReq.Remark != "" {
196-
resp, err := client.ModifyGlobalSSHRemark(gsshModifyRemarkReq)
197+
resp, err := BizClient.ModifyGlobalSSHRemark(gsshModifyRemarkReq)
197198
if err != nil {
198199
fmt.Println("Error:", err)
199200
} else {

cmd/list.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ import (
2020
"io/ioutil"
2121

2222
"github.com/spf13/cobra"
23-
"github.com/ucloud/ucloud-cli/model"
24-
"github.com/ucloud/ucloud-cli/util"
23+
2524
"github.com/ucloud/ucloud-sdk-go/service/uaccount"
2625
"github.com/ucloud/ucloud-sdk-go/service/uaccount/types"
26+
27+
. "github.com/ucloud/ucloud-cli/util"
2728
)
2829

2930
//NewCmdList ucloud ls
@@ -37,9 +38,13 @@ func NewCmdList() *cobra.Command {
3738
Run: func(cmd *cobra.Command, args []string) {
3839
switch listObject {
3940
case "region":
40-
listRegion()
41+
if err := listRegion(); err != nil {
42+
fmt.Println(err)
43+
}
4144
case "project":
42-
listProject()
45+
if err := listProject(); err != nil {
46+
fmt.Println(err)
47+
}
4348
default:
4449
fmt.Println("object should be region or project")
4550
}
@@ -52,7 +57,7 @@ func NewCmdList() *cobra.Command {
5257

5358
func getDefaultRegion() (string, error) {
5459
req := &uaccount.GetRegionRequest{}
55-
resp, err := client.GetRegion(req)
60+
resp, err := BizClient.GetRegion(req)
5661
if err != nil {
5762
return "", err
5863
}
@@ -69,7 +74,7 @@ func getDefaultRegion() (string, error) {
6974

7075
func listRegion() error {
7176
req := &uaccount.GetRegionRequest{}
72-
resp, err := client.GetRegion(req)
77+
resp, err := BizClient.GetRegion(req)
7378
if err != nil {
7479
return err
7580
}
@@ -91,8 +96,8 @@ func listRegion() error {
9196
}
9297

9398
func getDefaultProject() (string, error) {
94-
req := client.NewGetProjectListRequest()
95-
resp, err := client.GetProjectList(req)
99+
req := BizClient.NewGetProjectListRequest()
100+
resp, err := BizClient.GetProjectList(req)
96101
if err != nil {
97102
return "", err
98103
}
@@ -109,7 +114,7 @@ func getDefaultProject() (string, error) {
109114

110115
func listProject() error {
111116
req := &uaccount.GetProjectListRequest{}
112-
resp, err := client.GetProjectList(req)
117+
resp, err := BizClient.GetProjectList(req)
113118
if err != nil {
114119
return err
115120
}
@@ -127,9 +132,9 @@ func isUserCertified(userInfo *types.UserInfo) bool {
127132
}
128133

129134
func getUserInfo() (*types.UserInfo, error) {
130-
req := client.NewGetUserInfoRequest()
135+
req := BizClient.NewGetUserInfoRequest()
131136
var userInfo types.UserInfo
132-
resp, err := client.GetUserInfo(req)
137+
resp, err := BizClient.GetUserInfo(req)
133138

134139
if err != nil {
135140
return nil, err
@@ -140,14 +145,13 @@ func getUserInfo() (*types.UserInfo, error) {
140145
}
141146
if len(resp.DataSet) == 1 {
142147
userInfo = resp.DataSet[0]
143-
model.ClientConfig.TracerData["userName"] = userInfo.UserEmail
144-
model.ClientConfig.TracerData["userID"] = userInfo.UserEmail
145-
model.ClientConfig.TracerData["companyName"] = userInfo.CompanyName
148+
Tracer.AppendInfo("userName", userInfo.UserEmail)
149+
Tracer.AppendInfo("companyName", userInfo.CompanyName)
146150
bytes, err := json.Marshal(userInfo)
147151
if err != nil {
148152
return nil, err
149153
}
150-
fileFullPath := util.GetConfigPath() + "/user.json"
154+
fileFullPath := GetConfigPath() + "/user.json"
151155
err = ioutil.WriteFile(fileFullPath, bytes, 0600)
152156
if err != nil {
153157
return nil, err

0 commit comments

Comments
 (0)