From d299849c788b2518c803903b0625138d65a5d958 Mon Sep 17 00:00:00 2001 From: zzjc1234 <2359047351@qq.com> Date: Thu, 19 Sep 2024 10:31:51 +0800 Subject: [PATCH] fix: bad hw pattern --- cmd/joj3/conf.go | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/cmd/joj3/conf.go b/cmd/joj3/conf.go index 2bb892d..5e332ec 100644 --- a/cmd/joj3/conf.go +++ b/cmd/joj3/conf.go @@ -1,7 +1,9 @@ package main import ( + "fmt" "log/slog" + "regexp" "strings" "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" @@ -70,6 +72,17 @@ func parseConfFile(path string) (conf Conf, err error) { return } +func validateHw(hw string) error { + matched, err := regexp.MatchString(`^hw[0-9]+$`, hw) + if err != nil { + return fmt.Errorf("error compiling regex: %w", err) + } + if !matched { + return fmt.Errorf("error: hw does not match the required pattern") + } + return nil +} + func commitMsgToConf() (conf Conf, err error) { r, err := git.PlainOpen(".") if err != nil { @@ -87,19 +100,36 @@ func commitMsgToConf() (conf Conf, err error) { slog.Debug("commit msg to conf", "msg", msg) line := strings.Split(msg, "\n")[0] - words := strings.TrimSpace(line) - head := string(words[0]) + words := strings.Fields(line) - file := "conf.toml" + head := words[0] + var hw string if strings.HasSuffix(head, ":") || strings.HasSuffix(head, ".") { head = head[:len(head)-1] } + + file := "conf.toml" + switch head { case "feat", "fix", "refactor", "perf", "test", "build", "revert": - strings.Replace(file, "conf", "conf-cp", 1) + if len(words) < 2 { + return Conf{}, fmt.Errorf("error: hw not assigned") + } + hw = words[1] + if err = validateHw(hw); err != nil { + return + } + file = strings.Replace(file, "conf", "conf-"+hw+"-cq", 1) case "joj", "grading": - strings.Replace(file, "conf", "conf-oj", 1) + if len(words) < 2 { + return Conf{}, fmt.Errorf("error: hw not assigned") + } + hw = words[1] + if err = validateHw(hw); err != nil { + return + } + file = strings.Replace(file, "conf", "conf-"+hw+"-oj", 1) } conf, err = parseConfFile(file)