From ee7a4207fc5b89304346a194a8739ab20c95dc5c Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Tue, 17 Nov 2026 07:13:48 +0800 Subject: [PATCH] fix(crawlctl): honor flag separators during global parsing --- CHANGELOG.md | 2 ++ README.md | 3 +++ cmd/crawlctl/main.go | 4 ++++ cmd/crawlctl/main_test.go | 49 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8312374..d323e2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Honor `--` when extracting crawlctl global flags so flag-like job names remain literal selections instead of changing CLI behavior. + - Report a failed job when all configured `{repo}` values are blank instead of silently succeeding without running anything. - Reject Windows Task Scheduler intervals above 1439 minutes during planning instead of producing unsupported minute-mode commands. diff --git a/README.md b/README.md index 462d102..d7cfaed 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,9 @@ applies private file permissions before replacing existing content. | `install` | Install or render a periodic schedule | | `uninstall` | Remove an installed periodic schedule | +Use `crawlctl run -- JOB` for job names that look like global flags. Global flags +before `--` retain their normal behavior; names after it stay literal. + Scheduling uses launchd on macOS, systemd user units on Linux, Task Scheduler on Windows, and cron rendering as the portable fallback. Cron requires whole-minute intervals below one hour that divide 60 evenly, such as `5m`, `12m`, or `30m`; other intervals are rejected because minute steps restart each hour. Launchd and systemd plans require whole-second intervals of at least one minute. diff --git a/cmd/crawlctl/main.go b/cmd/crawlctl/main.go index 0bcbca9..f5c38eb 100644 --- a/cmd/crawlctl/main.go +++ b/cmd/crawlctl/main.go @@ -121,6 +121,10 @@ func hoistGlobalFlags(args []string) []string { rest := make([]string, 0, len(args)) for i := 0; i < len(args); i++ { arg := args[i] + if arg == "--" { + rest = append(rest, args[i:]...) + break + } switch { case arg == "--json" || arg == "--version": out = append(out, arg) diff --git a/cmd/crawlctl/main_test.go b/cmd/crawlctl/main_test.go index fab583c..8e31a7e 100644 --- a/cmd/crawlctl/main_test.go +++ b/cmd/crawlctl/main_test.go @@ -310,3 +310,52 @@ func TestRunJobsRejectsBlankRepositories(t *testing.T) { }) } } + +func TestRunHonorsFlagSeparatorForJobNames(t *testing.T) { + exe, err := os.Executable() + if err != nil { + t.Fatal(err) + } + for _, name := range []string{"ordinary", "--version", "--json", "--config=literal"} { + for _, jsonOutput := range []bool{false, true} { + t.Run(fmt.Sprintf("%s/json=%t", name, jsonOutput), func(t *testing.T) { + configPath := filepath.Join(t.TempDir(), "crawlctl.toml") + jobName := name + if strings.HasPrefix(name, "--config=") { + jobName = "--config=" + filepath.Join(t.TempDir(), "missing.toml") + } + cfg := scheduler.DefaultConfig() + for _, job := range []string{jobName, "other"} { + cfg.Jobs[job] = scheduler.Job{Enabled: true, Command: []string{exe, "-test.run=^$"}} + } + paths, err := scheduler.Save(configPath, cfg, false) + if err != nil { + t.Fatal(err) + } + args := []string{"--config", configPath, "run"} + if jsonOutput { + args = append(args, "--json") + } + args = append(args, "--", jobName) + var stdout bytes.Buffer + a := app{stdout: &stdout, stderr: ioDiscard{}} + if err := a.run(t.Context(), args); err != nil { + t.Fatal(err) + } + history, err := scheduler.ReadHistory(paths.History) + if err != nil || len(history) != 1 || history[0].Job != jobName || history[0].Status != "success" { + t.Fatalf("selected job not run exclusively: %+v, %v", history, err) + } + if a.json != jsonOutput { + t.Fatalf("JSON mode = %t, want %t", a.json, jsonOutput) + } + if jsonOutput { + var records []scheduler.RunRecord + if err := json.Unmarshal(stdout.Bytes(), &records); err != nil || len(records) != 1 || records[0].Job != jobName { + t.Fatalf("JSON output = %q, %v", stdout.String(), err) + } + } + }) + } + } +}