Blog/config/main.go

111 lines
2.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package config
import (
"blog/utils"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
)
type Config struct {
userConfig
systemConfig
memosConfig
}
//
var Cfg Config
func init() {
var err error
Cfg.CurrentDir, err = os.Getwd()
if err != nil {
panic(err)
}
configFile, err := ioutil.ReadFile(Cfg.CurrentDir + "/config.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(configFile, &Cfg)
if err != nil {
panic(err)
}
if "" == Cfg.Dashboard ||
!strings.HasPrefix(Cfg.Dashboard, "/") {
Cfg.Dashboard = "/admin"
}
if "" == Cfg.ThemePath ||
!strings.HasPrefix(Cfg.ThemePath, "/") {
Cfg.ThemesDir = Cfg.CurrentDir + "/themes/blog" //+ Cfg.ThemePath
} else {
Cfg.ThemesDir = Cfg.CurrentDir + "/themes" + Cfg.ThemePath
}
repoName, err := utils.GetRepoName(Cfg.DocumentGitUrl)
if err != nil {
panic(err)
}
Cfg.AppName = "WebServer"
Cfg.Version = 3.2
Cfg.DocumentDir = Cfg.CurrentDir + "/" + repoName
Cfg.GitHookUrl = "/api/git_push_hook"
Cfg.AppRepository = "https://gitea.starss.cc/JiXieShi/Blog"
}
func Initial() {
if _, err := exec.LookPath("git"); err != nil {
fmt.Println("请先安装git")
panic(err)
}
if !utils.IsDir(Cfg.DocumentDir) {
fmt.Println("正在克隆文档仓库,请稍等...")
out, err := utils.RunCmdByDir(Cfg.CurrentDir, "git", "clone", Cfg.DocumentGitUrl)
if err != nil {
panic(err)
}
fmt.Println(out)
} else {
out, err := utils.RunCmdByDir(Cfg.DocumentDir, "git", "pull")
fmt.Println(out)
if err != nil {
panic(err)
}
}
if err := checkDocDirAndBindConfig(&Cfg); err != nil {
fmt.Println("文档缺少必要的目录")
panic(err)
}
imgDir := Cfg.CurrentDir + "/images"
if !utils.IsDir(imgDir) {
if os.Mkdir(imgDir, os.ModePerm) != nil {
panic("生成images目录失败")
}
}
}
func checkDocDirAndBindConfig(cfg *Config) error {
dirs := []string{"assets", "content", "extra_nav"}
for _, dir := range dirs {
absoluteDir := Cfg.DocumentDir + "/" + dir
if !utils.IsDir(absoluteDir) {
return errors.New("documents cannot lack " + absoluteDir + " dir")
}
}
cfg.DocumentAssetsDir = cfg.DocumentDir + "/assets"
cfg.DocumentContentDir = cfg.DocumentDir + "/content"
cfg.DocumentExtraNavDir = cfg.DocumentDir + "/extra_nav"
return nil
}