Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions cmd/crawlctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")}
Expand Down
58 changes: 58 additions & 0 deletions cmd/crawlctl/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
Loading