wide/conf/wide.go

109 lines
2.4 KiB
Go
Raw Normal View History

2014-09-05 10:33:43 +04:00
// Wide 配置相关,所有配置(包括用户配置)都是保存在 wide.json 中.
2014-08-18 17:45:43 +04:00
package conf
import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
2014-09-08 07:37:34 +04:00
_ "github.com/b3log/wide/i18n"
"github.com/b3log/wide/util"
"github.com/golang/glog"
2014-08-18 17:45:43 +04:00
)
2014-08-31 14:50:38 +04:00
type User struct {
2014-09-13 12:50:18 +04:00
Name string
Password string
Workspace string // 指定了该用户的 GOPATH 路径
2014-08-31 09:31:26 +04:00
}
2014-08-23 19:07:24 +04:00
type conf struct {
2014-08-18 17:45:43 +04:00
Server string
StaticServer string
EditorChannel string
OutputChannel string
ShellChannel string
StaticResourceVersion string
2014-09-02 18:57:30 +04:00
MaxProcs int
2014-08-18 17:45:43 +04:00
RuntimeMode string
2014-09-13 12:50:18 +04:00
Pwd string
2014-08-31 14:50:38 +04:00
Users []User
2014-08-18 17:45:43 +04:00
}
2014-08-23 19:07:24 +04:00
var Wide conf
2014-08-31 14:50:38 +04:00
var rawWide conf
2014-08-18 17:45:43 +04:00
2014-09-13 12:50:18 +04:00
// 获取 username 指定的用户的工作空间路径.
2014-09-05 07:24:53 +04:00
func (this *conf) GetUserWorkspace(username string) string {
2014-09-13 12:50:18 +04:00
for _, user := range Wide.Users {
if user.Name == username {
ret := strings.Replace(user.Workspace, "{Pwd}", Wide.Pwd, 1)
return filepath.FromSlash(ret)
}
}
return ""
2014-09-05 07:24:53 +04:00
}
2014-08-31 14:50:38 +04:00
func Save() bool {
2014-09-13 12:50:18 +04:00
// 只有 Users 是可以通过界面修改的,其他属性只能手工维护 wide.json 配置文件
2014-08-31 14:50:38 +04:00
rawWide.Users = Wide.Users
// 原始配置文件内容
bytes, err := json.MarshalIndent(rawWide, "", " ")
if nil != err {
glog.Error(err)
return false
}
if err = ioutil.WriteFile("conf/wide.json", bytes, 0644); nil != err {
glog.Error(err)
return false
}
return true
}
func Load() {
2014-08-18 17:45:43 +04:00
bytes, _ := ioutil.ReadFile("conf/wide.json")
err := json.Unmarshal(bytes, &Wide)
if err != nil {
glog.Error(err)
os.Exit(-1)
}
2014-08-31 14:50:38 +04:00
// 保存未经变量替换处理的原始配置文件,用于写回时
json.Unmarshal(bytes, &rawWide)
2014-08-18 17:45:43 +04:00
ip, err := util.Net.LocalIP()
if err != nil {
glog.Error(err)
os.Exit(-1)
}
2014-09-08 07:37:34 +04:00
glog.V(3).Infof("IP [%s]", ip)
2014-08-18 17:45:43 +04:00
Wide.Server = strings.Replace(Wide.Server, "{IP}", ip, 1)
Wide.StaticServer = strings.Replace(Wide.StaticServer, "{IP}", ip, 1)
Wide.EditorChannel = strings.Replace(Wide.EditorChannel, "{IP}", ip, 1)
Wide.OutputChannel = strings.Replace(Wide.OutputChannel, "{IP}", ip, 1)
Wide.ShellChannel = strings.Replace(Wide.ShellChannel, "{IP}", ip, 1)
2014-08-28 06:51:03 +04:00
// 获取当前执行路径
2014-08-18 17:45:43 +04:00
file, _ := exec.LookPath(os.Args[0])
pwd, _ := filepath.Abs(file)
pwd = pwd[:strings.LastIndex(pwd, string(os.PathSeparator))]
2014-09-13 12:50:18 +04:00
Wide.Pwd = pwd
2014-09-08 07:37:34 +04:00
glog.V(3).Infof("pwd [%s]", pwd)
2014-08-28 06:51:03 +04:00
2014-09-08 07:37:34 +04:00
glog.V(3).Info("Conf: \n" + string(bytes))
2014-08-18 17:45:43 +04:00
}