This commit is contained in:
		
							parent
							
								
									8992d9df7a
								
							
						
					
					
						commit
						d172fae912
					
				|  | @ -1,7 +1,11 @@ | ||||||
| package main | package main | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"errors" | ||||||
|  | 	"fmt" | ||||||
| 	"log/slog" | 	"log/slog" | ||||||
|  | 	"os" | ||||||
|  | 	"regexp" | ||||||
| 
 | 
 | ||||||
| 	"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" | 	"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" | ||||||
| 	"github.com/go-git/go-git/v5" | 	"github.com/go-git/go-git/v5" | ||||||
|  | @ -60,6 +64,46 @@ type OptionalCmd struct { | ||||||
| 	AddressSpaceLimit *bool | 	AddressSpaceLimit *bool | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | // TODO: add other fields to match? not only limit to latest commit message
 | ||||||
|  | type MetaConf struct { | ||||||
|  | 	Patterns []struct { | ||||||
|  | 		Filename string | ||||||
|  | 		Regex    string | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func parseMetaConfFile(path string) (metaConf MetaConf, err error) { | ||||||
|  | 	// FIXME: remove this default meta config, it is only for demonstration
 | ||||||
|  | 	if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { | ||||||
|  | 		return MetaConf{ | ||||||
|  | 			Patterns: []struct { | ||||||
|  | 				Filename string | ||||||
|  | 				Regex    string | ||||||
|  | 			}{ | ||||||
|  | 				{ | ||||||
|  | 					Filename: "conf.json", | ||||||
|  | 					Regex:    ".*", | ||||||
|  | 				}, | ||||||
|  | 			}, | ||||||
|  | 		}, nil | ||||||
|  | 	} | ||||||
|  | 	d := &multiconfig.DefaultLoader{} | ||||||
|  | 	d.Loader = multiconfig.MultiLoader( | ||||||
|  | 		&multiconfig.TagLoader{}, | ||||||
|  | 		&multiconfig.JSONLoader{Path: path}, | ||||||
|  | 	) | ||||||
|  | 	d.Validator = multiconfig.MultiValidator(&multiconfig.RequiredValidator{}) | ||||||
|  | 	if err = d.Load(&metaConf); err != nil { | ||||||
|  | 		slog.Error("parse meta conf", "error", err) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	if err = d.Validate(&metaConf); err != nil { | ||||||
|  | 		slog.Error("validate meta conf", "error", err) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	return | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func parseConfFile(path string) (conf Conf, err error) { | func parseConfFile(path string) (conf Conf, err error) { | ||||||
| 	d := &multiconfig.DefaultLoader{} | 	d := &multiconfig.DefaultLoader{} | ||||||
| 	d.Loader = multiconfig.MultiLoader( | 	d.Loader = multiconfig.MultiLoader( | ||||||
|  | @ -79,6 +123,10 @@ func parseConfFile(path string) (conf Conf, err error) { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func commitMsgToConf() (conf Conf, err error) { | func commitMsgToConf() (conf Conf, err error) { | ||||||
|  | 	metaConf, err := parseMetaConfFile("meta-conf.json") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
| 	r, err := git.PlainOpen(".") | 	r, err := git.PlainOpen(".") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return | 		return | ||||||
|  | @ -93,7 +141,12 @@ func commitMsgToConf() (conf Conf, err error) { | ||||||
| 	} | 	} | ||||||
| 	msg := commit.Message | 	msg := commit.Message | ||||||
| 	slog.Debug("commit msg to conf", "msg", msg) | 	slog.Debug("commit msg to conf", "msg", msg) | ||||||
| 	// TODO: parse msg to conf name
 | 	for _, pattern := range metaConf.Patterns { | ||||||
| 	conf, err = parseConfFile("conf.json") | 		if matched, _ := regexp.MatchString(pattern.Regex, msg); matched { | ||||||
|  | 			slog.Debug("pattern matched", "pattern", pattern) | ||||||
|  | 			return parseConfFile(pattern.Filename) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	err = fmt.Errorf("no pattern matched") | ||||||
| 	return | 	return | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user