From b3550cf19b6a041435e68694f21f66413f786057 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Wed, 23 Mar 2016 13:15:27 +0800 Subject: [PATCH 1/2] refactor logger in main.go & add harbor.cfg to .gitignore --- .gitignore | 1 + main.go | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 2f8287303..27cd819fa 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ Deploy/config/registry/config.yml Deploy/config/ui/env Deploy/config/ui/app.conf Deploy/config/db/env +Deploy/harbor.cfg diff --git a/main.go b/main.go index c69c445b1..6b79f066a 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,8 @@ package main import ( "fmt" - "log" + + log "github.com/vmware/harbor/log" _ "github.com/vmware/harbor/auth/db" _ "github.com/vmware/harbor/auth/ldap" @@ -38,28 +39,27 @@ func updateInitPassword(userID int, password string) error { queryUser := models.User{UserID: userID} user, err := dao.GetUser(queryUser) if err != nil { - log.Println("Failed to get user, userID:", userID) - return err + return fmt.Errorf("Failed to get user, userID: %d", userID) } if user == nil { - log.Printf("User id: %d does not exist.", userID) return fmt.Errorf("User id: %d does not exist.", userID) - } else if user.Salt == "" { + } + if user.Salt == "" { salt, err := dao.GenerateRandomString() if err != nil { - log.Printf("Failed to generate salt for encrypting password, %v", err) - return err + return fmt.Errorf("Failed to generate salt for encrypting password, %v", err) } + user.Salt = salt user.Password = password err = dao.ChangeUserPassword(*user) if err != nil { - log.Printf("Failed to update user encrypted password, userID: %d, err: %v", userID, err) - return err + return fmt.Errorf("Failed to update user encrypted password, userID: %d, err: %v", userID, err) } - log.Printf("User id: %d updated its encypted password successfully.", userID) + + log.Infof("User id: %d updated its encypted password successfully.", userID) } else { - log.Printf("User id: %d already has its encrypted password.", userID) + log.Infof("User id: %d already has its encrypted password.", userID) } return nil } @@ -68,6 +68,8 @@ func main() { beego.BConfig.WebConfig.Session.SessionOn = true dao.InitDB() - updateInitPassword(adminUserID, os.Getenv("HARBOR_ADMIN_PASSWORD")) + if err := updateInitPassword(adminUserID, os.Getenv("HARBOR_ADMIN_PASSWORD")); err != nil { + log.Error(err) + } beego.Run() } From 5b0de9a36e7706e6cc1e71b68bdff02c10e5cf1c Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Wed, 23 Mar 2016 13:41:05 +0800 Subject: [PATCH 2/2] mv log to utils --- main.go | 4 ++-- {log => utils/log}/formatter.go | 0 {log => utils/log}/level.go | 0 {log => utils/log}/logger.go | 0 {log => utils/log}/record.go | 0 {log => utils/log}/textformatter.go | 0 6 files changed, 2 insertions(+), 2 deletions(-) rename {log => utils/log}/formatter.go (100%) rename {log => utils/log}/level.go (100%) rename {log => utils/log}/logger.go (100%) rename {log => utils/log}/record.go (100%) rename {log => utils/log}/textformatter.go (100%) diff --git a/main.go b/main.go index 6b79f066a..60d36bfd1 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ package main import ( "fmt" - log "github.com/vmware/harbor/log" + log "github.com/vmware/harbor/utils/log" _ "github.com/vmware/harbor/auth/db" _ "github.com/vmware/harbor/auth/ldap" @@ -39,7 +39,7 @@ func updateInitPassword(userID int, password string) error { queryUser := models.User{UserID: userID} user, err := dao.GetUser(queryUser) if err != nil { - return fmt.Errorf("Failed to get user, userID: %d", userID) + return fmt.Errorf("Failed to get user, userID: %d %v", userID, err) } if user == nil { return fmt.Errorf("User id: %d does not exist.", userID) diff --git a/log/formatter.go b/utils/log/formatter.go similarity index 100% rename from log/formatter.go rename to utils/log/formatter.go diff --git a/log/level.go b/utils/log/level.go similarity index 100% rename from log/level.go rename to utils/log/level.go diff --git a/log/logger.go b/utils/log/logger.go similarity index 100% rename from log/logger.go rename to utils/log/logger.go diff --git a/log/record.go b/utils/log/record.go similarity index 100% rename from log/record.go rename to utils/log/record.go diff --git a/log/textformatter.go b/utils/log/textformatter.go similarity index 100% rename from log/textformatter.go rename to utils/log/textformatter.go