diff --git a/cmd/joj3/conf/conf.go b/cmd/joj3/conf/conf.go index dd29123..bd1d23c 100644 --- a/cmd/joj3/conf/conf.go +++ b/cmd/joj3/conf/conf.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "fmt" "io" + "io/fs" "log/slog" "os" "path/filepath" @@ -215,7 +216,9 @@ func GetSHA256(filePath string) (string, error) { 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) conventionalCommit, err := parseConventionalCommit(msg) if err != nil { @@ -249,7 +252,7 @@ func ParseMsg(confRoot, confName, msg, tag string) (confPath, group string, err return } -func HintValidScopes(confRoot, confName string) { +func hintValidScopes(confRoot, confName string) { confRoot = filepath.Clean(confRoot) validScopes := []string{} _ = filepath.Walk(confRoot, func( @@ -279,6 +282,32 @@ func HintValidScopes(confRoot, confName string) { "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 { if conf.ExpireUnixTimestamp > 0 && conf.ExpireUnixTimestamp < time.Now().Unix() { diff --git a/cmd/joj3/main.go b/cmd/joj3/main.go index e065db8..756c2c4 100644 --- a/cmd/joj3/main.go +++ b/cmd/joj3/main.go @@ -45,26 +45,13 @@ func mainImpl() error { slog.Error("get commit msg", "error", 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 { - slog.Error("parse msg", "error", err) - conf.HintValidScopes(confRoot, confName) + slog.Error("get conf path", "error", err) return err } 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) if err != nil { slog.Error("parse conf", "error", err) @@ -75,6 +62,11 @@ func mainImpl() error { slog.Error("setup slog", "error", 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(), "size", confStat.Size()) if err := conf.CheckExpire(confObj); err != nil {