🎨 Fix #321 Rename user's getters
This commit is contained in:
parent
4893c974bc
commit
65e65e2d8a
46
conf/user.go
46
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))
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
2
main.go
2
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
|
||||
|
|
|
@ -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"] = "<span class='start-build'>" + msg + "</span>\n"
|
||||
channelRet["cmd"] = "start-build"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue