[Prototype] Implement pre-run hooks with .hooks.d support#15
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Tests:
Documentation:
TODO