Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
feat(conf): auto-gen config file when initializing the app
Browse files Browse the repository at this point in the history
Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode committed Oct 23, 2022
1 parent 02bd2a3 commit 13e2bab
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 87 deletions.
10 changes: 5 additions & 5 deletions artalk-go.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ ssl:

# 管理员账户
admin_users:
- name: "admin"
email: "[email protected]"
password: "" # 支持 bcrypt 或 md5 加密,如:"(md5)50c21190c6e4e5418c6a90d2b5031119"
badge_name: "管理员"
badge_color: "#FF6C00"
# - name: "admin"
# email: "[email protected]"
# password: "" # 支持 bcrypt 或 md5 加密,如:"(md5)50c21190c6e4e5418c6a90d2b5031119"
# badge_name: "管理员"
# badge_color: "#FF6C00"

# 评论审核
moderator:
Expand Down
80 changes: 9 additions & 71 deletions cmd/gen.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package cmd

import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strings"

"github.com/ArtalkJS/ArtalkGo/pkged"
"github.com/ArtalkJS/ArtalkGo/lib/core"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -19,70 +14,23 @@ var genCmd = &cobra.Command{
Long: "生成一些内容\n例如:artalk-go gen config ./artalk-go.yml",
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
isFileExisted := func(path string) bool {
_, err := os.Stat(path)
return err == nil
}

// 工作目录
if workDir != "" {
if err := os.Chdir(workDir); err != nil {
logrus.Fatal("工作目录切换错误 ", err)
}
}

// 参数
genType := args[0]
if genType == "config" || genType == "conf" {
genType = "artalk-go.example.yml"
}

genPath := filepath.Base(genType)
if len(args) >= 2 {
genPath = args[1]
}

file, err := pkged.Open("/" + strings.TrimPrefix(genType, "/"))
if err != nil {
logrus.Fatal("无效的内置资源: "+genType+" ", err)
var (
specificPath string
isForce bool
)
if len(args) > 1 {
specificPath = args[1]
}
isForce, _ = cmd.Flags().GetBool("force")

buf, err := ioutil.ReadAll(file)
if err != nil {
logrus.Fatal("读取内置资源: "+genType+" 失败 ", err)
}

// 自动生成 app_key
if strings.Contains(filepath.Base(genType), "artalk-go.example.yml") {
str := string(buf)
appKey := RandStringRunes(16)
str = strings.Replace(str, `app_key: ""`, fmt.Sprintf(`app_key: "%s"`, appKey), 1)
buf = []byte(str)
}

absPath, err := filepath.Abs(genPath)
if err != nil {
logrus.Fatal(err)
}
if s, err := os.Stat(absPath); err == nil && s.IsDir() {
absPath = filepath.Join(absPath, filepath.Base(genType))
}
isForce, _ := cmd.Flags().GetBool("force")
if isFileExisted(absPath) && !isForce {
logrus.Fatal("已存在文件: " + absPath)
}

dst, err := os.Create(absPath)
if err != nil {
logrus.Fatal("创建目标文件失败 ", err)
}
defer dst.Close()

if _, err = dst.Write(buf); err != nil {
logrus.Fatal("写入目标文件失败 ", err)
}

logrus.Info("创建文件: " + absPath)
core.Gen(args[0], specificPath, isForce)
},
}

Expand All @@ -91,13 +39,3 @@ func init() {

flagPV(genCmd, "force", "f", false, "Force overwrite an existing file")
}

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*")

func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
10 changes: 1 addition & 9 deletions config/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"os"
"strings"
"time"

Expand All @@ -27,14 +26,7 @@ func GetCfgFileLoaded() string {
}

// Init 初始化配置
func Init(cfgFile string, workDir string) {
// 切换工作目录
if workDir != "" {
if err := os.Chdir(workDir); err != nil {
logrus.Fatal("工作目录切换错误 ", err)
}
}

func Init(cfgFile string) {
if cfgFile == "" {
cfgFile = DEFAULT_CONF_FILE
}
Expand Down
18 changes: 17 additions & 1 deletion lib/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,23 @@ func LoadConfOnly(cfgFile string, workDir string) {

// 1. 初始化配置
func initConfig(cfgFile string, workDir string) {
config.Init(cfgFile, workDir)
// 切换工作目录
if workDir != "" {
if err := os.Chdir(workDir); err != nil {
logrus.Fatal("工作目录切换错误 ", err)
}
}

if cfgFile == "" {
cfgFile = config.DEFAULT_CONF_FILE
}

// 自动生成新配置文件
if !CheckFileExist(cfgFile) {
Gen("config", cfgFile, false)
}

config.Init(cfgFile)
}

// 2. 初始化日志
Expand Down
82 changes: 82 additions & 0 deletions lib/core/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package core

import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strings"

"github.com/ArtalkJS/ArtalkGo/pkged"
"github.com/sirupsen/logrus"
)

func Gen(genType string, specificPath string, overwrite bool) {
// 参数
if genType == "config" || genType == "conf" {
genType = "artalk-go.example.yml"
}

genPath := filepath.Base(genType)
if specificPath != "" {
genPath = specificPath
}

file, err := pkged.Open("/" + strings.TrimPrefix(genType, "/"))
if err != nil {
logrus.Fatal("无效的内置资源: "+genType+" ", err)
}

buf, err := ioutil.ReadAll(file)
if err != nil {
logrus.Fatal("读取内置资源: "+genType+" 失败 ", err)
}

// 自动生成 app_key
if strings.Contains(filepath.Base(genType), "artalk-go.example.yml") {
str := string(buf)
appKey := RandStringRunes(16)
str = strings.Replace(str, `app_key: ""`, fmt.Sprintf(`app_key: "%s"`, appKey), 1)
buf = []byte(str)
}

absPath, err := filepath.Abs(genPath)
if err != nil {
logrus.Fatal(err)
}
if s, err := os.Stat(absPath); err == nil && s.IsDir() {
absPath = filepath.Join(absPath, filepath.Base(genType))
}

if CheckFileExist(absPath) && !overwrite {
logrus.Fatal("已存在文件: " + absPath)
}

dst, err := os.Create(absPath)
if err != nil {
logrus.Fatal("创建目标文件失败 ", err)
}
defer dst.Close()

if _, err = dst.Write(buf); err != nil {
logrus.Fatal("写入目标文件失败 ", err)
}

logrus.Info("生成文件: " + absPath)
}

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*")

func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}

func CheckFileExist(path string) bool {
_, err := os.Stat(path)
return err == nil
}
2 changes: 1 addition & 1 deletion model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestMain(m *testing.M) {
var err error

// 加载测试配置
config.Init("./testdata/model_test_conf.yml", "")
config.Init("./testdata/model_test_conf.yml")

// 初始化测试数据库
dbFilename := "../data/test.db"
Expand Down

0 comments on commit 13e2bab

Please sign in to comment.