@@ -36,7 +36,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
3636const DefaultProfile = "default"
3737
3838//Version 版本号
39- const Version = "0.1.24 "
39+ const Version = "0.1.25 "
4040
4141//ConfigIns 配置实例, 程序加载时生成
4242var ConfigIns = & AggConfig {
@@ -79,14 +79,15 @@ type GlobalFlag struct {
7979
8080//CLIConfig cli_config element
8181type CLIConfig struct {
82- ProjectID string `json:"project_id"`
83- Region string `json:"region"`
84- Zone string `json:"zone"`
85- BaseURL string `json:"base_url"`
86- Timeout int `json:"timeout_sec"`
87- Profile string `json:"profile"`
88- Active bool `json:"active"` //是否生效
89- MaxRetryTimes * int `json:"max_retry_times"`
82+ ProjectID string `json:"project_id"`
83+ Region string `json:"region"`
84+ Zone string `json:"zone"`
85+ BaseURL string `json:"base_url"`
86+ Timeout int `json:"timeout_sec"`
87+ Profile string `json:"profile"`
88+ Active bool `json:"active"` //是否生效
89+ MaxRetryTimes * int `json:"max_retry_times"`
90+ AgreeUploadLog bool `json:"agree_upload_log"`
9091}
9192
9293//CredentialConfig credential element
@@ -98,16 +99,17 @@ type CredentialConfig struct {
9899
99100//AggConfig 聚合配置 config+credential
100101type AggConfig struct {
101- Profile string `json:"profile"`
102- Active bool `json:"active"`
103- ProjectID string `json:"project_id"`
104- Region string `json:"region"`
105- Zone string `json:"zone"`
106- BaseURL string `json:"base_url"`
107- Timeout int `json:"timeout_sec"`
108- PublicKey string `json:"public_key"`
109- PrivateKey string `json:"private_key"`
110- MaxRetryTimes * int `json:"max_retry_times"`
102+ Profile string `json:"profile"`
103+ Active bool `json:"active"`
104+ ProjectID string `json:"project_id"`
105+ Region string `json:"region"`
106+ Zone string `json:"zone"`
107+ BaseURL string `json:"base_url"`
108+ Timeout int `json:"timeout_sec"`
109+ PublicKey string `json:"public_key"`
110+ PrivateKey string `json:"private_key"`
111+ MaxRetryTimes * int `json:"max_retry_times"`
112+ AgreeUploadLog bool `json:"agree_upload_log"`
111113}
112114
113115//ConfigPublicKey 输入公钥
@@ -136,6 +138,22 @@ func (p *AggConfig) ConfigPrivateKey() error {
136138 return nil
137139}
138140
141+ //ConfigUploadLog agree upload log or not
142+ func (p * AggConfig ) ConfigUploadLog () error {
143+ 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):" )
145+ _ , err := fmt .Scanf ("%s\n " , & input )
146+ if err != nil {
147+ HandleError (err )
148+ return err
149+ }
150+
151+ if str := strings .ToLower (input ); str == "y" || str == "ye" || str == "yes" {
152+ p .AgreeUploadLog = true
153+ }
154+ return nil
155+ }
156+
139157//GetClientConfig 用来生成sdkClient
140158func (p * AggConfig ) GetClientConfig (isDebug bool ) * sdk.Config {
141159 clientConfig := & sdk.Config {
@@ -169,6 +187,7 @@ func (p *AggConfig) copyToCLIConfig(target *CLIConfig) {
169187 target .Zone = p .Zone
170188 target .Active = p .Active
171189 target .MaxRetryTimes = p .MaxRetryTimes
190+ target .AgreeUploadLog = p .AgreeUploadLog
172191}
173192
174193func (p * AggConfig ) copyToCredentialConfig (target * CredentialConfig ) {
@@ -217,7 +236,7 @@ func NewAggConfigManager(cfgFile, credFile *os.File) (*AggConfigManager, error)
217236//Append config to list, override if already exist the same profile
218237func (p * AggConfigManager ) Append (config * AggConfig ) error {
219238 if _ , ok := p .configs [config .Profile ]; ok {
220- return fmt .Errorf ("profile %s exists already" , config .Profile )
239+ return fmt .Errorf ("profile [%s] exists already" , config .Profile )
221240 }
222241
223242 if config .Active && config .Profile != p .activeProfile {
@@ -279,16 +298,17 @@ func (p *AggConfigManager) Load() error {
279298 }
280299
281300 p .configs [profile ] = & AggConfig {
282- PrivateKey : cred .PrivateKey ,
283- PublicKey : cred .PublicKey ,
284- Profile : config .Profile ,
285- ProjectID : config .ProjectID ,
286- Region : config .Region ,
287- Zone : config .Zone ,
288- BaseURL : config .BaseURL ,
289- Timeout : config .Timeout ,
290- Active : config .Active ,
291- MaxRetryTimes : config .MaxRetryTimes ,
301+ PrivateKey : cred .PrivateKey ,
302+ PublicKey : cred .PublicKey ,
303+ Profile : config .Profile ,
304+ ProjectID : config .ProjectID ,
305+ Region : config .Region ,
306+ Zone : config .Zone ,
307+ BaseURL : config .BaseURL ,
308+ Timeout : config .Timeout ,
309+ Active : config .Active ,
310+ MaxRetryTimes : config .MaxRetryTimes ,
311+ AgreeUploadLog : config .AgreeUploadLog ,
292312 }
293313 }
294314
@@ -465,6 +485,37 @@ func LoadUserInfo() (*uaccount.UserInfo, error) {
465485 return & user , nil
466486}
467487
488+ //GetUserInfo from local file and remote api
489+ func GetUserInfo () (* uaccount.UserInfo , error ) {
490+ user , err := LoadUserInfo ()
491+ if err == nil {
492+ return user , nil
493+ }
494+
495+ req := BizClient .NewGetUserInfoRequest ()
496+ resp , err := BizClient .GetUserInfo (req )
497+
498+ if err != nil {
499+ return nil , err
500+ }
501+
502+ if len (resp .DataSet ) == 1 {
503+ user = & resp .DataSet [0 ]
504+ bytes , err := json .Marshal (user )
505+ if err != nil {
506+ return nil , err
507+ }
508+ fileFullPath := GetConfigDir () + "/user.json"
509+ err = ioutil .WriteFile (fileFullPath , bytes , 0600 )
510+ if err != nil {
511+ return nil , err
512+ }
513+ } else {
514+ return nil , fmt .Errorf ("GetUserInfo DataSet length: %d" , len (resp .DataSet ))
515+ }
516+ return user , nil
517+ }
518+
468519//OldConfig 0.1.7以及之前版本的配置struct
469520type OldConfig struct {
470521 PublicKey string `json:"public_key"`
@@ -585,18 +636,15 @@ func InitConfig() {
585636 HandleError (err )
586637 }
587638 } else {
588- var ok bool
589- ins , ok = AggConfigListIns .GetAggConfigByProfile (Global .Profile )
590- if ! ok {
591- LogError ("Profile %s does not exist" , Global .Profile )
592- }
639+ ins , _ = AggConfigListIns .GetAggConfigByProfile (Global .Profile )
593640 }
594641
595642 if ins != nil {
596643 ConfigIns = ins
597644 } else {
598645 ins = ConfigIns
599646 }
647+ logCmd ()
600648
601649 bc , err := GetBizClient (ConfigIns )
602650 if err != nil {
0 commit comments