refactor(cmd/joj3): split parse msg & parse conf file
All checks were successful
build / build (push) Successful in 1m4s
build / trigger-build-image (push) Successful in 7s

This commit is contained in:
张泊明518370910136 2024-10-12 06:57:01 -04:00
parent f8a7ae6067
commit 9cd0e44678
GPG Key ID: D47306D7062CDA9D
2 changed files with 11 additions and 10 deletions

View File

@ -144,7 +144,7 @@ func parseConventionalCommit(commit string) (*ConventionalCommit, error) {
return cc, nil return cc, nil
} }
func parseConfFile(path string) (conf Conf, err error) { func ParseConfFile(path string) (conf Conf, err error) {
d := &multiconfig.DefaultLoader{} d := &multiconfig.DefaultLoader{}
d.Loader = multiconfig.MultiLoader( d.Loader = multiconfig.MultiLoader(
&multiconfig.TagLoader{}, &multiconfig.TagLoader{},
@ -191,7 +191,7 @@ func parseConfFile(path string) (conf Conf, err error) {
return return
} }
func ParseMsg(confRoot, confName, msg string) (conf Conf, group string, err error) { func ParseMsg(confRoot, confName, msg 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 {
@ -199,7 +199,7 @@ func ParseMsg(confRoot, confName, msg string) (conf Conf, group string, err erro
} }
slog.Info("conventional commit", "commit", conventionalCommit) slog.Info("conventional commit", "commit", conventionalCommit)
confRoot = filepath.Clean(confRoot) confRoot = filepath.Clean(confRoot)
confPath := filepath.Clean(fmt.Sprintf("%s/%s/%s", confPath = filepath.Clean(fmt.Sprintf("%s/%s/%s",
confRoot, conventionalCommit.Scope, confName)) confRoot, conventionalCommit.Scope, confName))
relPath, err := filepath.Rel(confRoot, confPath) relPath, err := filepath.Rel(confRoot, confPath)
if err != nil { if err != nil {
@ -209,11 +209,6 @@ func ParseMsg(confRoot, confName, msg string) (conf Conf, group string, err erro
err = fmt.Errorf("invalid scope as path: %s", conventionalCommit.Scope) err = fmt.Errorf("invalid scope as path: %s", conventionalCommit.Scope)
return return
} }
slog.Info("try to load conf", "path", confPath)
conf, err = parseConfFile(confPath)
if err != nil {
return
}
groupKeywords := []string{"joj"} groupKeywords := []string{"joj"}
for _, groupKeyword := range groupKeywords { for _, groupKeyword := range groupKeywords {
if strings.Contains( if strings.Contains(
@ -222,7 +217,6 @@ func ParseMsg(confRoot, confName, msg string) (conf Conf, group string, err erro
break break
} }
} }
slog.Debug("conf loaded", "conf", conf)
return return
} }

View File

@ -45,7 +45,7 @@ func mainImpl() error {
return err return err
} }
} }
confObj, group, err := conf.ParseMsg(confRoot, confName, msg) confPath, group, err := conf.ParseMsg(confRoot, confName, msg)
if err != nil { if err != nil {
slog.Error("parse msg", "error", err) slog.Error("parse msg", "error", err)
validScopes, scopeErr := conf.ListValidScopes( validScopes, scopeErr := conf.ListValidScopes(
@ -58,6 +58,13 @@ func mainImpl() error {
"valid scopes", validScopes) "valid scopes", validScopes)
return err return err
} }
slog.Info("try to load conf", "path", confPath)
confObj, err := conf.ParseConfFile(confPath)
if err != nil {
slog.Error("parse conf", "error", err)
return err
}
slog.Debug("conf loaded", "conf", confObj)
if err := setupSlog(confObj.LogPath); err != nil { // after conf is loaded if err := setupSlog(confObj.LogPath); err != nil { // after conf is loaded
slog.Error("setup slog", "error", err) slog.Error("setup slog", "error", err)
return err return err