Skip to content

Commit 70b3685

Browse files
author
YangSen-qn
committed
optimize get user
1 parent 9531234 commit 70b3685

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

iqshell/common/account/actions.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func SaveToDB(acc Account, accountOver bool) (err *data.CodeError) {
9797
}
9898

9999
func getAccount(pt string) (account Account, err *data.CodeError) {
100-
accountFh, openErr := os.Open(pt)
100+
accountFh, openErr := os.OpenFile(info.AccountPath, os.O_RDWR, 0600)
101101
if openErr != nil {
102102
err = data.NewEmptyError().AppendDescF("Get account error, %s, please use `account` to set Id and SecretKey first", openErr)
103103
return
@@ -121,16 +121,43 @@ func getAccount(pt string) (account Account, err *data.CodeError) {
121121
return
122122
}
123123

124+
oldError := data.NewEmptyError().AppendDescF("Decrypt account bytes: %s, you can delete account file(%s) and use `account` command to reset the account", dErr, pt)
124125
if len(acc.Name) == 0 {
125-
return account, data.NewEmptyError().AppendDescF("Decrypt account bytes: %s, you can delete account file(%s) and use `account` command to reset the account", dErr, pt)
126+
return account, oldError
126127
}
127128

128-
accs, lErr := LookUp(acc.Name)
129-
if lErr != nil || len(accs) == 0 {
130-
return account, data.NewEmptyError().AppendDescF("Decrypt account bytes: %s, you can delete account file(%s) and use `account` command to reset the account", dErr, pt)
129+
accounts, lErr := LookUp(acc.Name)
130+
if lErr != nil || len(accounts) == 0 {
131+
return account, oldError
131132
}
132133

133-
return accs[0], nil
134+
account = accounts[0]
135+
136+
// 尝试替换错误缓存,失败也没关系
137+
jsonStr, mErr := account.value()
138+
if mErr != nil {
139+
log.WarningF("get account, get json error:%s", mErr)
140+
return account, nil
141+
}
142+
143+
_, sErr := accountFh.Seek(0, io.SeekStart)
144+
if sErr != nil {
145+
log.WarningF("get account, file seek error:%s", sErr)
146+
return account, nil
147+
}
148+
149+
tErr := accountFh.Truncate(0)
150+
if tErr != nil {
151+
log.WarningF("get account, file truncate error:%s", tErr)
152+
return account, nil
153+
}
154+
155+
_, wErr := accountFh.WriteString(jsonStr)
156+
if wErr != nil {
157+
log.WarningF("get account, file write error:%s", wErr)
158+
}
159+
160+
return account, nil
134161
}
135162

136163
// qshell 会记录当前的user信息,当切换账户后, 老的账户信息会记录下来

0 commit comments

Comments
 (0)