Skip to content

fix(cli): require an initialized project before save/restore/history (#9)#42

Merged
kavix merged 1 commit into
kavix:mainfrom
AzarAI-TOP:fix/require-init-before-commands
Jun 15, 2026
Merged

fix(cli): require an initialized project before save/restore/history (#9)#42
kavix merged 1 commit into
kavix:mainfrom
AzarAI-TOP:fix/require-init-before-commands

Conversation

@AzarAI-TOP

Copy link
Copy Markdown
Contributor

Closes #9.

Running a command in a directory that hasn't been initialized currently fails in a confusing way:

  • eko save calls snapshot.CreateSnapshot() (which creates a .eko/snapshots/... tree) and then inserts into a snapshots table that doesn't exist yet — so it prints Snapshot saved: <id> but the snapshot is never actually recorded.
  • eko restore / eko history open .eko/db.sqlite for a project that isn't there and silently do nothing.

Change

Add a small shared guard, requireInitialized, wired in as a Cobra PreRunE on save, restore and history. If there's no .eko directory it fails early with a helpful message:

$ eko save
Error: not an eko project: no .eko directory found here
run 'eko init' to initialize eko in this directory
$ echo $?
1

init itself is unaffected, and the happy path (eko initeko saveeko history) works as before.

The root command now sets SilenceUsage/SilenceErrors so the guidance is printed exactly once (on stderr) instead of being duplicated and followed by a usage dump, which is Cobra's default for a PreRunE error.

Tests / verification

  • New cmd/project_test.go covers isInitialized (missing .eko, present .eko, and a plain file named .eko which must not count) and requireInitialized (helpful error when missing, nil when present), using t.Chdir + t.TempDir.
  • go test ./cmd/ passes; built the CLI with -tags no_gui and verified the behaviour above end to end.

I scoped the guard to the three data commands named in the issue. Happy to extend it or adjust the wording if you'd prefer.

🤖 Generated with Claude Code

)

Running `eko save`, `eko restore` or `eko history` outside an
initialized project gave a confusing experience: `save` silently
created a `.eko` tree but failed to record the snapshot (the table
doesn't exist yet), and `restore`/`history` opened a non-existent
database and did nothing useful.

Add a shared `requireInitialized` PreRunE guard that checks for the
`.eko` directory and, when missing, fails early with a clear message
pointing at `eko init`. Root command now silences Cobra's duplicate
error/usage output so the guidance is printed once, on stderr.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@kavix kavix merged commit 12170de into kavix:main Jun 15, 2026
1 check passed
@kavix kavix self-requested a review June 15, 2026 18:15
@AzarAI-TOP AzarAI-TOP deleted the fix/require-init-before-commands branch June 16, 2026 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI: Improve error message when .eko directory is missing

2 participants