Skip to content

Commit

Permalink
Merge pull request #43 from lixiaojun629/develop
Browse files Browse the repository at this point in the history
fixbug about base-url
  • Loading branch information
lixiaojun629 authored Oct 11, 2019
2 parents 89f8009 + 90b6a68 commit 9c765d5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export VERSION=0.1.25
export VERSION=0.1.26

.PHONY : install
install:
Expand Down
2 changes: 1 addition & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

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
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

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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ English | [简体中文](./README-CN.md)
![](./docs/_static/ucloud_cli_demo.gif)

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.
https://docs.ucloud.cn/software/cli/index
https://docs.ucloud.cn/developer/cli/index

## Installing ucloud-cli on macOS or Linux

Expand Down
69 changes: 40 additions & 29 deletions base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
const DefaultProfile = "default"

//Version 版本号
const Version = "0.1.25"
const Version = "0.1.26"

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

//ConfigBaseURL 输入BaseURL
func (p *AggConfig) ConfigBaseURL() error {
fmt.Printf("Default base-url(%s):", DefaultBaseURL)
_, err := fmt.Scanf("%s\n", &p.BaseURL)
if err != nil {
return err
}
p.BaseURL = strings.TrimSpace(p.BaseURL)
if len(p.BaseURL) == 0 {
p.BaseURL = DefaultBaseURL
}
return nil
}

//ConfigUploadLog agree upload log or not
func (p *AggConfig) ConfigUploadLog() error {
var input string
fmt.Print("Do you agree to upload log in local file ~/.ucloud/cli.log to help ucloud-cli get better(yes/no):")
fmt.Print("Do you agree to upload log in local file ~/.ucloud/cli.log to help ucloud-cli get better(yes|no):")
_, err := fmt.Scanf("%s\n", &input)
if err != nil {
HandleError(err)
Expand Down Expand Up @@ -578,39 +592,19 @@ func GetBizClient(ac *AggConfig) (*Client, error) {
if err != nil {
err = fmt.Errorf("parse timeout %ds failed: %v", ac.Timeout, err)
}
baseURL := ac.BaseURL
if Global.BaseURL != "" {
baseURL = Global.BaseURL
}
if Global.Timeout != 0 {
timeout = time.Duration(Global.Timeout) * time.Second
}
maxRetryTimes := *ac.MaxRetryTimes
if Global.MaxRetryTimes != -1 {
maxRetryTimes = Global.MaxRetryTimes
}
ClientConfig = &sdk.Config{
BaseUrl: baseURL,
BaseUrl: ac.BaseURL,
Timeout: timeout,
UserAgent: fmt.Sprintf("UCloud-CLI/%s", Version),
LogLevel: log.FatalLevel,
Region: ac.Region,
ProjectId: ac.ProjectID,
MaxRetries: maxRetryTimes,
MaxRetries: *ac.MaxRetryTimes,
}

if Global.PublicKey != "" && Global.PrivateKey != "" {
AuthCredential = &auth.Credential{
PublicKey: Global.PublicKey,
PrivateKey: Global.PrivateKey,
}
} else {
AuthCredential = &auth.Credential{
PublicKey: ac.PublicKey,
PrivateKey: ac.PrivateKey,
}
AuthCredential = &auth.Credential{
PublicKey: ac.PublicKey,
PrivateKey: ac.PrivateKey,
}

return NewClient(ClientConfig, AuthCredential), err
}

Expand Down Expand Up @@ -641,9 +635,9 @@ func InitConfig() {

if ins != nil {
ConfigIns = ins
} else {
ins = ConfigIns
}

mergeConfigIns(ConfigIns)
logCmd()

bc, err := GetBizClient(ConfigIns)
Expand All @@ -655,6 +649,23 @@ func InitConfig() {
}
}

func mergeConfigIns(ins *AggConfig) {
if Global.BaseURL != "" {
ins.BaseURL = Global.BaseURL
}
if Global.Timeout != 0 {
ins.Timeout = Global.Timeout
}
if Global.MaxRetryTimes != -1 {
ins.MaxRetryTimes = sdk.Int(Global.MaxRetryTimes)
}

if Global.PublicKey != "" && Global.PrivateKey != "" {
ins.PrivateKey = Global.PrivateKey
ins.PublicKey = Global.PublicKey
}
}

func init() {
//配置日志
err := initLog()
Expand Down
6 changes: 4 additions & 2 deletions base/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ func logCmd() {
args := make([]string, len(os.Args))
copy(args, os.Args)
for idx, arg := range args {
if strings.Contains(arg, "password") && idx <= len(args)-2 {
args[idx+1] = strings.Repeat("*", 8)
for _, word := range []string{"password", "private-key", "public-key"} {
if strings.Contains(arg, word) && idx <= len(args)-2 {
args[idx+1] = strings.Repeat("*", 8)
}
}
}
LogInfo(fmt.Sprintf("command: %s", strings.Join(args, " ")))
Expand Down
10 changes: 6 additions & 4 deletions cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ func NewCmdInit() *cobra.Command {
fmt.Println(configDesc)
base.ConfigIns.ConfigPublicKey()
base.ConfigIns.ConfigPrivateKey()
base.ConfigIns.ConfigBaseURL()

region, zone, err := getDefaultRegion()
region, err := fetchRegionWithConfig(base.ConfigIns)
if err != nil {
if uErr, ok := err.(uerr.Error); ok {
if uErr.Code() == 172 {
Expand All @@ -66,9 +67,9 @@ func NewCmdInit() *cobra.Command {
fmt.Println(err)
return
}
base.ConfigIns.Region = region
base.ConfigIns.Zone = zone
fmt.Printf("Configured default region:%s zone:%s\n", region, zone)
base.ConfigIns.Region = region.DefaultRegion
base.ConfigIns.Zone = region.DefaultZone
fmt.Printf("Configured default region:%s zone:%s\n", region.DefaultRegion, region.DefaultZone)

projectID, projectName, err := getDefaultProject()
if err != nil {
Expand All @@ -79,6 +80,7 @@ func NewCmdInit() *cobra.Command {
fmt.Printf("Configured default project:%s %s\n", projectID, projectName)
base.ConfigIns.Timeout = base.DefaultTimeoutSec
base.ConfigIns.BaseURL = base.DefaultBaseURL
base.ConfigIns.MaxRetryTimes = sdk.Int(base.DefaultMaxRetryTimes)
base.ConfigIns.Active = true
fmt.Printf("Configured default base url:%s\n", base.ConfigIns.BaseURL)
fmt.Printf("Configured default timeout_sec:%ds\n", base.ConfigIns.Timeout)
Expand Down
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func NewCmdDoc(out io.Writer) *cobra.Command {
log.Fatal(err)
}
case "douku":
err := doc.GenDoukuTree(rootCmd, dir, "software/cli/cmd/")
err := doc.GenDoukuTree(rootCmd, dir, "developer/cli/cmd/")
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -207,6 +207,8 @@ func Execute() {
}

func init() {
//-1表示不覆盖配置文件中的MaxRetryTimes参数
global.MaxRetryTimes = -1
for idx, arg := range os.Args {
if arg == "--profile" && len(os.Args) > idx+1 && os.Args[idx+1] != "" {
global.Profile = os.Args[idx+1]
Expand Down Expand Up @@ -235,7 +237,6 @@ func init() {
} else {
global.MaxRetryTimes = times
}
global.MaxRetryTimes = times
}
}
cobra.EnableCommandSorting = false
Expand Down

0 comments on commit 9c765d5

Please sign in to comment.