Skip to content

Commit 9c765d5

Browse files
authored
Merge pull request #43 from lixiaojun629/develop
fixbug about base-url
2 parents 89f8009 + 90b6a68 commit 9c765d5

7 files changed

Lines changed: 56 additions & 40 deletions

File tree

Makefile

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

33
.PHONY : install
44
install:

README-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
![](./docs/_static/ucloud_cli_demo.gif)
55

6-
UCloud CLI为管理UCloud平台上的资源和服务提供了一致性的操作接口,它使用[ucloud-sdk-go](https://github.com/ucloud/ucloud-sdk-go)调用[UCloud OpenAPI](https://docs.ucloud.cn/api/summary/overview),从而实现对资源和服务的操作,兼容Linux, macOS和Windows平台 https://docs.ucloud.cn/software/cli/index
6+
UCloud CLI为管理UCloud平台上的资源和服务提供了一致性的操作接口,它使用[ucloud-sdk-go](https://github.com/ucloud/ucloud-sdk-go)调用[UCloud OpenAPI](https://docs.ucloud.cn/api/summary/overview),从而实现对资源和服务的操作,兼容Linux, macOS和Windows平台 https://docs.ucloud.cn/developer/cli/index
77

88
## 在macOS或Linux平台安装UCloud-CLI
99

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ English | [简体中文](./README-CN.md)
55
![](./docs/_static/ucloud_cli_demo.gif)
66

77
The UCloud CLI provides a unified command line interface to UCloud services. It works on [ucloud-sdk-go](https://github.com/ucloud/ucloud-sdk-go) based on UCloud OpenAPI and supports Linux, macOS and Windows.
8-
https://docs.ucloud.cn/software/cli/index
8+
https://docs.ucloud.cn/developer/cli/index
99

1010
## Installing ucloud-cli on macOS or Linux
1111

base/config.go

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
3636
const DefaultProfile = "default"
3737

3838
//Version 版本号
39-
const Version = "0.1.25"
39+
const Version = "0.1.26"
4040

4141
//ConfigIns 配置实例, 程序加载时生成
4242
var ConfigIns = &AggConfig{
@@ -138,10 +138,24 @@ func (p *AggConfig) ConfigPrivateKey() error {
138138
return nil
139139
}
140140

141+
//ConfigBaseURL 输入BaseURL
142+
func (p *AggConfig) ConfigBaseURL() error {
143+
fmt.Printf("Default base-url(%s):", DefaultBaseURL)
144+
_, err := fmt.Scanf("%s\n", &p.BaseURL)
145+
if err != nil {
146+
return err
147+
}
148+
p.BaseURL = strings.TrimSpace(p.BaseURL)
149+
if len(p.BaseURL) == 0 {
150+
p.BaseURL = DefaultBaseURL
151+
}
152+
return nil
153+
}
154+
141155
//ConfigUploadLog agree upload log or not
142156
func (p *AggConfig) ConfigUploadLog() error {
143157
var input string
144-
fmt.Print("Do you agree to upload log in local file ~/.ucloud/cli.log to help ucloud-cli get better(yes/no):")
158+
fmt.Print("Do you agree to upload log in local file ~/.ucloud/cli.log to help ucloud-cli get better(yes|no):")
145159
_, err := fmt.Scanf("%s\n", &input)
146160
if err != nil {
147161
HandleError(err)
@@ -578,39 +592,19 @@ func GetBizClient(ac *AggConfig) (*Client, error) {
578592
if err != nil {
579593
err = fmt.Errorf("parse timeout %ds failed: %v", ac.Timeout, err)
580594
}
581-
baseURL := ac.BaseURL
582-
if Global.BaseURL != "" {
583-
baseURL = Global.BaseURL
584-
}
585-
if Global.Timeout != 0 {
586-
timeout = time.Duration(Global.Timeout) * time.Second
587-
}
588-
maxRetryTimes := *ac.MaxRetryTimes
589-
if Global.MaxRetryTimes != -1 {
590-
maxRetryTimes = Global.MaxRetryTimes
591-
}
592595
ClientConfig = &sdk.Config{
593-
BaseUrl: baseURL,
596+
BaseUrl: ac.BaseURL,
594597
Timeout: timeout,
595598
UserAgent: fmt.Sprintf("UCloud-CLI/%s", Version),
596599
LogLevel: log.FatalLevel,
597600
Region: ac.Region,
598601
ProjectId: ac.ProjectID,
599-
MaxRetries: maxRetryTimes,
602+
MaxRetries: *ac.MaxRetryTimes,
600603
}
601-
602-
if Global.PublicKey != "" && Global.PrivateKey != "" {
603-
AuthCredential = &auth.Credential{
604-
PublicKey: Global.PublicKey,
605-
PrivateKey: Global.PrivateKey,
606-
}
607-
} else {
608-
AuthCredential = &auth.Credential{
609-
PublicKey: ac.PublicKey,
610-
PrivateKey: ac.PrivateKey,
611-
}
604+
AuthCredential = &auth.Credential{
605+
PublicKey: ac.PublicKey,
606+
PrivateKey: ac.PrivateKey,
612607
}
613-
614608
return NewClient(ClientConfig, AuthCredential), err
615609
}
616610

@@ -641,9 +635,9 @@ func InitConfig() {
641635

642636
if ins != nil {
643637
ConfigIns = ins
644-
} else {
645-
ins = ConfigIns
646638
}
639+
640+
mergeConfigIns(ConfigIns)
647641
logCmd()
648642

649643
bc, err := GetBizClient(ConfigIns)
@@ -655,6 +649,23 @@ func InitConfig() {
655649
}
656650
}
657651

652+
func mergeConfigIns(ins *AggConfig) {
653+
if Global.BaseURL != "" {
654+
ins.BaseURL = Global.BaseURL
655+
}
656+
if Global.Timeout != 0 {
657+
ins.Timeout = Global.Timeout
658+
}
659+
if Global.MaxRetryTimes != -1 {
660+
ins.MaxRetryTimes = sdk.Int(Global.MaxRetryTimes)
661+
}
662+
663+
if Global.PublicKey != "" && Global.PrivateKey != "" {
664+
ins.PrivateKey = Global.PrivateKey
665+
ins.PublicKey = Global.PublicKey
666+
}
667+
}
668+
658669
func init() {
659670
//配置日志
660671
err := initLog()

base/log.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ func logCmd() {
5353
args := make([]string, len(os.Args))
5454
copy(args, os.Args)
5555
for idx, arg := range args {
56-
if strings.Contains(arg, "password") && idx <= len(args)-2 {
57-
args[idx+1] = strings.Repeat("*", 8)
56+
for _, word := range []string{"password", "private-key", "public-key"} {
57+
if strings.Contains(arg, word) && idx <= len(args)-2 {
58+
args[idx+1] = strings.Repeat("*", 8)
59+
}
5860
}
5961
}
6062
LogInfo(fmt.Sprintf("command: %s", strings.Join(args, " ")))

cmd/configure.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ func NewCmdInit() *cobra.Command {
5454
fmt.Println(configDesc)
5555
base.ConfigIns.ConfigPublicKey()
5656
base.ConfigIns.ConfigPrivateKey()
57+
base.ConfigIns.ConfigBaseURL()
5758

58-
region, zone, err := getDefaultRegion()
59+
region, err := fetchRegionWithConfig(base.ConfigIns)
5960
if err != nil {
6061
if uErr, ok := err.(uerr.Error); ok {
6162
if uErr.Code() == 172 {
@@ -66,9 +67,9 @@ func NewCmdInit() *cobra.Command {
6667
fmt.Println(err)
6768
return
6869
}
69-
base.ConfigIns.Region = region
70-
base.ConfigIns.Zone = zone
71-
fmt.Printf("Configured default region:%s zone:%s\n", region, zone)
70+
base.ConfigIns.Region = region.DefaultRegion
71+
base.ConfigIns.Zone = region.DefaultZone
72+
fmt.Printf("Configured default region:%s zone:%s\n", region.DefaultRegion, region.DefaultZone)
7273

7374
projectID, projectName, err := getDefaultProject()
7475
if err != nil {
@@ -79,6 +80,7 @@ func NewCmdInit() *cobra.Command {
7980
fmt.Printf("Configured default project:%s %s\n", projectID, projectName)
8081
base.ConfigIns.Timeout = base.DefaultTimeoutSec
8182
base.ConfigIns.BaseURL = base.DefaultBaseURL
83+
base.ConfigIns.MaxRetryTimes = sdk.Int(base.DefaultMaxRetryTimes)
8284
base.ConfigIns.Active = true
8385
fmt.Printf("Configured default base url:%s\n", base.ConfigIns.BaseURL)
8486
fmt.Printf("Configured default timeout_sec:%ds\n", base.ConfigIns.Timeout)

cmd/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func NewCmdDoc(out io.Writer) *cobra.Command {
9898
log.Fatal(err)
9999
}
100100
case "douku":
101-
err := doc.GenDoukuTree(rootCmd, dir, "software/cli/cmd/")
101+
err := doc.GenDoukuTree(rootCmd, dir, "developer/cli/cmd/")
102102
if err != nil {
103103
log.Fatal(err)
104104
}
@@ -207,6 +207,8 @@ func Execute() {
207207
}
208208

209209
func init() {
210+
//-1表示不覆盖配置文件中的MaxRetryTimes参数
211+
global.MaxRetryTimes = -1
210212
for idx, arg := range os.Args {
211213
if arg == "--profile" && len(os.Args) > idx+1 && os.Args[idx+1] != "" {
212214
global.Profile = os.Args[idx+1]
@@ -235,7 +237,6 @@ func init() {
235237
} else {
236238
global.MaxRetryTimes = times
237239
}
238-
global.MaxRetryTimes = times
239240
}
240241
}
241242
cobra.EnableCommandSorting = false

0 commit comments

Comments
 (0)