🎨 Fix #321 Rename user's getters

This commit is contained in:
Liang Ding 2017-05-05 13:35:09 +08:00
parent 4893c974bc
commit 65e65e2d8a
7 changed files with 31 additions and 31 deletions

View File

@ -129,40 +129,21 @@ func (u *User) Save() bool {
return true return true
} }
// GetWorkspace gets workspace path of the user. // WorkspacePath gets workspace path of the user.
// //
// Compared to the use of Wide.Workspace, this function will be processed as follows: // Compared to the use of Wide.Workspace, this function will be processed as follows:
// 1. Replace {WD} variable with the actual directory path // 1. Replace {WD} variable with the actual directory path
// 2. Replace ${GOPATH} with enviorment variable GOPATH // 2. Replace ${GOPATH} with enviorment variable GOPATH
// 3. Replace "/" with "\\" (Windows) // 3. Replace "/" with "\\" (Windows)
func (u *User) GetWorkspace() string { func (u *User) WorkspacePath() string {
w := strings.Replace(u.Workspace, "{WD}", Wide.WD, 1) w := strings.Replace(u.Workspace, "{WD}", Wide.WD, 1)
w = strings.Replace(w, "${GOPATH}", os.Getenv("GOPATH"), 1) w = strings.Replace(w, "${GOPATH}", os.Getenv("GOPATH"), 1)
return filepath.FromSlash(w) return filepath.FromSlash(w)
} }
// GetOwner gets the user the specified path belongs to. Returns "" if not found. // BuildArgs get build args with the specified os.
func GetOwner(path string) string { func (u *User) BuildArgs(os string) []string {
for _, user := range Users {
if strings.HasPrefix(path, user.GetWorkspace()) {
return user.Name
}
}
return ""
}
// Salt salts the specified password with the specified salt.
func Salt(password, salt string) string {
sha1hash := sha1.New()
sha1hash.Write([]byte(password + salt))
return hex.EncodeToString(sha1hash.Sum(nil))
}
// GetBuildArgs get build args with the specified os.
func (u *User) GetBuildArgs(os string) []string {
var tmp string var tmp string
if os == "windows" { if os == "windows" {
tmp = u.GoBuildArgsForWindows tmp = u.GoBuildArgsForWindows
@ -182,3 +163,22 @@ func (u *User) GetBuildArgs(os string) []string {
return ret return ret
} }
// GetOwner gets the user the specified path belongs to. Returns "" if not found.
func GetOwner(path string) string {
for _, user := range Users {
if strings.HasPrefix(path, user.WorkspacePath()) {
return user.Name
}
}
return ""
}
// Salt salts the specified password with the specified salt.
func Salt(password, salt string) string {
sha1hash := sha1.New()
sha1hash.Write([]byte(password + salt))
return hex.EncodeToString(sha1hash.Sum(nil))
}

View File

@ -326,7 +326,7 @@ func checkEnv() {
func GetUserWorkspace(username string) string { func GetUserWorkspace(username string) string {
for _, user := range Users { for _, user := range Users {
if user.Name == username { if user.Name == username {
return user.GetWorkspace() return user.WorkspacePath()
} }
} }
@ -430,7 +430,7 @@ func initWorkspaceDirs() {
paths := []string{} paths := []string{}
for _, user := range Users { for _, user := range Users {
paths = append(paths, filepath.SplitList(user.GetWorkspace())...) paths = append(paths, filepath.SplitList(user.WorkspacePath())...)
} }
for _, path := range paths { for _, path := range paths {

View File

@ -214,7 +214,7 @@ func GetFileHandler(w http.ResponseWriter, r *http.Request) {
user := conf.GetUser(username) user := conf.GetUser(username)
data["path"] = "/workspace/" + user.Name + "/" + strings.Replace(path, user.GetWorkspace(), "", 1) data["path"] = "/workspace/" + user.Name + "/" + strings.Replace(path, user.WorkspacePath(), "", 1)
return return
} }

View File

@ -110,7 +110,7 @@ func main() {
// workspaces // workspaces
for _, user := range conf.Users { for _, user := range conf.Users {
http.Handle(conf.Wide.Context+"/workspace/"+user.Name+"/", http.Handle(conf.Wide.Context+"/workspace/"+user.Name+"/",
http.StripPrefix(conf.Wide.Context+"/workspace/"+user.Name+"/", http.FileServer(http.Dir(user.GetWorkspace())))) http.StripPrefix(conf.Wide.Context+"/workspace/"+user.Name+"/", http.FileServer(http.Dir(user.WorkspacePath()))))
} }
// session // session

View File

@ -96,7 +96,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
goBuildArgs := []string{} goBuildArgs := []string{}
goBuildArgs = append(goBuildArgs, "build") goBuildArgs = append(goBuildArgs, "build")
goBuildArgs = append(goBuildArgs, user.GetBuildArgs(runtime.GOOS)...) goBuildArgs = append(goBuildArgs, user.BuildArgs(runtime.GOOS)...)
cmd := exec.Command("go", goBuildArgs...) cmd := exec.Command("go", goBuildArgs...)
cmd.Dir = curDir cmd.Dir = curDir
@ -132,7 +132,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
// display "START [go build]" in front-end browser // display "START [go build]" in front-end browser
msg := i18n.Get(locale, "start-build").(string) msg := i18n.Get(locale, "start-build").(string)
msg = strings.Replace(msg, "build]", "build "+fmt.Sprint(user.GetBuildArgs(runtime.GOOS))+"]", 1) msg = strings.Replace(msg, "build]", "build "+fmt.Sprint(user.BuildArgs(runtime.GOOS))+"]", 1)
channelRet["output"] = "<span class='start-build'>" + msg + "</span>\n" channelRet["output"] = "<span class='start-build'>" + msg + "</span>\n"
channelRet["cmd"] = "start-build" channelRet["cmd"] = "start-build"

View File

@ -78,7 +78,7 @@ func CrossCompilationHandler(w http.ResponseWriter, r *http.Request) {
user := conf.GetUser(username) user := conf.GetUser(username)
goBuildArgs := []string{} goBuildArgs := []string{}
goBuildArgs = append(goBuildArgs, "build") goBuildArgs = append(goBuildArgs, "build")
goBuildArgs = append(goBuildArgs, user.GetBuildArgs(goos)...) goBuildArgs = append(goBuildArgs, user.BuildArgs(goos)...)
cmd := exec.Command("go", goBuildArgs...) cmd := exec.Command("go", goBuildArgs...)
cmd.Dir = curDir cmd.Dir = curDir

View File

@ -410,7 +410,7 @@ func addUser(username, password, email string) string {
conf.UpdateCustomizedConf(username) conf.UpdateCustomizedConf(username)
http.Handle("/workspace/"+username+"/", http.Handle("/workspace/"+username+"/",
http.StripPrefix("/workspace/"+username+"/", http.FileServer(http.Dir(newUser.GetWorkspace())))) http.StripPrefix("/workspace/"+username+"/", http.FileServer(http.Dir(newUser.WorkspacePath()))))
logger.Infof("Created a user [%s]", username) logger.Infof("Created a user [%s]", username)