diff --git a/CHANGELOG.md b/CHANGELOG.md index d323e2d..a173ed3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- 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. - Report a failed job when all configured `{repo}` values are blank instead of silently succeeding without running anything. diff --git a/README.md b/README.md index d7cfaed..8119754 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,9 @@ applies private file permissions before replacing existing content. Use `crawlctl run -- JOB` for job names that look like global flags. Global flags 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. + 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 f5c38eb..65a5c62 100644 --- a/cmd/crawlctl/main.go +++ b/cmd/crawlctl/main.go @@ -395,13 +395,34 @@ func (a *app) runInstall(args []string) error { if fs.NArg() != 0 { return usageError{fmt.Errorf("install takes flags only")} } - paths, err := scheduler.DefaultPaths(a.config) + configPath := a.config + paths, err := scheduler.DefaultPaths(configPath) if err != nil { return err } + // Scheduled commands may start outside the caller's working directory. + if !filepath.IsAbs(paths.ConfigPath) { + if filepath.Separator == '/' { + cwd, err := os.Getwd() + if err != nil { + return err + } + // Cleaning Unix components can change symlink/.. traversal. + configPath = strings.TrimSuffix(cwd, "/") + "/" + paths.ConfigPath + } else { + configPath, err = filepath.Abs(paths.ConfigPath) + if err != nil { + return err + } + } + paths, err = scheduler.DefaultPaths(configPath) + if err != nil { + return err + } + } installEvery := *every if strings.TrimSpace(installEvery) == "" { - cfg, loadedPaths, err := scheduler.Load(a.config) + cfg, loadedPaths, err := scheduler.Load(configPath) if err != nil && !errors.Is(err, os.ErrNotExist) { return err } diff --git a/cmd/crawlctl/main_test.go b/cmd/crawlctl/main_test.go index 8e31a7e..a54baed 100644 --- a/cmd/crawlctl/main_test.go +++ b/cmd/crawlctl/main_test.go @@ -10,6 +10,7 @@ import ( "path/filepath" "runtime" "runtime/debug" + "strconv" "strings" "testing" @@ -359,3 +360,87 @@ func TestRunHonorsFlagSeparatorForJobNames(t *testing.T) { } } } + +func TestInstallKeepsRelativeConfigAndLogPaths(t *testing.T) { + t.Chdir(t.TempDir()) + relativeConfig := filepath.Join("config space", "crawlctl.toml") + absoluteConfig, err := filepath.Abs(relativeConfig) + if err != nil { + t.Fatal(err) + } + cfg := scheduler.DefaultConfig() + cfg.Runner.Every = "12m" + paths, err := scheduler.Save(absoluteConfig, cfg, false) + if err != nil { + t.Fatal(err) + } + for _, backend := range []string{"cron", "launchd", "systemd", "windows"} { + for _, override := range []bool{false, true} { + t.Run(fmt.Sprintf("%s/override=%t", backend, override), func(t *testing.T) { + args := []string{"--backend", backend, "--dry-run"} + if override { + args = append(args, "--every", "5m") + } + var stdout bytes.Buffer + a := app{stdout: &stdout, stderr: ioDiscard{}, config: relativeConfig, json: true} + if err := a.runInstall(args); err != nil { + t.Fatal(err) + } + var plan scheduler.InstallPlan + if err := json.Unmarshal(stdout.Bytes(), &plan); err != nil { + t.Fatal(err) + } + content := plan.Content + strings.Join(plan.Command, " ") + wantConfig := absoluteConfig + if backend == "systemd" { + wantConfig = strconv.Quote(wantConfig) + } + if !strings.Contains(content, wantConfig) { + t.Fatalf("scheduled config no longer names the selected file: %s", content) + } + if (backend == "cron" || backend == "launchd") && !strings.Contains(content, paths.LogDir) { + t.Fatalf("scheduled logs are relative to a different working directory: %s", content) + } + }) + } + } +} + +func TestInstallPreservesSymlinkParentConfigPath(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Unix symlink parent traversal semantics") + } + dir := t.TempDir() + t.Chdir(dir) + if err := os.MkdirAll(filepath.Join(dir, "target", "nested"), 0o700); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(dir, "target", "nested"), filepath.Join(dir, "link")); err != nil { + t.Fatal(err) + } + cfg := scheduler.DefaultConfig() + cfg.Runner.Every = "12m" + if _, err := scheduler.Save(filepath.Join(dir, "target", "crawlctl.toml"), cfg, false); err != nil { + t.Fatal(err) + } + cfg.Runner.Every = "5m" + if _, err := scheduler.Save(filepath.Join(dir, "crawlctl.toml"), cfg, false); err != nil { + t.Fatal(err) + } + var stdout bytes.Buffer + a := app{stdout: &stdout, stderr: ioDiscard{}, config: "link/../crawlctl.toml", json: true} + if err := a.runInstall([]string{"--backend", "cron", "--dry-run"}); err != nil { + t.Fatal(err) + } + var plan scheduler.InstallPlan + if err := json.Unmarshal(stdout.Bytes(), &plan); err != nil { + t.Fatal(err) + } + cwd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + if !strings.HasPrefix(plan.Content, "*/12 ") || !strings.Contains(plan.Content, "'"+cwd+"/link/../crawlctl.toml'") { + t.Fatalf("schedule selected a different config: %s", plan.Content) + } +}