feat: hint valid scopes
All checks were successful
build / build (push) Successful in 1m17s
build / trigger-build-image (push) Successful in 5s

This commit is contained in:
张泊明518370910136 2024-10-03 05:20:34 -04:00
parent 64ae632003
commit 5c2fe4ffcb
GPG Key ID: D47306D7062CDA9D
2 changed files with 36 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"log/slog" "log/slog"
"os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -140,3 +141,31 @@ func parseMsg(confRoot, confName, msg string) (conf Conf, group string, err erro
slog.Debug("conf loaded", "conf", conf) slog.Debug("conf loaded", "conf", conf)
return return
} }
func listValidScopes(confRoot, confName, msg string) ([]string, error) {
conventionalCommit, err := parseConventionalCommit(msg)
if err != nil {
return []string{}, err
}
slog.Info("conventional commit", "commit", conventionalCommit)
confRoot = filepath.Clean(confRoot)
validScopes := []string{}
err = filepath.Walk(confRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
slog.Error("list valid scopes", "error", err)
return err
}
if info.IsDir() {
confPath := filepath.Join(path, confName)
if _, err := os.Stat(confPath); err == nil {
relPath, err := filepath.Rel(confRoot, path)
if err != nil {
return err
}
validScopes = append(validScopes, relPath)
}
}
return nil
})
return validScopes, err
}

View File

@ -122,6 +122,13 @@ func mainImpl() error {
conf, group, err := parseMsg(confRoot, confName, msg) conf, group, err := parseMsg(confRoot, confName, msg)
if err != nil { if err != nil {
slog.Error("parse msg", "error", err) slog.Error("parse msg", "error", err)
validScopes, scopeErr := listValidScopes(
confRoot, confName, msg)
if scopeErr != nil {
slog.Error("list valid scopes", "error", scopeErr)
return scopeErr
}
slog.Info("hint: valid scopes in commit message", "scopes", validScopes)
return err return err
} }
if err := setupSlog(conf.LogPath); err != nil { // after conf is loaded if err := setupSlog(conf.LogPath); err != nil { // after conf is loaded