Skip to content

Conversation

@zph
Copy link
Owner

@zph zph commented Nov 2, 2025

Add pre-run hooks feature that executes scripts before main script execution. Hooks enable environment validation, dependency checking, authentication, and setup tasks.

Why: it solves for needing a couple lines as a preamble in many scripts in the main repo where I use this tool which sources a lib/common.sh for standard setup of require env vars and a few wrapper functions.

Implementation:

  • Hook discovery from .hooks.d directory with lexicographic ordering
  • Two hook types: executable (separate process) and sourced (same shell context)
  • Sourced hooks use .source suffix and can modify environment variables
  • Wrapper script generation for proper execution flow
  • --skip-hooks flag to bypass hook execution
  • Full environment variable injection (TOME_ROOT, TOME_SCRIPT_PATH, etc.)

Tests:

  • 38 total tests covering unit, integration, and E2E scenarios
  • Comprehensive validation of hook discovery, execution, and environment handling
  • Deno E2E tests verify real-world usage with both tome-cli and wrapper

Documentation:

  • Complete user guide in docs/hooks.md with examples and use cases
  • Updated README.md with hooks feature section
  • Example hooks in examples/.hooks.d for reference

TODO

  • Implement form of this that doesn't land a script on disk
  • Build the execution script based on templating rather than string concat

zph added 2 commits November 2, 2025 12:50
Add pre-run hooks feature that executes scripts before main script execution.
Hooks enable environment validation, dependency checking, authentication, and setup tasks.

Implementation:
- Hook discovery from .hooks.d directory with lexicographic ordering
- Two hook types: executable (separate process) and sourced (same shell context)
- Sourced hooks use .source suffix and can modify environment variables
- Wrapper script generation for proper execution flow
- --skip-hooks flag to bypass hook execution
- Full environment variable injection (TOME_ROOT, TOME_SCRIPT_PATH, etc.)

Tests:
- 38 total tests covering unit, integration, and E2E scenarios
- Comprehensive validation of hook discovery, execution, and environment handling
- Deno E2E tests verify real-world usage with both tome-cli and wrapper

Documentation:
- Complete user guide in docs/hooks.md with examples and use cases
- Updated README.md with hooks feature section
- Example hooks in examples/.hooks.d for reference

All tests passing. Feature ready for use.
…ility

This commit improves the hooks system with better shell compatibility,
proper argument escaping, and cleaner code organization.

Changes:

* Rename findBash() to findShell() and simplify implementation
  - Better name reflects that it finds bash OR sh
  - Remove hardcoded path checks for bash and sh
  - Use exec.LookPath("bash") with fallback to exec.LookPath("sh")
  - Use actual shell name (bash/sh) as argv[0] instead of hardcoding
  - More portable and idiomatic Go code

* Add proper shell escaping for paths and arguments
  - Integrate al.essio.dev/pkg/shellescape library
  - Quote all hook paths, script paths in wrapper template
  - Quote arguments only when necessary (e.g., spaces, special chars)
  - Prevents command injection and handles edge cases correctly

* Fix hardcoded /tmp paths in tests
  - Replace all hardcoded /tmp paths with t.TempDir()
  - Ensures proper test isolation and cross-platform compatibility
  - Updated: hooks_test.go, hooks_integration_test.go

* Add comprehensive sh compatibility tests
  - New TestShellCompatibility suite with 3 test cases
  - Verify wrapper scripts execute correctly with sh (not just bash)
  - Test sourced hooks work with sh
  - Validate POSIX-compliant syntax (no bash-specific features)
  - Add executeWrapperContentWithSh() helper function

* Add test for paths with spaces
  - Verify proper quoting of paths containing spaces
  - Test that simple args remain unquoted while complex args are quoted
  - Ensures robustness with real-world path scenarios

All tests passing:
- Unit tests: 9 test suites
- Integration tests: including new sh compatibility tests
- E2E tests: 25/25
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.

2 participants