From 0e87ad89434d83e5f25a2efd15c716eae986e52a Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Tue, 5 Mar 2024 04:31:50 -0500 Subject: [PATCH] fix: parse config --- _example/simple/.gitignore | 1 + _example/simple/{1.stdin => cases/1.in} | 0 _example/simple/{1.stdout => cases/1.out} | 0 _example/simple/{2.stdin => cases/2.in} | 0 _example/simple/{2.stdout => cases/2.out} | 0 _example/simple/conf.toml | 30 +++++++++-------------- cmd/joj3/conf.go | 7 ++++++ cmd/joj3/main.go | 20 ++++++++++----- 8 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 _example/simple/.gitignore rename _example/simple/{1.stdin => cases/1.in} (100%) rename _example/simple/{1.stdout => cases/1.out} (100%) rename _example/simple/{2.stdin => cases/2.in} (100%) rename _example/simple/{2.stdout => cases/2.out} (100%) diff --git a/_example/simple/.gitignore b/_example/simple/.gitignore new file mode 100644 index 0000000..6aa6b80 --- /dev/null +++ b/_example/simple/.gitignore @@ -0,0 +1 @@ +!*.out diff --git a/_example/simple/1.stdin b/_example/simple/cases/1.in similarity index 100% rename from _example/simple/1.stdin rename to _example/simple/cases/1.in diff --git a/_example/simple/1.stdout b/_example/simple/cases/1.out similarity index 100% rename from _example/simple/1.stdout rename to _example/simple/cases/1.out diff --git a/_example/simple/2.stdin b/_example/simple/cases/2.in similarity index 100% rename from _example/simple/2.stdin rename to _example/simple/cases/2.in diff --git a/_example/simple/2.stdout b/_example/simple/cases/2.out similarity index 100% rename from _example/simple/2.stdout rename to _example/simple/cases/2.out diff --git a/_example/simple/conf.toml b/_example/simple/conf.toml index e70ab47..ea5052f 100644 --- a/_example/simple/conf.toml +++ b/_example/simple/conf.toml @@ -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" diff --git a/cmd/joj3/conf.go b/cmd/joj3/conf.go index aca580c..06cacfa 100644 --- a/cmd/joj3/conf.go +++ b/cmd/joj3/conf.go @@ -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 diff --git a/cmd/joj3/main.go b/cmd/joj3/main.go index 8e68b62..cf3eac1 100644 --- a/cmd/joj3/main.go +++ b/cmd/joj3/main.go @@ -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{