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

- Reject malformed `crawlctl logs --tail` counts instead of silently accepting an integer prefix.

- Save discovered executables as absolute paths so relative app selections work from another runner directory.

- Preserve each job's latest text status when history keys collide or runs finish in the same second.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ applies private file permissions before replacing existing content.
| `discover` | Print discovered crawl apps |
| `run` | Run enabled refresh jobs |
| `status` | Show the latest recorded job status |
| `logs` | Print recent job logs |
| `logs` | Print recent job logs (`--tail` requires a positive integer) |
| `install` | Install or render a periodic schedule |
| `uninstall` | Remove an installed periodic schedule |

Expand Down
5 changes: 3 additions & 2 deletions cmd/crawlctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"runtime/debug"
"sort"
"strconv"
"strings"

"github.com/openclaw/crawlkit/scheduler"
Expand Down Expand Up @@ -368,8 +369,8 @@ func (a *app) runLogs(args []string) error {
}

func parseTail(value string) (int, error) {
var parsed int
if _, err := fmt.Sscanf(value, "%d", &parsed); err != nil {
parsed, err := strconv.Atoi(value)
if err != nil {
return 0, fmt.Errorf("invalid --tail value %q", value)
}
if parsed <= 0 {
Expand Down
56 changes: 50 additions & 6 deletions cmd/crawlctl/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,56 @@ every = "7m"
}

func TestRunLogsRejectsInvalidTail(t *testing.T) {
var stdout bytes.Buffer
a := app{stdout: &stdout, stderr: ioDiscard{}}
err := a.runLogs([]string{"--tail=-1"})
var usage usageError
if !errors.As(err, &usage) {
t.Fatalf("err = %v, want usageError", err)
for _, value := range []string{"-1", "0", "", "bad", "2junk", "1.5", "1e3", "2 3", " 2", "2\n"} {
for _, joined := range []bool{false, true} {
t.Run(fmt.Sprintf("%q/joined=%t", value, joined), func(t *testing.T) {
args := []string{"--tail", value}
if joined {
args = []string{"--tail=" + value}
}
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) || exitCode(err) != 2 || stdout.Len() != 0 {
t.Fatalf("err = %v, stdout = %q; want usage error without output", err, stdout.String())
}
})
}
}
}

func TestRunLogsHonorsTailCount(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)
}
logPath := filepath.Join(paths.BaseDir, "job.log")
if err := os.WriteFile(logPath, []byte("first\nsecond\nthird\n"), 0o600); err != nil {
t.Fatal(err)
}
history, err := json.Marshal(scheduler.RunRecord{Job: "sync", LogPath: logPath})
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(paths.History, append(history, '\n'), 0o600); err != nil {
t.Fatal(err)
}
for _, args := range [][]string{{"--tail", "2"}, {"--tail=2"}, {"--tail=+2"}, {"--tail=02"}} {
t.Run(strings.Join(args, " "), func(t *testing.T) {
var stdout bytes.Buffer
a := app{stdout: &stdout, stderr: ioDiscard{}, config: configPath}
if err := a.runLogs(args); err != nil {
t.Fatal(err)
}
if stdout.String() != "second\nthird\n" {
t.Fatalf("tail = %q", stdout.String())
}
})
}
}

Expand Down
Loading