feat(cmd/joj3): fallback to conf in conf-root on parse msg error
This commit is contained in:
parent
0a28347247
commit
896c4ac220
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -215,7 +216,9 @@ func GetSHA256(filePath string) (string, error) {
|
||||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseMsg(confRoot, confName, msg, tag string) (confPath, group string, err error) {
|
func parseMsg(confRoot, confName, msg, tag string) (
|
||||||
|
confPath, group string, err error,
|
||||||
|
) {
|
||||||
slog.Info("parse msg", "msg", msg)
|
slog.Info("parse msg", "msg", msg)
|
||||||
conventionalCommit, err := parseConventionalCommit(msg)
|
conventionalCommit, err := parseConventionalCommit(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -249,7 +252,7 @@ func ParseMsg(confRoot, confName, msg, tag string) (confPath, group string, err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func HintValidScopes(confRoot, confName string) {
|
func hintValidScopes(confRoot, confName string) {
|
||||||
confRoot = filepath.Clean(confRoot)
|
confRoot = filepath.Clean(confRoot)
|
||||||
validScopes := []string{}
|
validScopes := []string{}
|
||||||
_ = filepath.Walk(confRoot, func(
|
_ = filepath.Walk(confRoot, func(
|
||||||
|
@ -279,6 +282,32 @@ func HintValidScopes(confRoot, confName string) {
|
||||||
"valid scopes", validScopes)
|
"valid scopes", validScopes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetConfPath(confRoot, confName, msg, tag string) (
|
||||||
|
confPath, group string, confStat fs.FileInfo, err error,
|
||||||
|
) {
|
||||||
|
confPath, group, err = parseMsg(confRoot, confName, msg, tag)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("parse msg", "error", err)
|
||||||
|
// fallback to conf file in conf root on parse error
|
||||||
|
confPath = filepath.Clean(fmt.Sprintf("%s/%s", confRoot, confName))
|
||||||
|
}
|
||||||
|
confStat, err = os.Stat(confPath)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
hintValidScopes(confRoot, confName)
|
||||||
|
}
|
||||||
|
slog.Error("stat conf", "error", err)
|
||||||
|
// fallback to conf file in conf root on conf not exist
|
||||||
|
confPath = filepath.Clean(fmt.Sprintf("%s/%s", confRoot, confName))
|
||||||
|
confStat, err = os.Stat(confPath)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("stat fallback conf", "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func CheckExpire(conf *Conf) error {
|
func CheckExpire(conf *Conf) error {
|
||||||
if conf.ExpireUnixTimestamp > 0 &&
|
if conf.ExpireUnixTimestamp > 0 &&
|
||||||
conf.ExpireUnixTimestamp < time.Now().Unix() {
|
conf.ExpireUnixTimestamp < time.Now().Unix() {
|
||||||
|
|
|
@ -45,26 +45,13 @@ func mainImpl() error {
|
||||||
slog.Error("get commit msg", "error", err)
|
slog.Error("get commit msg", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
confPath, group, err := conf.ParseMsg(confRoot, confName, msg, tag)
|
confPath, group, confStat, err := conf.GetConfPath(
|
||||||
|
confRoot, confName, msg, tag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("parse msg", "error", err)
|
slog.Error("get conf path", "error", err)
|
||||||
conf.HintValidScopes(confRoot, confName)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
slog.Info("try to load conf", "path", confPath)
|
slog.Info("try to load conf", "path", confPath)
|
||||||
confStat, err := os.Stat(confPath)
|
|
||||||
if err != nil {
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
conf.HintValidScopes(confRoot, confName)
|
|
||||||
}
|
|
||||||
slog.Error("stat conf", "error", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
sha256, err := conf.GetSHA256(confPath)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("get sha256", "error", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
confObj, err := conf.ParseConfFile(confPath)
|
confObj, err := conf.ParseConfFile(confPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("parse conf", "error", err)
|
slog.Error("parse conf", "error", err)
|
||||||
|
@ -75,6 +62,11 @@ func mainImpl() error {
|
||||||
slog.Error("setup slog", "error", err)
|
slog.Error("setup slog", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
sha256, err := conf.GetSHA256(confPath)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("get sha256", "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
slog.Info("conf info", "sha256", sha256, "modTime", confStat.ModTime(),
|
slog.Info("conf info", "sha256", sha256, "modTime", confStat.ModTime(),
|
||||||
"size", confStat.Size())
|
"size", confStat.Size())
|
||||||
if err := conf.CheckExpire(confObj); err != nil {
|
if err := conf.CheckExpire(confObj); err != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user