Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions base/branding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package base

// 编译时用 -ldflags 覆盖
var (
BrandName = "UCloud"
BrandNameLower = "ucloud"
BrandAPIURL = "https://api.ucloud.cn/"
BrandUmountURL = "https://docs.ucloud.cn/storage_cdn/udisk/userguide/umount"
BrandLogURL = "https://das-rpt.ucloud.cn/log"
BrandAuthURL = "https://accountv2.ucloud.cn/authentication"
BrandAccountURL = "https://accountv2.ucloud.cn"
BrandRegisterURL = "https://passport.ucloud.cn/#register"
BrandUhostTypeURL = "https://docs.ucloud.cn/api/uhost-api/uhost_type"
BrandDiskTypeURL = "https://docs.ucloud.cn/api/uhost-api/disk_type"
BrandUserDataURL = "https://docs.ucloud.cn/uhost/guide/metadata/userdata"
)
13 changes: 7 additions & 6 deletions base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ const DefaultTimeoutSec = 15
const DefaultMaxRetryTimes = 3

// DefaultBaseURL location of api server
const DefaultBaseURL = "https://api.ucloud.cn/"
var DefaultBaseURL string

// DefaultProfile name of default profile
const DefaultProfile = "default"

// Version 版本号
const Version = "0.3.0"

var UserAgent = fmt.Sprintf("UCloud-CLI/%s", Version)
var UserAgent = fmt.Sprintf("%s-CLI/%s", BrandName, Version)

var InCloudShell = os.Getenv("CLOUD_SHELL") == "true"

Expand Down Expand Up @@ -166,7 +166,7 @@ func (p *AggConfig) ConfigBaseURL() error {
// 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.Printf("Do you agree to upload log in local file ~/%s/cli.log to help %s-cli get better(yes|no):", ConfigPath, BrandNameLower)
_, err := fmt.Scanf("%s\n", &input)
if err != nil {
HandleError(err)
Expand Down Expand Up @@ -342,10 +342,10 @@ func (p *AggConfigManager) Load() error {
}

if p.activeProfile == "" && len(configMap) > 0 {
return fmt.Errorf("no active config found, run 'ucloud config list' to check")
return fmt.Errorf("no active config found, run '%s config list' to check", BrandNameLower)
}
if _, ok := credMap[p.activeProfile]; p.activeProfile != "" && !ok {
return fmt.Errorf("profile %s's credential don't exist, run 'ucloud config list' to check", p.activeProfile)
return fmt.Errorf("profile %s's credential don't exist, run '%s config list' to check", p.activeProfile, BrandNameLower)
}

return nil
Expand Down Expand Up @@ -544,7 +544,7 @@ func (p *AggConfigManager) GetActiveAggConfig() (*AggConfig, error) {
if ac, ok := p.configs[p.activeProfile]; ok {
return ac, nil
}
return nil, fmt.Errorf("active profile not found. see 'ucloud config list'")
return nil, fmt.Errorf("active profile not found. see '%s config list'", BrandNameLower)
}

// GetActiveAggConfigName get active config name
Expand Down Expand Up @@ -847,6 +847,7 @@ func mergeConfigIns(ins *AggConfig) {
}

func init() {
DefaultBaseURL = fmt.Sprintf("%s", BrandAPIURL)
//配置日志
err := initLog()
if err != nil {
Expand Down
27 changes: 15 additions & 12 deletions base/config_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package base

import (
"fmt"
"io/ioutil"
"os"
"testing"
)

const cliConfigJSON = `[
{"project_id":"org-bdks4e","region":"cn-bj2","zone":"cn-bj2-04","base_url":"https://api.ucloud.cn/","timeout_sec":15,"profile":"uweb","active":true},
{"project_id":"org-oxjwoi","region":"hk","zone":"hk-02","base_url":"https://api.ucloud.cn/","timeout_sec":15,"profile":"test","active":false}
{"project_id":"org-bdks4e","region":"cn-bj2","zone":"cn-bj2-04","base_url":"%s","timeout_sec":15,"profile":"uweb","active":true},
{"project_id":"org-oxjwoi","region":"hk","zone":"hk-02","base_url":"%s","timeout_sec":15,"profile":"test","active":false}
]`

const credentialJSON = `[
Expand All @@ -17,28 +18,30 @@ const credentialJSON = `[
]`

func TestAggConfigManager(t *testing.T) {
os.MkdirAll(".ucloud", 0700)
err := ioutil.WriteFile(".ucloud/config.json", []byte(cliConfigJSON), LocalFileMode)
os.MkdirAll(ConfigPath, 0700)
baseURL := fmt.Sprintf("%s", BrandAPIURL)
configJSON := fmt.Sprintf(cliConfigJSON, baseURL, baseURL)
err := ioutil.WriteFile(ConfigPath+"/config.json", []byte(configJSON), LocalFileMode)
if err != nil {
t.Error(err)
}
err = ioutil.WriteFile(".ucloud/credential.json", []byte(credentialJSON), LocalFileMode)
err = ioutil.WriteFile(ConfigPath+"/credential.json", []byte(credentialJSON), LocalFileMode)
if err != nil {
t.Error(err)
}
defer func() {
err := os.RemoveAll(".ucloud")
err := os.RemoveAll(ConfigPath)
if err != nil {
t.Error(err)
}
}()

configFile, err := os.OpenFile(".ucloud/config.json", os.O_CREATE|os.O_RDONLY, LocalFileMode)
configFile, err := os.OpenFile(ConfigPath+"/config.json", os.O_CREATE|os.O_RDONLY, LocalFileMode)
if err != nil {
t.Error(err)
}

credFile, err := os.OpenFile(".ucloud/credential.json", os.O_CREATE|os.O_RDONLY, LocalFileMode)
credFile, err := os.OpenFile(ConfigPath+"/credential.json", os.O_CREATE|os.O_RDONLY, LocalFileMode)
if err != nil {
t.Error(err)
}
Expand All @@ -55,20 +58,20 @@ func TestAggConfigManager(t *testing.T) {
}

func TestEmptyAggConfigManager(t *testing.T) {
os.MkdirAll(".ucloud", 0700)
os.MkdirAll(ConfigPath, 0700)
defer func() {
err := os.RemoveAll(".ucloud")
err := os.RemoveAll(ConfigPath)
if err != nil {
t.Error(err)
}
}()

configFile, err := os.OpenFile(".ucloud/config.json", os.O_CREATE|os.O_RDONLY, LocalFileMode)
configFile, err := os.OpenFile(ConfigPath+"/config.json", os.O_CREATE|os.O_RDONLY, LocalFileMode)
if err != nil {
t.Error(err)
}

credFile, err := os.OpenFile(".ucloud/credential.json", os.O_CREATE|os.O_RDONLY, LocalFileMode)
credFile, err := os.OpenFile(ConfigPath+"/credential.json", os.O_CREATE|os.O_RDONLY, LocalFileMode)
if err != nil {
t.Error(err)
}
Expand Down
7 changes: 6 additions & 1 deletion base/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import (
"github.com/ucloud/ucloud-sdk-go/ucloud/version"
)

const DefaultDasURL = "https://das-rpt.ucloud.cn/log"
// DefaultDasURL location of das server
var DefaultDasURL string

func init() {
DefaultDasURL = fmt.Sprintf("%s", BrandLogURL)
}

// Logger 日志
var logger *log.Logger
Expand Down
2 changes: 1 addition & 1 deletion base/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

// ConfigPath 配置文件路径
const ConfigPath = ".ucloud"
var ConfigPath = fmt.Sprintf(".%s", BrandNameLower)

// GAP 表格列直接的间隔字符数
const GAP = 2
Expand Down
4 changes: 2 additions & 2 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const RepeatsField = "repeats"
const ConcurrentField = "concurrent"
const DefaultConcurrent = 20
const HelpField = "help"
const HelpInfo = `Usage: ucloud api [options] --Action actionName --param1 value1 --param2 value2 ...
const HelpInfo = `Usage: %s api [options] --Action actionName --param1 value1 --param2 value2 ...
Options:
--local-file string the path of the local file which contains the api parameters
--repeats string the number of repeats
Expand All @@ -50,7 +50,7 @@ func NewCmdAPI(out io.Writer) *cobra.Command {
Long: "Call API",
Run: func(c *cobra.Command, args []string) {
if containHelp(args) {
fmt.Fprintln(out, HelpInfo)
fmt.Fprintln(out, fmt.Sprintf(HelpInfo, base.BrandNameLower))
return
}
params, err := parseParamsFromCmdLine(args)
Expand Down
8 changes: 4 additions & 4 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func NewCmdUDBBackupDelete(out io.Writer) *cobra.Command {
Use: "delete",
Short: "Delete backups of MySQL instance",
Long: "Delete backups of MySQL instance",
Example: "ucloud udb backup delete --backup-id 65534,65535",
Example: fmt.Sprintf("%s udb backup delete --backup-id 65534,65535", base.BrandNameLower),
Run: func(c *cobra.Command, args []string) {
for _, id := range ids {
req.BackupId = sdk.Int(id)
Expand Down Expand Up @@ -280,7 +280,7 @@ func NewCmdUDBLogArchiveCreate(out io.Writer) *cobra.Command {
Use: "archive",
Short: "Archive the log of mysql as a compressed file",
Long: "Archive the log of mysql as a compressed file",
Example: "ucloud mysql logs archive --name test.cli2 --udb-id udb-xxx/test.cli1 --log-type slow_query --begin-time 2019-02-23/15:30:00 --end-time 2019-02-24/15:31:00",
Example: fmt.Sprintf("%s mysql logs archive --name test.cli2 --udb-id udb-xxx/test.cli1 --log-type slow_query --begin-time 2019-02-23/15:30:00 --end-time 2019-02-24/15:31:00", base.BrandNameLower),
Run: func(c *cobra.Command, args []string) {
udbID = base.PickResourceID(udbID)
if logType == "slow_query" {
Expand Down Expand Up @@ -463,7 +463,7 @@ func NewCmdUDBLogArchiveGetDownloadURL(out io.Writer) *cobra.Command {
Use: "download",
Short: "Display url of an archive(log file)",
Long: "Display url of an archive(log file)",
Example: "ucloud mysql logs download --udb-id udb-urixxx/test.cli1 --archive-id 35044",
Example: fmt.Sprintf("%s mysql logs download --udb-id udb-urixxx/test.cli1 --archive-id 35044", base.BrandNameLower),
Run: func(c *cobra.Command, args []string) {
*req.DBId = base.PickResourceID(*req.DBId)
resp, err := base.BizClient.DescribeUDBBinlogBackupURL(req)
Expand Down Expand Up @@ -501,7 +501,7 @@ func NewCmdUDBLogArchiveDelete(out io.Writer) *cobra.Command {
Use: "delete",
Short: "Delete log archives(log files)",
Long: "Delete log archives(log files)",
Example: "ucloud mysql logs delete --archive-id 35025",
Example: fmt.Sprintf("%s mysql logs delete --archive-id 35025", base.BrandNameLower),
Run: func(c *cobra.Command, args []string) {
for _, id := range ids {
req.BackupId = sdk.Int(id)
Expand Down
4 changes: 2 additions & 2 deletions cmd/bandwidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func NewCmdBandwidthPkgCreate() *cobra.Command {
Use: "create",
Short: "Create bandwidth package",
Long: "Create bandwidth package",
Example: "ucloud bw pkg create --eip-id eip-xxx --bandwidth-mb 20 --start-time 2018-12-15/09:20:00 --end-time 2018-12-16/09:20:00",
Example: fmt.Sprintf("%s bw pkg create --eip-id eip-xxx --bandwidth-mb 20 --start-time 2018-12-15/09:20:00 --end-time 2018-12-16/09:20:00", base.BrandNameLower),
Run: func(c *cobra.Command, args []string) {
st, err := time.ParseInLocation(timeLayout, *start, loc)
if err != nil {
Expand Down Expand Up @@ -381,7 +381,7 @@ func NewCmdBandwidthPkgDelete() *cobra.Command {
Use: "delete",
Short: "Delete bandwidth packages",
Long: "Delete bandwidth packages",
Example: "ucloud bw pkg delete --resource-id bwpack-xxx",
Example: fmt.Sprintf("%s bw pkg delete --resource-id bwpack-xxx", base.BrandNameLower),
Run: func(c *cobra.Command, args []string) {
for _, id := range ids {
id := base.PickResourceID(id)
Expand Down
8 changes: 4 additions & 4 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ func NewCmdCompletion() *cobra.Command {
func bashCompletion(cmd *cobra.Command) {
platform := runtime.GOOS
if platform == "darwin" {
fmt.Println(`Please append 'complete -C $(which ucloud) ucloud' to file '~/.bash_profile'`)
fmt.Printf(`Please append 'complete -C $(which %s) %s' to file '~/.bash_profile'`, base.BrandNameLower, base.BrandNameLower)

} else if platform == "linux" {
fmt.Println(`Please append 'complete -C $(which ucloud) ucloud' to file '~/.bashrc'`)
fmt.Printf(`Please append 'complete -C $(which %s) %s' to file '~/.bashrc'`, base.BrandNameLower, base.BrandNameLower)
}
}

func zshCompletion(cmd *cobra.Command) {
fmt.Println(`Please append the following scripts to file '~/.zshrc'.
fmt.Printf(`Please append the following scripts to file '~/.zshrc'.

autoload -U +X bashcompinit && bashcompinit
complete -F $(which ucloud) ucloud`)
complete -F $(which %s) %s`, base.BrandNameLower, base.BrandNameLower)
}

func getBashVersion() (version string, err error) {
Expand Down
Loading