diff --git a/conf/user.go b/conf/user.go index abd624a..fe5d4fd 100644 --- a/conf/user.go +++ b/conf/user.go @@ -129,40 +129,21 @@ func (u *User) Save() bool { 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: // 1. Replace {WD} variable with the actual directory path // 2. Replace ${GOPATH} with enviorment variable GOPATH // 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(w, "${GOPATH}", os.Getenv("GOPATH"), 1) return filepath.FromSlash(w) } -// 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.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 { +// BuildArgs get build args with the specified os. +func (u *User) BuildArgs(os string) []string { var tmp string if os == "windows" { tmp = u.GoBuildArgsForWindows @@ -182,3 +163,22 @@ func (u *User) GetBuildArgs(os string) []string { 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)) +} diff --git a/conf/wide.go b/conf/wide.go index 3f122f2..da5cf27 100644 --- a/conf/wide.go +++ b/conf/wide.go @@ -326,7 +326,7 @@ func checkEnv() { func GetUserWorkspace(username string) string { for _, user := range Users { if user.Name == username { - return user.GetWorkspace() + return user.WorkspacePath() } } @@ -430,7 +430,7 @@ func initWorkspaceDirs() { paths := []string{} for _, user := range Users { - paths = append(paths, filepath.SplitList(user.GetWorkspace())...) + paths = append(paths, filepath.SplitList(user.WorkspacePath())...) } for _, path := range paths { diff --git a/file/files.go b/file/files.go index c553673..d0c2ffb 100644 --- a/file/files.go +++ b/file/files.go @@ -214,7 +214,7 @@ func GetFileHandler(w http.ResponseWriter, r *http.Request) { 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 } diff --git a/main.go b/main.go index 2baa647..3ffcbd2 100644 --- a/main.go +++ b/main.go @@ -110,7 +110,7 @@ func main() { // workspaces for _, user := range conf.Users { 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 diff --git a/output/build.go b/output/build.go index 5a6030e..f4dcbf9 100644 --- a/output/build.go +++ b/output/build.go @@ -96,7 +96,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { goBuildArgs := []string{} goBuildArgs = append(goBuildArgs, "build") - goBuildArgs = append(goBuildArgs, user.GetBuildArgs(runtime.GOOS)...) + goBuildArgs = append(goBuildArgs, user.BuildArgs(runtime.GOOS)...) cmd := exec.Command("go", goBuildArgs...) cmd.Dir = curDir @@ -132,7 +132,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { // display "START [go build]" in front-end browser 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"] = "" + msg + "\n" channelRet["cmd"] = "start-build" diff --git a/output/cross.go b/output/cross.go index d7db419..f6f5ae6 100644 --- a/output/cross.go +++ b/output/cross.go @@ -78,7 +78,7 @@ func CrossCompilationHandler(w http.ResponseWriter, r *http.Request) { user := conf.GetUser(username) goBuildArgs := []string{} goBuildArgs = append(goBuildArgs, "build") - goBuildArgs = append(goBuildArgs, user.GetBuildArgs(goos)...) + goBuildArgs = append(goBuildArgs, user.BuildArgs(goos)...) cmd := exec.Command("go", goBuildArgs...) cmd.Dir = curDir diff --git a/session/users.go b/session/users.go index 78f1fac..7203331 100644 --- a/session/users.go +++ b/session/users.go @@ -410,7 +410,7 @@ func addUser(username, password, email string) string { conf.UpdateCustomizedConf(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)