parent
							
								
									0409bee600
								
							
						
					
					
						commit
						8d0000016a
					
				
							
								
								
									
										109
									
								
								cmd/joj3/conf.go
									
									
									
									
									
								
							
							
						
						
									
										109
									
								
								cmd/joj3/conf.go
									
									
									
									
									
								
							|  | @ -3,9 +3,7 @@ package main | |||
| import ( | ||||
| 	"fmt" | ||||
| 	"log/slog" | ||||
| 	"os" | ||||
| 	"regexp" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| 
 | ||||
| 	"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" | ||||
|  | @ -13,14 +11,6 @@ import ( | |||
| 	"github.com/koding/multiconfig" | ||||
| ) | ||||
| 
 | ||||
| type JobType int | ||||
| 
 | ||||
| const ( | ||||
| 	HC JobType = iota | ||||
| 	CQ | ||||
| 	OJ | ||||
| ) | ||||
| 
 | ||||
| type Conf struct { | ||||
| 	SandboxExecServer string `default:"localhost:5051"` | ||||
| 	SandboxToken      string `default:""` | ||||
|  | @ -91,7 +81,7 @@ func parseConfFile(path string) (conf Conf, err error) { | |||
| 	return | ||||
| } | ||||
| 
 | ||||
| func _validateHw(hw string) error { | ||||
| func validateHw(hw string) error { | ||||
| 	matched, err := regexp.MatchString(`^hw[0-9]+$`, hw) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("error compiling regex: %w", err) | ||||
|  | @ -102,36 +92,7 @@ func _validateHw(hw string) error { | |||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func _getExList(pattern string) (hwList []string, 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) { | ||||
| func commitMsgToConf() (conf Conf, err error) { | ||||
| 	r, err := git.PlainOpen(".") | ||||
| 	if err != nil { | ||||
| 		return | ||||
|  | @ -147,75 +108,51 @@ func commitMsgToConf() (confs []Conf, jobtype JobType, err error) { | |||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	var conf Conf | ||||
| 	var exList []string | ||||
| 	jobtype = HC | ||||
| 	file := "conf.json" | ||||
| 
 | ||||
| 	msg := commit.Message | ||||
| 	slog.Debug("commit msg to conf", "msg", msg) | ||||
| 
 | ||||
| 	// INFO: default config
 | ||||
| 	conf, err = parseConfFile(file) | ||||
| 	confs = append(confs, conf) | ||||
| 	if msg == "" { | ||||
| 		conf, err = parseConfFile(file) | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	line := strings.Split(msg, "\n")[0] | ||||
| 	words := strings.Fields(line) | ||||
| 	head := words[0] | ||||
| 
 | ||||
| 	words := strings.Fields(line) | ||||
| 
 | ||||
| 	head := words[0] | ||||
| 	var hw string | ||||
| 
 | ||||
| 	// INFO: get worktype
 | ||||
| 	if strings.HasSuffix(head, ":") || strings.HasSuffix(head, ".") { | ||||
| 		head = head[:len(head)-1] | ||||
| 	} | ||||
| 
 | ||||
| 	switch head { | ||||
| 	case "feat", "fix", "refactor", "perf", "test", "build", "revert": | ||||
| 		jobtype = CQ | ||||
| 	case "joj", "grading": | ||||
| 		jobtype = OJ | ||||
| 	} | ||||
| 
 | ||||
| 	// INFO: get configs
 | ||||
| 		// 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, 1) | ||||
| 			if err = validateHw(hw); err == nil { | ||||
| 				file = strings.Replace(file, "conf", "conf-"+hw+"-cq", 1) | ||||
| 			} | ||||
| 		var exTot []string | ||||
| 
 | ||||
| 		// INFO: get all hw ex if no args provided
 | ||||
| 		exTot, err = _getExList(file) | ||||
| 		if len(words) == 2 { | ||||
| 			exList = exTot | ||||
| 		} else { | ||||
| 			var tmpList []string | ||||
| 			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") | ||||
| 	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) | ||||
| 			} | ||||
| 				tmpList = append(tmpList, file) | ||||
| 			} | ||||
| 			exList = tmpList | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	for _, ex := range exList { | ||||
| 		conf, err = parseConfFile(ex) | ||||
| 		if err != nil { | ||||
| 			return | ||||
| 		} | ||||
| 		confs = append(confs, conf) | ||||
| 	} | ||||
| 
 | ||||
| 	conf, err = parseConfFile(file) | ||||
| 	return | ||||
| } | ||||
|  |  | |||
|  | @ -71,12 +71,11 @@ func outputResult(conf Conf, results []stage.StageResult) error { | |||
| } | ||||
| 
 | ||||
| func mainImpl() error { | ||||
| 	conf, jobtype, err := commitMsgToConf() | ||||
| 	conf, err := commitMsgToConf() | ||||
| 	if err != nil { | ||||
| 		slog.Error("no conf found", "error", err) | ||||
| 		return err | ||||
| 	} | ||||
| 	// TODO: implementation for multiple configs
 | ||||
| 	setupSlog(conf) | ||||
| 	executors.InitWithConf(conf.SandboxExecServer, conf.SandboxToken) | ||||
| 	stages, err := generateStages(conf) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 zzjc1234
						zzjc1234