fix: parse config

This commit is contained in:
张泊明518370910136 2024-03-05 04:31:50 -05:00
parent 5c15388882
commit 0e87ad8943
GPG Key ID: D47306D7062CDA9D
8 changed files with 34 additions and 24 deletions

1
_example/simple/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
!*.out

View File

@ -1,10 +1,10 @@
logLevel = 8
logLevel = 0
[[stages]]
name = "compile"
[stages.executor]
name = "sandbox"
[stages.executor.with.default]
args = ["/usr/bin/g++", "a.cc", "-o", "a"]
args = ["g++", "a.cc", "-o", "a"]
env = ["PATH=/usr/bin:/bin"]
cpuLimit = 10_000_000_000
memoryLimit = 104_857_600
@ -36,31 +36,25 @@ cpuLimit = 10_000_000_000
memoryLimit = 104_857_600
procLimit = 50
copyOut = ["stdout", "stderr"]
[stages.executor.with.default.stdout]
name = "stdout"
max = 4_096
[stages.executor.with.default.stderr]
name = "stderr"
max = 4_096
[stages.executor.with.default.copyInCached]
a = "a"
[[stages.executor.with.cases]]
[stages.executor.with.cases.stdin]
src = "1.stdin"
[stages.executor.with.cases.stdout]
name = "stdout"
max = 4_096
[stages.executor.with.cases.stderr]
name = "stderr"
max = 4_096
src = "./cases/1.in"
[[stages.executor.with.cases]]
[stages.executor.with.cases.stdin]
src = "2.stdin"
[stages.executor.with.cases.stdout]
name = "stdout"
max = 4_096
[stages.executor.with.cases.stderr]
name = "stderr"
max = 4_096
src = "./cases/2.in"
[stages.parser]
name = "diff"
[[stages.parser.with.cases]]
score = 100
stdoutPath = "1.stdout"
stdoutPath = "./cases/1.out"
[[stages.parser.with.cases]]
score = 100
stdoutPath = "2.stdout"
stdoutPath = "./cases/2.out"

View File

@ -21,6 +21,13 @@ type Conf struct {
}
}
func DefaultConf() Conf {
return Conf{
LogLevel: 0,
OutputPath: "joj3_result.json",
}
}
type OptionalCmd struct {
Args *[]string
Env *[]string

View File

@ -21,11 +21,7 @@ func parseConfFile(tomlPath *string) Conf {
slog.Error("read toml config", "error", err)
os.Exit(1)
}
// fill in default value of config file
conf := Conf{
LogLevel: 0,
OutputPath: "joj3_result.json",
}
conf := DefaultConf()
err = toml.Unmarshal(tomlConfig, &conf)
if err != nil {
slog.Error("parse stages config", "error", err)
@ -47,6 +43,7 @@ func generateStages(conf Conf) []stage.Stage {
stages := []stage.Stage{}
for _, s := range conf.Stages {
var cmds []stage.Cmd
defaultCmd := s.Executor.With.Default
for _, optionalCmd := range s.Executor.With.Cases {
cmd := s.Executor.With.Default
err := copier.Copy(&cmd, &optionalCmd)
@ -54,10 +51,21 @@ func generateStages(conf Conf) []stage.Stage {
slog.Error("generate stages", "error", err)
os.Exit(1)
}
// since these 3 values are pointers, copier will always copy
// them, so we need to check them manually
if defaultCmd.Stdin != nil && optionalCmd.Stdin == nil {
cmd.Stdin = defaultCmd.Stdin
}
if defaultCmd.Stdout != nil && optionalCmd.Stdout == nil {
cmd.Stdout = defaultCmd.Stdout
}
if defaultCmd.Stderr != nil && optionalCmd.Stderr == nil {
cmd.Stderr = defaultCmd.Stderr
}
cmds = append(cmds, cmd)
}
if len(s.Executor.With.Cases) == 0 {
cmds = append(cmds, s.Executor.With.Default)
cmds = []stage.Cmd{defaultCmd}
}
slog.Info("parse stages config", "cmds", cmds)
stages = append(stages, stage.Stage{