From d6d557bf0ba0d5ab0f796b87b9597eb699dcf9c1 Mon Sep 17 00:00:00 2001 From: Alexander Gornak Date: Thu, 6 Jun 2024 20:40:11 +0300 Subject: [PATCH] perf: casper: logs into ejudge once; read ids from stdin (#29) --- README.md | 19 +++++++++---------- cmd/casper/main.go | 42 +++++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 82f546a..82e9d5b 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ blanka -i 51013 -t 51000 -e ## boban *Filter Ejudge runs.* -### Abount +### About Filter and print Ejudge runs ids. @@ -149,18 +149,16 @@ boban -i 50014 -c 10000 2> /dev/null | wc -l ![boban logo](https://algolymp.ru/static/img/boban.png) ## casper -*Change Ejudge contest visibility by id.* +*Change Ejudge contest visibility by ids.* ### About -- Make contest visible; -- Make contest invisible. +Read contest ids from `stdin` and make them invisible or visible. -Useful with bash `for` loop at the end of the year. +Useful with bash `seq`. Logs into Ejuge only once since `v0.14.2`. ### Flags -- `-i` - contest id (required) -- `-s` - make contest visible (invisible if not set) +- `-m` - invisible or visible (required, `hide|show`) ### Config - `ejudge.url` @@ -170,9 +168,9 @@ Useful with bash `for` loop at the end of the year. ### Examples ```bash casper --help -casper -i 41014 -casper -i 41014 -s -for i in {41014..41023}; do casper -i $i; done +echo 41014 | casper -m hide +seq 40301 40315 | casper -m show +casper -m hide # read from stdin ``` ![casper logo](https://algolymp.ru/static/img/casper.png) @@ -250,6 +248,7 @@ No config needed. ### Examples ```bash +fara --help fara -f /home/judges/048025/conf/serve.cfg -q .score_system,virtual,contest_time fara -f /home/judges/048025/conf/serve.cfg -q @problem.id,short_name,long_name fara -f /home/judges/049013/conf/serve.cfg -q @problem.use_stdin,use_stdout -d diff --git a/cmd/casper/main.go b/cmd/casper/main.go index 76f9b3b..07bd74f 100644 --- a/cmd/casper/main.go +++ b/cmd/casper/main.go @@ -1,6 +1,9 @@ package main import ( + "errors" + "fmt" + "io" "os" "github.com/Gornak40/algolymp/config" @@ -9,15 +12,16 @@ import ( "github.com/sirupsen/logrus" ) +const ( + makeInvisible = "hide" + makeVisible = "show" +) + func main() { - parser := argparse.NewParser("casper", "Change Ejudge contest visibility.") - cID := parser.Int("i", "cid", &argparse.Options{ + parser := argparse.NewParser("casper", "Change Ejudge contests visibility (stdin input).") + mode := parser.Selector("m", "mode", []string{makeInvisible, makeVisible}, &argparse.Options{ Required: true, - Help: "Ejudge contest ID", - }) - visible := parser.Flag("s", "show", &argparse.Options{ - Required: false, - Help: "Make contest visible (invisible if flag is not set)", + Help: "Invisible or visible", }) if err := parser.Parse(os.Args); err != nil { logrus.WithError(err).Fatal("bad arguments") @@ -31,14 +35,26 @@ func main() { logrus.WithError(err).Fatal("login failed") } - if *visible { - err = ejClient.MakeVisible(sid, *cID) - } else { - err = ejClient.MakeInvisible(sid, *cID) + var casperFunc func(string, int) error + switch *mode { + case makeVisible: + casperFunc = ejClient.MakeVisible + case makeInvisible: + casperFunc = ejClient.MakeInvisible + default: + logrus.WithField("mode", *mode).Fatal("unknown mode") } - if err != nil { - logrus.WithError(err).Fatal("change visible status failed") + logrus.Info("waiting for contest ids input...") + for { + var cid int + _, err := fmt.Scan(&cid) + if errors.Is(err, io.EOF) { + break + } + if err := casperFunc(sid, cid); err != nil { + logrus.WithError(err).Fatal("change visible status failed") + } } if err := ejClient.Logout(sid); err != nil {