diff --git a/cmd/joj3/conf.go b/cmd/joj3/conf.go index 0556478..bb5f484 100644 --- a/cmd/joj3/conf.go +++ b/cmd/joj3/conf.go @@ -11,25 +11,35 @@ import ( "github.com/koding/multiconfig" ) +type JobType int + +const ( + HC JobType = iota + CQ + OJ +) + +type Stage struct { + Name string + Executor struct { + Name string + With struct { + Default stage.Cmd + Cases []OptionalCmd + } + } + Parser struct { + Name string + With interface{} + } +} + 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{} - } - } + Stages []Stage } type OptionalCmd struct { @@ -63,24 +73,51 @@ type OptionalCmd struct { AddressSpaceLimit *bool } -func parseConfFile(path string) (conf Conf, err error) { +func parseConfFile(path string, jobtype JobType) (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 } + + filteredStages := []Stage{} + + for _, stage := range conf.Stages { + if filterStage(stage, jobtype) { + filteredStages = append(filteredStages, stage) + } + } + + conf.Stages = filteredStages + return } +func filterStage(stage Stage, jobtype JobType, +) bool { + switch jobtype { + case HC: + return stage.Name == "healthcheck" + case CQ: + return stage.Name == "compile" || stage.Name == "healthcheck" + case OJ: + return true + default: + return false + } +} + func validateHw(hw string) error { matched, err := regexp.MatchString(`^hw[0-9]+$`, hw) if err != nil { @@ -109,11 +146,12 @@ func commitMsgToConf() (conf Conf, err error) { } file := "conf.json" + jobtype := HC msg := commit.Message slog.Debug("commit msg to conf", "msg", msg) if msg == "" { - conf, err = parseConfFile(file) + conf, err = parseConfFile(file, jobtype) return } @@ -128,31 +166,22 @@ func commitMsgToConf() (conf Conf, err error) { 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) - } + if len(words) == 3 { + hw = words[1] + if err = validateHw(hw); err != nil { + return } - 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) - } + + switch head { + case "feat", "fix", "refactor", "perf", "test", "build", "revert": + file = strings.Replace(file, "conf", "conf-"+hw, 1) + jobtype = CQ + case "joj", "grading": + file = strings.Replace(file, "conf", "conf-"+hw, 1) + jobtype = OJ } } - conf, err = parseConfFile(file) + conf, err = parseConfFile(file, jobtype) return }