refactor: move modules
This commit is contained in:
parent
6c3de223d1
commit
8ffa64de4a
|
@ -1,4 +1,4 @@
|
|||
package stage
|
||||
package conf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -8,6 +8,7 @@ import (
|
|||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/joint-online-judge/JOJ3/internal/stage"
|
||||
"github.com/koding/multiconfig"
|
||||
)
|
||||
|
@ -73,6 +74,23 @@ type ConventionalCommit struct {
|
|||
Footer string
|
||||
}
|
||||
|
||||
func GetCommitMsg() (msg string, 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
|
||||
}
|
||||
msg = commit.Message
|
||||
return
|
||||
}
|
||||
|
||||
func parseConventionalCommit(commit string) (*ConventionalCommit, error) {
|
||||
re := regexp.MustCompile(`(?s)^(\w+)(\(([^)]+)\))?!?: (.+?)(\n\n(.+?))?(\n\n(.+))?$`)
|
||||
matches := re.FindStringSubmatch(strings.TrimSpace(commit))
|
||||
|
@ -107,7 +125,7 @@ func parseConfFile(path string) (conf Conf, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func parseMsg(confRoot, confName, msg string) (conf Conf, group string, err error) {
|
||||
func ParseMsg(confRoot, confName, msg string) (conf Conf, group string, err error) {
|
||||
slog.Info("parse msg", "msg", msg)
|
||||
conventionalCommit, err := parseConventionalCommit(msg)
|
||||
if err != nil {
|
||||
|
@ -142,7 +160,7 @@ func parseMsg(confRoot, confName, msg string) (conf Conf, group string, err erro
|
|||
return
|
||||
}
|
||||
|
||||
func listValidScopes(confRoot, confName, msg string) ([]string, error) {
|
||||
func ListValidScopes(confRoot, confName, msg string) ([]string, error) {
|
||||
conventionalCommit, err := parseConventionalCommit(msg)
|
||||
if err != nil {
|
||||
return []string{}, err
|
|
@ -1,4 +1,4 @@
|
|||
package stage
|
||||
package conf
|
||||
|
||||
import (
|
||||
"reflect"
|
|
@ -1,4 +1,4 @@
|
|||
package stage
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
|
@ -1,19 +1,71 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/joint-online-judge/JOJ3/cmd/joj3/conf"
|
||||
"github.com/joint-online-judge/JOJ3/cmd/joj3/stage"
|
||||
"github.com/joint-online-judge/JOJ3/cmd/joj3/teapot"
|
||||
)
|
||||
|
||||
var (
|
||||
confRoot string
|
||||
confName string
|
||||
msg string
|
||||
showVersion *bool
|
||||
Version string = "debug"
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&confRoot, "conf-root", ".", "root path for all config files")
|
||||
flag.StringVar(&confName, "conf-name", "conf.json", "filename for config files")
|
||||
flag.StringVar(&msg, "msg", "", "message to trigger the running, leave empty to use git commit message on HEAD")
|
||||
showVersion = flag.Bool("version", false, "print current version")
|
||||
}
|
||||
|
||||
func main() {
|
||||
// TODO: call stage-runner
|
||||
if err := stage.Run(); err != nil {
|
||||
slog.Error("stage runner error", "error", err)
|
||||
if err := setupSlog(""); err != nil { // before conf is loaded
|
||||
slog.Error("setup slog", "error", err)
|
||||
return
|
||||
}
|
||||
// TODO: call joint-teapot
|
||||
if err := teapot.Run(); err != nil {
|
||||
slog.Error("teapot caller error", "error", err)
|
||||
flag.Parse()
|
||||
if *showVersion {
|
||||
fmt.Println(Version)
|
||||
return
|
||||
}
|
||||
slog.Info("start joj3", "version", Version)
|
||||
if msg == "" {
|
||||
var err error
|
||||
msg, err = conf.GetCommitMsg()
|
||||
if err != nil {
|
||||
slog.Error("get commit msg", "error", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
confObj, group, err := conf.ParseMsg(confRoot, confName, msg)
|
||||
if err != nil {
|
||||
slog.Error("parse msg", "error", err)
|
||||
validScopes, scopeErr := conf.ListValidScopes(
|
||||
confRoot, confName, msg)
|
||||
if scopeErr != nil {
|
||||
slog.Error("list valid scopes", "error", scopeErr)
|
||||
return
|
||||
}
|
||||
slog.Info("hint: valid scopes in commit message", "scopes", validScopes)
|
||||
return
|
||||
}
|
||||
if err := setupSlog(confObj.LogPath); err != nil { // after conf is loaded
|
||||
slog.Error("setup slog", "error", err)
|
||||
return
|
||||
}
|
||||
if err := stage.Run(confObj, group); err != nil {
|
||||
slog.Error("stage run", "error", err)
|
||||
return
|
||||
}
|
||||
if err := teapot.Run(confObj); err != nil {
|
||||
slog.Error("teapot run", "error", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package stage
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
@ -63,7 +63,7 @@ func readStageResults(t *testing.T, path string) []stage.StageResult {
|
|||
|
||||
func TestRun(t *testing.T) {
|
||||
var tests []string
|
||||
root := "../../../tmp/submodules/JOJ3-examples/examples/"
|
||||
root := "../../tmp/submodules/JOJ3-examples/examples/"
|
||||
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -105,9 +105,7 @@ func TestRun(t *testing.T) {
|
|||
os.Args = []string{"./joj3"}
|
||||
outputFile := "joj3_result.json"
|
||||
defer os.Remove(outputFile)
|
||||
if err := Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
main()
|
||||
stageResults := readStageResults(t, outputFile)
|
||||
regex := true
|
||||
expectedFile := "expected_regex.json"
|
|
@ -2,37 +2,18 @@ package stage
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"github.com/joint-online-judge/JOJ3/cmd/joj3/conf"
|
||||
"github.com/joint-online-judge/JOJ3/internal/executors"
|
||||
_ "github.com/joint-online-judge/JOJ3/internal/parsers"
|
||||
"github.com/joint-online-judge/JOJ3/internal/stage"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/jinzhu/copier"
|
||||
)
|
||||
|
||||
func getCommitMsg() (msg string, 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
|
||||
}
|
||||
msg = commit.Message
|
||||
return
|
||||
}
|
||||
|
||||
func generateStages(conf Conf, group string) ([]stage.Stage, error) {
|
||||
func generateStages(conf conf.Conf, group string) ([]stage.Stage, error) {
|
||||
stages := []stage.Stage{}
|
||||
existNames := map[string]bool{}
|
||||
for _, s := range conf.Stages {
|
||||
|
@ -92,54 +73,7 @@ func outputResult(outputPath string, results []stage.StageResult) error {
|
|||
append(content, []byte("\n")...), 0o600)
|
||||
}
|
||||
|
||||
var (
|
||||
confRoot string
|
||||
confName string
|
||||
msg string
|
||||
showVersion *bool
|
||||
Version string = "debug"
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&confRoot, "conf-root", ".", "root path for all config files")
|
||||
flag.StringVar(&confName, "conf-name", "conf.json", "filename for config files")
|
||||
flag.StringVar(&msg, "msg", "", "message to trigger the running, leave empty to use git commit message on HEAD")
|
||||
showVersion = flag.Bool("version", false, "print current version")
|
||||
}
|
||||
|
||||
func Run() error {
|
||||
if err := setupSlog(""); err != nil { // before conf is loaded
|
||||
return err
|
||||
}
|
||||
flag.Parse()
|
||||
if *showVersion {
|
||||
fmt.Println(Version)
|
||||
return nil
|
||||
}
|
||||
slog.Info("start joj3", "version", Version)
|
||||
if msg == "" {
|
||||
var err error
|
||||
msg, err = getCommitMsg()
|
||||
if err != nil {
|
||||
slog.Error("get commit msg", "error", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
conf, group, err := parseMsg(confRoot, confName, msg)
|
||||
if err != nil {
|
||||
slog.Error("parse msg", "error", err)
|
||||
validScopes, scopeErr := listValidScopes(
|
||||
confRoot, confName, msg)
|
||||
if scopeErr != nil {
|
||||
slog.Error("list valid scopes", "error", scopeErr)
|
||||
return scopeErr
|
||||
}
|
||||
slog.Info("hint: valid scopes in commit message", "scopes", validScopes)
|
||||
return err
|
||||
}
|
||||
if err := setupSlog(conf.LogPath); err != nil { // after conf is loaded
|
||||
return err
|
||||
}
|
||||
func Run(conf conf.Conf, group string) error {
|
||||
executors.InitWithConf(conf.SandboxExecServer, conf.SandboxToken)
|
||||
stages, err := generateStages(conf, group)
|
||||
if err != nil {
|
||||
|
|
|
@ -3,9 +3,11 @@ package teapot
|
|||
import (
|
||||
"log/slog"
|
||||
"os/exec"
|
||||
|
||||
"github.com/joint-online-judge/JOJ3/cmd/joj3/conf"
|
||||
)
|
||||
|
||||
func Run() error {
|
||||
func Run(conf conf.Conf) error {
|
||||
// TODO: call teapot
|
||||
cmd := exec.Command("joint-teapot", "--help")
|
||||
output, err := cmd.CombinedOutput()
|
||||
|
|
Loading…
Reference in New Issue
Block a user