From 1d479733bcbeb630557cd07f721d4510e989934c Mon Sep 17 00:00:00 2001 From: Flu0r1ne Date: Sun, 19 Sep 2021 00:27:55 -0500 Subject: Add list command, copy for bash command, and ability to run non-directory commands anywhere --- cmd/planr/main.go | 30 +++++++++++++++++++----------- cmd/planr/sub/cli.go | 14 ++++++++++++++ cmd/planr/sub/evaluate.go | 28 ++++++++++++++++++++++------ cmd/planr/sub/list.go | 13 +++++++++++++ 4 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 cmd/planr/sub/list.go (limited to 'cmd') diff --git a/cmd/planr/main.go b/cmd/planr/main.go index 2243203..e2ed19f 100644 --- a/cmd/planr/main.go +++ b/cmd/planr/main.go @@ -80,29 +80,37 @@ func main() { dieUsage() } + subcommand := flag.Arg(0) + subargs := flag.Args()[1:] + + switch subcommand { + case "version": + fmt.Printf("%s\n", planr.VERSION) + os.Exit(0) + case "help", "-h", "-help", "--help": + printUsage(os.Stdout) + os.Exit(0) + case "build", "evaluate", "eval", "clean", "config", "list": + break + default: + fmt.Fprintf(os.Stderr, "unrecognized command %s\n", subcommand) + dieUsage() + } + runner := getConfiguredRunner() cfg := planr.DecodeConfig(runner.ConfigDir()) - subcommand := flag.Arg(0) - subargs := flag.Args()[1:] - switch subcommand { - case "version": - fmt.Printf("%s\n", planr.VERSION) case "build": sub.Build(runner, subargs, cfg) case "evaluate", "eval": sub.Evaluate(runner, subargs, cfg) + case "list": + sub.List(runner, subargs, cfg) case "clean": sub.Clean(runner, subargs) case "config": sub.Config(runner, subargs) - case "help", "-h", "-help", "--help": - printUsage(os.Stdout) - default: - fmt.Fprintf(os.Stderr, "unrecognized command %s\n", subcommand) - dieUsage() } - } diff --git a/cmd/planr/sub/cli.go b/cmd/planr/sub/cli.go index 5defa96..9e1099c 100644 --- a/cmd/planr/sub/cli.go +++ b/cmd/planr/sub/cli.go @@ -82,6 +82,20 @@ func (opt PrintOpts) HasFlag(flag PrintOpts) bool { return (opt & flag) == flag } +func tcPrint(tc planr.TestCase) { + title := "" + + if tc.Config.Title != "" { + title = tc.Config.Title + } else { + title = tc.Cname + } + + col_title.Println(title) + pprintLabeled("id", tc.Cname) + fmt.Println() +} + func tcPprint(tr planr.TestResult, opt PrintOpts) { tcStatusLine(tr) diff --git a/cmd/planr/sub/evaluate.go b/cmd/planr/sub/evaluate.go index 4f5e4e1..3f1718f 100644 --- a/cmd/planr/sub/evaluate.go +++ b/cmd/planr/sub/evaluate.go @@ -2,9 +2,10 @@ package sub import ( "encoding/json" + "flag" "fmt" + "io/ioutil" "log" - "flag" "golang.flu0r1ne.net/planr" ) @@ -30,26 +31,39 @@ func prettyPrint(results gradingResults, verbose, summarize bool) { } } -func jsonPrint(results gradingResults) { +func jsonPrint(results gradingResults, file string) { res, err := json.Marshal(results) if err != nil { log.Fatalf("Error printing JSON: %v\n", err) } - fmt.Println(string(res)) + if file == "" { + fmt.Println(string(res)) + return + } + + + if err := ioutil.WriteFile(file, res, 0664); err != nil { + log.Fatalf("Could not write JSON file %v", err) + } } func Evaluate(runner planr.Runner, params []string, cfg *planr.Config) { f := flag.NewFlagSet("evaluate", flag.ExitOnError) jsonOutput := f.Bool("json", false, "print json output") + jsonFile := f.String("json_to_file", "", "export json to a file") extra := f.Bool("extra", false, "print extra grading information") dieIncompatibleVersion(cfg) f.Parse(params) + if *jsonFile != "" && *jsonOutput { + log.Fatalf("-json and -json_to_file are mutually exclusive") + } + tcs := runner.CollectCases() // Filter those tests which patch IDs in params @@ -80,9 +94,11 @@ func Evaluate(runner planr.Runner, params []string, cfg *planr.Config) { Score: planr.Score(trs), } - if *jsonOutput { - jsonPrint(results) - } else { + if *jsonOutput || *jsonFile != "" { + jsonPrint(results, *jsonFile) + } + + if ! *jsonOutput { prettyPrint(results, *extra, summarizeScore) } } diff --git a/cmd/planr/sub/list.go b/cmd/planr/sub/list.go new file mode 100644 index 0000000..911a7e6 --- /dev/null +++ b/cmd/planr/sub/list.go @@ -0,0 +1,13 @@ +package sub + +import ( + "golang.flu0r1ne.net/planr" +) + +func List(runner planr.Runner, params []string, cfg * planr.Config) { + tcs := runner.CollectCases() + + for _, tc := range tcs { + tcPrint(tc) + } +} -- cgit v1.2.3