121 lines
2.4 KiB
Go
121 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
type AppConf struct {
|
|
Path string `json:"Path"`
|
|
LastWorkspace string `json:"lastWorkspace"`
|
|
}
|
|
|
|
var appConf *AppConf
|
|
|
|
func init() {
|
|
readConfig()
|
|
}
|
|
|
|
func readConfig() {
|
|
appConf = &AppConf{}
|
|
_, err := os.Stat("conf.json")
|
|
if os.IsNotExist(err) {
|
|
fmt.Println("创建conf.json")
|
|
file, err := os.Create("conf.json")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer file.Close()
|
|
}
|
|
file, err := os.ReadFile("conf.json")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
json.Unmarshal(file, appConf)
|
|
fmt.Println("读取配置文件成功")
|
|
fmt.Println(appConf)
|
|
}
|
|
|
|
func GetConfig() *AppConf {
|
|
return appConf
|
|
}
|
|
|
|
func SetPath(path string) {
|
|
appConf.Path = path
|
|
confJson, err := json.Marshal(appConf)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
os.WriteFile("conf.json", confJson, os.ModePerm)
|
|
}
|
|
|
|
func SetWorkSpace(workspace string) {
|
|
appConf.LastWorkspace = workspace
|
|
confJson, err := json.Marshal(appConf)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
os.WriteFile("conf.json", confJson, os.ModePerm)
|
|
}
|
|
|
|
type WorkspaceConf struct {
|
|
BrowserPath string `json:"browserPath"`
|
|
BrowserType string `json:"browserType"`
|
|
Workspace string `json:"workspace"`
|
|
}
|
|
|
|
func readWorkspaceConf(workspace string) (*WorkspaceConf, error) {
|
|
currWorkConf := &WorkspaceConf{}
|
|
dataDir := GetDataDir()
|
|
workDir := filepath.Join(dataDir, workspace)
|
|
_, err := os.Stat(workDir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
confJson, err := os.ReadFile(filepath.Join(workDir, "workspace.json"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = json.Unmarshal(confJson, currWorkConf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return currWorkConf, nil
|
|
}
|
|
|
|
func GetCurrWorkspaceConf() *WorkspaceConf {
|
|
conf, err := readWorkspaceConf(GetConfig().LastWorkspace)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return conf
|
|
}
|
|
|
|
func GetCurrWorkspace() string {
|
|
return GetConfig().LastWorkspace
|
|
}
|
|
|
|
func SetBrowserPath(browserPath string) {
|
|
conf := GetCurrWorkspaceConf()
|
|
conf.BrowserPath = browserPath
|
|
conf.WriteToFile()
|
|
}
|
|
|
|
func SetBrowserType(browserType string) {
|
|
conf := GetCurrWorkspaceConf()
|
|
conf.BrowserType = browserType
|
|
conf.WriteToFile()
|
|
}
|
|
|
|
func (conf *WorkspaceConf) WriteToFile() {
|
|
confJson, _ := json.Marshal(conf)
|
|
jsonDir := filepath.Join(GetDataDir(), conf.Workspace, "workspace.json")
|
|
fmt.Printf("写出配置文件: %v -> %v\n", jsonDir, confJson)
|
|
err := os.WriteFile(jsonDir, confJson, os.ModePerm)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|