Revert "feat: multiple configs"
Some checks failed
checks / build (push) Failing after 1m14s
checks / build (pull_request) Failing after 1m10s

This reverts commit 0409bee600.
This commit is contained in:
zzjc1234 2024-09-20 15:30:00 +08:00
parent 0409bee600
commit 8d0000016a
2 changed files with 28 additions and 92 deletions

View File

@ -3,9 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"log/slog" "log/slog"
"os"
"regexp" "regexp"
"strconv"
"strings" "strings"
"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
@ -13,14 +11,6 @@ import (
"github.com/koding/multiconfig" "github.com/koding/multiconfig"
) )
type JobType int
const (
HC JobType = iota
CQ
OJ
)
type Conf struct { type Conf struct {
SandboxExecServer string `default:"localhost:5051"` SandboxExecServer string `default:"localhost:5051"`
SandboxToken string `default:""` SandboxToken string `default:""`
@ -91,7 +81,7 @@ func parseConfFile(path string) (conf Conf, err error) {
return return
} }
func _validateHw(hw string) error { func validateHw(hw string) error {
matched, err := regexp.MatchString(`^hw[0-9]+$`, hw) matched, err := regexp.MatchString(`^hw[0-9]+$`, hw)
if err != nil { if err != nil {
return fmt.Errorf("error compiling regex: %w", err) return fmt.Errorf("error compiling regex: %w", err)
@ -102,36 +92,7 @@ func _validateHw(hw string) error {
return nil return nil
} }
func _getExList(pattern string) (hwList []string, err error) { func commitMsgToConf() (conf Conf, err error) {
re, err := regexp.Compile(pattern)
if err != nil {
return nil, fmt.Errorf("failed to compile regex: %w", err)
}
files, err := os.ReadDir(".")
if err != nil {
return nil, fmt.Errorf("error reading directory: %w", err)
}
for _, file := range files {
if !file.IsDir() && re.MatchString(file.Name()) {
hwList = append(hwList, file.Name())
}
}
return hwList, nil
}
func _contains(list []string, item string) bool {
for _, v := range list {
if v == item {
return true
}
}
return false
}
func commitMsgToConf() (confs []Conf, jobtype JobType, err error) {
r, err := git.PlainOpen(".") r, err := git.PlainOpen(".")
if err != nil { if err != nil {
return return
@ -147,75 +108,51 @@ func commitMsgToConf() (confs []Conf, jobtype JobType, err error) {
return return
} }
var conf Conf
var exList []string
jobtype = HC
file := "conf.json" file := "conf.json"
msg := commit.Message msg := commit.Message
slog.Debug("commit msg to conf", "msg", msg) slog.Debug("commit msg to conf", "msg", msg)
// INFO: default config
conf, err = parseConfFile(file)
confs = append(confs, conf)
if msg == "" { if msg == "" {
conf, err = parseConfFile(file)
return return
} }
line := strings.Split(msg, "\n")[0] line := strings.Split(msg, "\n")[0]
words := strings.Fields(line)
head := words[0]
words := strings.Fields(line)
head := words[0]
var hw string var hw string
// INFO: get worktype
if strings.HasSuffix(head, ":") || strings.HasSuffix(head, ".") { if strings.HasSuffix(head, ":") || strings.HasSuffix(head, ".") {
head = head[:len(head)-1] head = head[:len(head)-1]
} }
switch head { switch head {
case "feat", "fix", "refactor", "perf", "test", "build", "revert": case "feat", "fix", "refactor", "perf", "test", "build", "revert":
jobtype = CQ // TODO: Decide strategy to give students error
case "joj", "grading": // if len(words) < 2 {
jobtype = OJ // return Conf{}, fmt.Errorf("error: hw not assigned")
} // }
if len(words) >= 2 {
// INFO: get configs hw = words[1]
if len(words) >= 2 { if err = validateHw(hw); err == nil {
hw = words[1] file = strings.Replace(file, "conf", "conf-"+hw+"-cq", 1)
if err = _validateHw(hw); err == nil { }
file = strings.Replace(file, "conf", "conf-"+hw, 1) }
} case "joj", "grading":
var exTot []string // TODO: Decide strategy to give students error
// if len(words) < 2 {
// INFO: get all hw ex if no args provided // return Conf{}, fmt.Errorf("error: hw not assigned")
exTot, err = _getExList(file) // }
if len(words) == 2 { if len(words) >= 2 {
exList = exTot hw = words[1]
} else { if err = validateHw(hw); err == nil {
var tmpList []string file = strings.Replace(file, "conf", "conf-"+hw+"-oj", 1)
for idx := 2; idx < len(words); idx++ {
word := words[idx]
num, err := strconv.Atoi(word)
if err != nil {
return confs, HC, fmt.Errorf("error: bad ex number")
}
file := fmt.Sprintf("conf-"+hw+"-ex%d.json", num)
if !_contains(exList, file) {
return confs, HC, fmt.Errorf("error: bad ex number")
}
tmpList = append(tmpList, file)
} }
exList = tmpList
} }
} }
for _, ex := range exList { conf, err = parseConfFile(file)
conf, err = parseConfFile(ex)
if err != nil {
return
}
confs = append(confs, conf)
}
return return
} }

View File

@ -71,12 +71,11 @@ func outputResult(conf Conf, results []stage.StageResult) error {
} }
func mainImpl() error { func mainImpl() error {
conf, jobtype, err := commitMsgToConf() conf, err := commitMsgToConf()
if err != nil { if err != nil {
slog.Error("no conf found", "error", err) slog.Error("no conf found", "error", err)
return err return err
} }
// TODO: implementation for multiple configs
setupSlog(conf) setupSlog(conf)
executors.InitWithConf(conf.SandboxExecServer, conf.SandboxToken) executors.InitWithConf(conf.SandboxExecServer, conf.SandboxToken)
stages, err := generateStages(conf) stages, err := generateStages(conf)