Commit bb4f689
authored
[Prototype] Implement pre-run hooks with .hooks.d support (#15)
* Implement pre-run hooks with .hooks.d support
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.
* Refactor hooks implementation with proper quoting and improved portability
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/251 parent b5d315d commit bb4f689
File tree
13 files changed
+2534
-3
lines changed- cmd
- docs
- examples
- .hooks.d
- test
13 files changed
+2534
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
176 | 176 | | |
177 | 177 | | |
178 | 178 | | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
179 | 199 | | |
180 | 200 | | |
181 | 201 | | |
| |||
200 | 220 | | |
201 | 221 | | |
202 | 222 | | |
| 223 | + | |
203 | 224 | | |
204 | 225 | | |
205 | 226 | | |
206 | | - | |
207 | 227 | | |
208 | 228 | | |
209 | 229 | | |
| |||
318 | 338 | | |
319 | 339 | | |
320 | 340 | | |
| 341 | + | |
321 | 342 | | |
322 | 343 | | |
323 | 344 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
83 | | - | |
84 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
85 | 126 | | |
86 | 127 | | |
87 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
88 | 145 | | |
89 | 146 | | |
90 | 147 | | |
| |||
128 | 185 | | |
129 | 186 | | |
130 | 187 | | |
| 188 | + | |
131 | 189 | | |
132 | 190 | | |
133 | 191 | | |
| 192 | + | |
134 | 193 | | |
| 194 | + | |
135 | 195 | | |
136 | 196 | | |
0 commit comments