diff --git a/CHANGELOG.md b/CHANGELOG.md index a173ed3..7ea3d7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Let `crawlctl logs -- JOB` display logs for jobs whose names look like command-line flags. + - Keep CLI-installed schedules tied to the selected config and log directory when `--config` is relative. - Honor `--` when extracting crawlctl global flags so flag-like job names remain literal selections instead of changing CLI behavior. diff --git a/README.md b/README.md index 8119754..59db57f 100644 --- a/README.md +++ b/README.md @@ -144,8 +144,8 @@ 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. +Use `crawlctl run -- JOB` or `crawlctl logs -- JOB` for job names that look like +flags. Options before `--` retain their normal behavior; names after it stay literal. The `install` command resolves relative `--config` paths before creating a schedule, so the scheduled run uses the same config and log directory from another working directory. diff --git a/cmd/crawlctl/main.go b/cmd/crawlctl/main.go index 65a5c62..14f27b8 100644 --- a/cmd/crawlctl/main.go +++ b/cmd/crawlctl/main.go @@ -326,8 +326,18 @@ func latestRecordForJob(history []scheduler.RunRecord, name string) (scheduler.R func (a *app) runLogs(args []string) error { tail := 80 job := "" +parseArgs: for i := 0; i < len(args); i++ { switch args[i] { + case "--": + remaining := args[i+1:] + if len(remaining) > 1 || (job != "" && len(remaining) > 0) { + return usageError{fmt.Errorf("logs accepts at most one job")} + } + if len(remaining) == 1 { + job = remaining[0] + } + break parseArgs case "--tail": if i+1 >= len(args) { return usageError{fmt.Errorf("--tail requires a line count")} diff --git a/cmd/crawlctl/main_test.go b/cmd/crawlctl/main_test.go index a54baed..4f68797 100644 --- a/cmd/crawlctl/main_test.go +++ b/cmd/crawlctl/main_test.go @@ -444,3 +444,61 @@ func TestInstallPreservesSymlinkParentConfigPath(t *testing.T) { t.Fatalf("schedule selected a different config: %s", plan.Content) } } + +func TestLogsSelectsFlagLikeJobNames(t *testing.T) { + for _, name := range []string{"plain", "--version", "--json", "--config=literal", "--tail=1", "--"} { + t.Run(name, func(t *testing.T) { + configPath := filepath.Join(t.TempDir(), "crawlctl.toml") + paths, err := scheduler.Save(configPath, scheduler.DefaultConfig(), false) + if err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(paths.StateDir, 0o700); err != nil { + t.Fatal(err) + } + var history bytes.Buffer + for _, record := range []scheduler.RunRecord{ + {Job: name, LogPath: filepath.Join(paths.BaseDir, "selected.log")}, + {Job: "other", LogPath: filepath.Join(paths.BaseDir, "other.log")}, + } { + if err := os.WriteFile(record.LogPath, []byte(record.Job+" first\n"+record.Job+" last\n"), 0o600); err != nil { + t.Fatal(err) + } + if err := json.NewEncoder(&history).Encode(record); err != nil { + t.Fatal(err) + } + } + if err := os.WriteFile(paths.History, history.Bytes(), 0o600); err != nil { + t.Fatal(err) + } + var stdout bytes.Buffer + a := app{stdout: &stdout, stderr: ioDiscard{}} + args := []string{"--config", configPath, "logs", "--tail", "1", "--", name} + if err := a.run(t.Context(), args); err != nil { + t.Fatal(err) + } + if want := name + " last\n"; stdout.String() != want { + t.Fatalf("logs = %q, want %q", stdout.String(), want) + } + stdout.Reset() + if err := a.run(t.Context(), []string{"--config", configPath, "logs", "--tail=1", "--"}); err != nil { + t.Fatal(err) + } + if stdout.String() != "other last\n" { + t.Fatalf("latest logs = %q", stdout.String()) + } + }) + } +} + +func TestLogsRejectsMultipleJobsAcrossSeparator(t *testing.T) { + for _, args := range [][]string{{"first", "--", "second"}, {"--", "first", "second"}} { + var stdout bytes.Buffer + a := app{stdout: &stdout, stderr: ioDiscard{}, config: filepath.Join(t.TempDir(), "missing.toml")} + err := a.runLogs(args) + var usage usageError + if !errors.As(err, &usage) || !strings.Contains(err.Error(), "at most one job") || stdout.Len() != 0 { + t.Fatalf("logs(%q): err = %v, stdout = %q", args, err, stdout.String()) + } + } +}