package main import ( "fmt" "log/slog" "regexp" "strings" "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" "github.com/go-git/go-git/v5" "github.com/koding/multiconfig" ) type Conf struct { SandboxExecServer string `default:"localhost:5051"` SandboxToken string `default:""` LogLevel int `default:"0"` OutputPath string `default:"joj3_result.json"` Stages []struct { Name string Executor struct { Name string With struct { Default stage.Cmd Cases []OptionalCmd } } Parser struct { Name string With interface{} } } } type OptionalCmd struct { Args *[]string Env *[]string Stdin *stage.CmdFile Stdout *stage.CmdFile Stderr *stage.CmdFile CPULimit *uint64 RealCPULimit *uint64 ClockLimit *uint64 MemoryLimit *uint64 StackLimit *uint64 ProcLimit *uint64 CPURateLimit *uint64 CPUSetLimit *string CopyIn *map[string]stage.CmdFile CopyInCached *map[string]string CopyInCwd *bool CopyOut *[]string CopyOutCached *[]string CopyOutMax *uint64 CopyOutDir *string TTY *bool StrictMemoryLimit *bool DataSegmentLimit *bool AddressSpaceLimit *bool } func parseConfFile(path string) (conf Conf, err error) { d := &multiconfig.DefaultLoader{} d.Loader = multiconfig.MultiLoader( &multiconfig.TagLoader{}, &multiconfig.JSONLoader{Path: path}, ) d.Validator = multiconfig.MultiValidator(&multiconfig.RequiredValidator{}) if err = d.Load(&conf); err != nil { slog.Error("parse stages conf", "error", err) return } if err = d.Validate(&conf); err != nil { slog.Error("validate stages conf", "error", err) return } 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 { return } ref, err := r.Head() if err != nil { return } commit, err := r.CommitObject(ref.Hash()) if err != nil { return } file := "conf.json" msg := commit.Message slog.Debug("commit msg to conf", "msg", msg) if msg == "" { conf, err = parseConfFile(file) return } line := strings.Split(msg, "\n")[0] words := strings.Fields(line) head := words[0] var hw string if strings.HasSuffix(head, ":") || strings.HasSuffix(head, ".") { head = head[:len(head)-1] } switch head { case "feat", "fix", "refactor", "perf", "test", "build", "revert": // TODO: Decide strategy to give students error // if len(words) < 2 { // return Conf{}, fmt.Errorf("error: hw not assigned") // } if len(words) >= 2 { hw = words[1] if err = validateHw(hw); err == nil { file = strings.Replace(file, "conf", "conf-"+hw+"-cq", 1) } } case "joj", "grading": // TODO: Decide strategy to give students error // if len(words) < 2 { // return Conf{}, fmt.Errorf("error: hw not assigned") // } if len(words) >= 2 { hw = words[1] if err = validateHw(hw); err == nil { file = strings.Replace(file, "conf", "conf-"+hw+"-oj", 1) } } } conf, err = parseConfFile(file) return }