-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: Fix the problem of abnormal acquisition of user-info #8902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,7 +99,7 @@ func username() { | |
| return | ||
| } | ||
|
|
||
| db, err := loadDBConn() | ||
| db, err := loadDBConn("core.db") | ||
| if err != nil { | ||
| fmt.Println(i18n.GetMsgWithMapForCmd("DBConnErr", map[string]interface{}{"err": err.Error()})) | ||
| return | ||
|
|
@@ -131,7 +131,7 @@ func password() { | |
| fmt.Println("\n" + i18n.GetMsgByKeyForCmd("UpdateUPasswordBlank")) | ||
| return | ||
| } | ||
| db, err := loadDBConn() | ||
| db, err := loadDBConn("core.db") | ||
| if err != nil { | ||
| fmt.Println("\n" + i18n.GetMsgWithMapForCmd("DBConnErr", map[string]interface{}{"err": err.Error()})) | ||
| return | ||
|
|
@@ -196,7 +196,7 @@ func port() { | |
| fmt.Println(i18n.GetMsgByKeyForCmd("UpdatePortUsed")) | ||
| return | ||
| } | ||
| db, err := loadDBConn() | ||
| db, err := loadDBConn("core.db") | ||
| if err != nil { | ||
| fmt.Println(i18n.GetMsgWithMapForCmd("DBConnErr", map[string]interface{}{"err": err.Error()})) | ||
| return | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided patch is mostly correct, but there are a few areas that could be improved:
Here's an optimized version of the code: func setupDatabaseConnection(dbPath string) (*db.DB, error) {
db, err := db.NewSQLClient(dbPath)
if err != nil {
return nil, fmt.Errorf(i18n.GetMsgWithMapForCmd("FailedToLoadConfigFile", map[string]interface{}{
"file": "core.db",
"error": err.Error(),
}))
}
return db, nil
}
func username() {
// Some other logic...
- db, err := loadDBConn()
+ connErrPrefix := "Failed to establish a database connection"
+ db, connErr := setupDatabaseConnection("core.db")
// Error handling...
}
func password() {
// Similar logic...
- db, err := loadDBConn()
+ db, connErr := setupDatabaseConnection("core.db")Explanation:
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,10 +22,14 @@ var userinfoCmd = &cobra.Command{ | |
| fmt.Println(i18n.GetMsgWithMapForCmd("SudoHelper", map[string]interface{}{"cmd": "sudo 1pctl user-info"})) | ||
| return nil | ||
| } | ||
| db, err := loadDBConn() | ||
| db, err := loadDBConn("core.db") | ||
| if err != nil { | ||
| return fmt.Errorf("init my db conn failed, err: %v \n", err) | ||
| } | ||
| agentDB, err := loadDBConn("agent.db") | ||
| if err != nil { | ||
| return fmt.Errorf("init my agent db conn failed, err: %v \n", err) | ||
| } | ||
| user := getSettingByKey(db, "UserName") | ||
| pass := "********" | ||
| if isDefault(db) { | ||
|
|
@@ -39,7 +43,7 @@ var userinfoCmd = &cobra.Command{ | |
| port := getSettingByKey(db, "ServerPort") | ||
| ssl := getSettingByKey(db, "SSL") | ||
| entrance := getSettingByKey(db, "SecurityEntrance") | ||
| address := getSettingByKey(db, "SystemIP") | ||
| address := getSettingByKey(agentDB, "SystemIP") | ||
|
|
||
| protocol := "http" | ||
| if ssl == constant.StatusEnable { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code Differences CheckThe provided
Potential Issues and Improvements
Here’s a suggested refactored version incorporating some of these improvements with minimal changes: func LoadSettingsAndConnections(ctx context.Context, cmd *cobra.Command) (*gorm.DB, *gorm.DB, error) {
var agentDbConfig struct {
DBName string `mapstructure:"name"`
}
err := config.UnmarshalKey(config.KeyAgentDB, &agentDbConfig)
if err != nil {
return nil, nil, fmt.Errorf("unmarshaling agent DB config: %v", err)
}
customDBOptions, ok := ctx.Value(context.CTXKeyDB).(map[string]string)
if !ok {
return nil, nil, errors.New("DB options not found in context")
}
db, err := openGormDatabases(customDBOptions["core"], customDBOptions["agent"])
if err != nil {
return nil, nil, fmt.Errorf("open databases failed: %v", err)
}
db.SetLogger(sql.LoggerAdapter(logrus.StandardLogger()))
agentDB.SetLogger(sql.LoggerAdapter(logrus.StandardLogger()))
// Retrieve and assign values to settings here...
return db, agentDB, nil
}This approach consolidates the database opening process and makes error handling uniform across functions, improving overall maintainability and robustness. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no significant issue with the provided code. The changes made to add
"core.db"as an argument toloadDBConn()do not affect the functionality or logic of the commands. Each command initializes a new database connection usingloadDBConn(), which takes a string representing the database filename as an optional parameter, defaulting to"db.sqlite".The addition of
"core.db"ensures clarity about where the core configuration file might be located relative to other databases used by the application. This change does not introduce any performance overhead since loading multiple databases would likely lead to caching and efficient access.Therefore, the existing code is correct and can remain unchanged unless further context suggests it should handle different scenarios based on these filenames differently.