Skip to content

Commit 6ba9b43

Browse files
committed
adjust ak update
1 parent f265142 commit 6ba9b43

File tree

2 files changed

+94
-117
lines changed

2 files changed

+94
-117
lines changed

ots/otsutil.go

Lines changed: 66 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ func (c *Context) Run(args []string) error {
132132
return nil
133133
}
134134

135-
// InitializeAndValidatePlatform 初始化基础信息并验证平台支持
136135
func (c *Context) InitializeAndValidatePlatform() error {
137136
c.InitBasicInfo()
138137
c.CheckOsTypeAndArch()
@@ -155,7 +154,7 @@ func (c *Context) ExecuteTablestoreCli(args []string) error {
155154
cmd.Stderr = c.originCtx.Stderr()
156155
cmd.Stdin = os.Stdin
157156
// 设置工作目录为 aliyun 配置目录,以便 tablestore CLI 能找到 .tablestore_config 文件
158-
cmd.Dir = c.configPath
157+
// cmd.Dir = c.configPath
159158

160159
err := cmd.Run()
161160
if err != nil {
@@ -168,13 +167,11 @@ func (c *Context) ExecuteTablestoreCli(args []string) error {
168167
// 如果未安装则安装,如果已安装则检查并更新到最新版本
169168
func (c *Context) EnsureInstalledAndUpdated() error {
170169
if !c.installed {
171-
// 未安装时,设置版本并安装
172170
c.versionRemote = currentVersion
173171
err := c.Install()
174172
if err != nil {
175173
return err
176174
}
177-
// 更新版本检查时间
178175
err = c.UpdateCheckCacheTime()
179176
if err != nil {
180177
return err
@@ -208,10 +205,8 @@ func (c *Context) CheckOsTypeAndArch() {
208205
c.osType = runtimeGOOSFunc()
209206
c.osArch = runtimeGOARCHFunc()
210207

211-
// 构建平台标识符
212208
platformKey := c.osType + "-" + c.osArch
213209

214-
// 检查是否支持该平台
215210
if _, exists := platformPaths[platformKey]; exists {
216211
c.osSupport = true
217212
c.downloadPathSuffix = platformKey
@@ -414,6 +409,8 @@ type tablestoreConfig struct {
414409
AccessKeySecret string `json:"AccessKeySecret"`
415410
AccessKeySecretToken string `json:"AccessKeySecretToken,omitempty"`
416411
Instance string `json:"Instance,omitempty"`
412+
RegionId string `json:"RegionId,omitempty"`
413+
ProfileMode string `json:"ProfileMode,omitempty"`
417414
}
418415

419416
// PrepareEnv 准备用户身份环境变量并创建配置文件
@@ -441,27 +438,7 @@ func (c *Context) PrepareEnv() error {
441438
if profile.StsToken != "" {
442439
stsToken = profile.StsToken
443440
}
444-
case config.EcsRamRole, config.CredentialsURI, config.OIDC:
445-
// 这些模式需要先获取临时凭证
446-
proxyHost, ok := c.originCtx.Flags().GetValue("proxy-host")
447-
if !ok {
448-
proxyHost = ""
449-
}
450-
credential, err := profile.GetCredential(c.originCtx, tea.String(proxyHost))
451-
if err != nil {
452-
return fmt.Errorf("can't get credential %s", err)
453-
}
454-
model, err := credential.GetCredential()
455-
if err != nil {
456-
return fmt.Errorf("can't get credential %s", err)
457-
}
458-
accessKeyId = *model.AccessKeyId
459-
accessKeySecret = *model.AccessKeySecret
460-
if model.SecurityToken != nil {
461-
stsToken = *model.SecurityToken
462-
}
463441
default:
464-
// 其他模式,获取临时凭证
465442
proxyHost, ok := c.originCtx.Flags().GetValue("proxy-host")
466443
if !ok {
467444
proxyHost = ""
@@ -481,75 +458,92 @@ func (c *Context) PrepareEnv() error {
481458
}
482459
}
483460

484-
// 创建 tablestore CLI 配置文件
461+
if accessKeyId == "" || accessKeySecret == "" {
462+
return fmt.Errorf("access key id or access key secret is empty, please run `aliyun configure` first")
463+
}
464+
485465
tsConfig := tablestoreConfig{
486-
Endpoint: "", // 将从命令行参数获取
466+
Endpoint: "",
487467
AccessKeyId: accessKeyId,
488468
AccessKeySecret: accessKeySecret,
489469
AccessKeySecretToken: stsToken,
490-
Instance: "", // 将从命令行参数获取
470+
Instance: "",
471+
}
472+
473+
wd, _ := os.Getwd()
474+
configPath := filepath.Join(wd, ".tablestore_config")
475+
476+
if fileExists(configPath) {
477+
data, err := os.ReadFile(configPath)
478+
if err != nil {
479+
return fmt.Errorf("failed to read config file: %v", err)
480+
}
481+
var existing tablestoreConfig
482+
if err := json.Unmarshal(data, &existing); err != nil {
483+
return fmt.Errorf("failed to unmarshal existing config: %v", err)
484+
}
485+
existing.AccessKeyId = accessKeyId
486+
existing.AccessKeySecret = accessKeySecret
487+
existing.AccessKeySecretToken = stsToken
488+
updatedJSON, err := json.MarshalIndent(existing, "", " ")
489+
if err != nil {
490+
return fmt.Errorf("failed to marshal updated config: %v", err)
491+
}
492+
if err := os.WriteFile(configPath, updatedJSON, 0600); err != nil {
493+
return fmt.Errorf("failed to write updated config file: %v", err)
494+
}
495+
return nil
491496
}
492497

493-
// 将配置写入 aliyun 配置目录下的 .tablestore_config 文件
494-
// tablestore CLI 会从其工作目录读取配置
495-
configPath := filepath.Join(c.configPath, ".tablestore_config")
496498
configJSON, err := json.MarshalIndent(tsConfig, "", " ")
497499
if err != nil {
498500
return fmt.Errorf("failed to marshal config: %v", err)
499501
}
500-
501-
err = os.WriteFile(configPath, configJSON, 0600)
502-
if err != nil {
502+
if err := os.WriteFile(configPath, configJSON, 0600); err != nil {
503503
return fmt.Errorf("failed to write config file: %v", err)
504504
}
505505

506-
// 初始化并设置环境变量 map
507-
if c.envMap == nil {
508-
c.envMap = make(map[string]string)
509-
}
510-
c.envMap["ALIBABA_CLOUD_ACCESS_KEY_ID"] = accessKeyId
511-
c.envMap["ALIBABA_CLOUD_ACCESS_KEY_SECRET"] = accessKeySecret
512-
if stsToken != "" {
513-
c.envMap["ALIBABA_CLOUD_SECURITY_TOKEN"] = stsToken
514-
}
515-
516506
return nil
517507
}
518508

519-
// RemoveFlagsForMainCli 移除主程序使用的 flag,避免传递给 otsutil 出错
520509
func (c *Context) RemoveFlagsForMainCli(args []string) ([]string, error) {
521-
argsNew := make([]string, len(args))
522-
copy(argsNew, args)
523-
if c.originCtx.Flags() != nil && c.originCtx.Flags().Flags() != nil {
524-
for _, f := range c.originCtx.Flags().Flags() {
525-
if f.IsAssigned() && f.Category == "config" {
526-
for i, arg := range argsNew {
527-
if arg == "--"+f.Name || (f.Shorthand != 0 && arg == "-"+string(f.Shorthand)) {
528-
argsNew = append(argsNew[:i], argsNew[i+1:]...)
529-
if i < len(argsNew) {
530-
if f.AssignedMode != cli.AssignedNone {
531-
argsNew = append(argsNew[:i], argsNew[i+1:]...)
532-
}
533-
}
534-
break
535-
}
536-
}
537-
}
510+
if c.originCtx.Flags() == nil || c.originCtx.Flags().Flags() == nil {
511+
return append([]string(nil), args...), nil
512+
}
513+
longNeedsValue := make(map[string]bool) // key: --name
514+
shortNeedsValue := make(map[string]bool) // key: -x
515+
for _, f := range c.originCtx.Flags().Flags() {
516+
if !f.IsAssigned() || f.Category != "config" {
517+
continue
518+
}
519+
needsValue := f.AssignedMode != cli.AssignedNone
520+
if f.Name != "" {
521+
longNeedsValue["--"+f.Name] = needsValue
522+
}
523+
if f.Shorthand != 0 {
524+
shortNeedsValue["-"+string(f.Shorthand)] = needsValue
538525
}
539526
}
540-
if c.defaultLanguage != "" {
541-
languageFlagExists := false
542-
for _, arg := range argsNew {
543-
if arg == "--language" {
544-
languageFlagExists = true
545-
break
527+
528+
// single pass: copy args we want to keep
529+
out := make([]string, 0, len(args))
530+
for i := 0; i < len(args); i++ {
531+
a := args[i]
532+
if needs, ok := longNeedsValue[a]; ok {
533+
if needs && i+1 < len(args) { // skip value
534+
i++
546535
}
536+
continue
547537
}
548-
if !languageFlagExists {
549-
argsNew = append(argsNew, "--language", c.defaultLanguage)
538+
if needs, ok := shortNeedsValue[a]; ok {
539+
if needs && i+1 < len(args) { // skip value
540+
i++
541+
}
542+
continue
550543
}
544+
out = append(out, a)
551545
}
552-
return argsNew, nil
546+
return out, nil
553547
}
554548

555549
func fileExists(path string) bool {

0 commit comments

Comments
 (0)