This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(conf): auto-gen config file when initializing the app
Signed-off-by: qwqcode <[email protected]>
- Loading branch information
Showing
6 changed files
with
115 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters