@@ -11,55 +11,41 @@ import (
1111// Each string is treated as a shell command to run.
1212// Empty strings are silently skipped.
1313func HooksFromCLI (preToolUse , postToolUse , sessionStart , sessionEnd , onUserInput []string ) * latest.HooksConfig {
14- hooks := & latest.HooksConfig {}
15-
16- if len (preToolUse ) > 0 {
17- var defs []latest.HookDefinition
18- for _ , cmd := range preToolUse {
19- if strings .TrimSpace (cmd ) == "" {
20- continue
21- }
22- defs = append (defs , latest.HookDefinition {Type : "command" , Command : cmd })
23- }
24- if len (defs ) > 0 {
25- hooks .PreToolUse = []latest.HookMatcherConfig {{Hooks : defs }}
26- }
14+ hooks := & latest.HooksConfig {
15+ PreToolUse : matcherFromCommands (preToolUse ),
16+ PostToolUse : matcherFromCommands (postToolUse ),
17+ SessionStart : defsFromCommands (sessionStart ),
18+ SessionEnd : defsFromCommands (sessionEnd ),
19+ OnUserInput : defsFromCommands (onUserInput ),
2720 }
2821
29- if len (postToolUse ) > 0 {
30- var defs []latest.HookDefinition
31- for _ , cmd := range postToolUse {
32- if strings .TrimSpace (cmd ) == "" {
33- continue
34- }
35- defs = append (defs , latest.HookDefinition {Type : "command" , Command : cmd })
36- }
37- if len (defs ) > 0 {
38- hooks .PostToolUse = []latest.HookMatcherConfig {{Hooks : defs }}
39- }
22+ if hooks .IsEmpty () {
23+ return nil
4024 }
25+ return hooks
26+ }
4127
42- for _ , cmd := range sessionStart {
43- if strings .TrimSpace (cmd ) != "" {
44- hooks .SessionStart = append (hooks .SessionStart , latest.HookDefinition {Type : "command" , Command : cmd })
45- }
46- }
47- for _ , cmd := range sessionEnd {
48- if strings .TrimSpace (cmd ) != "" {
49- hooks .SessionEnd = append (hooks .SessionEnd , latest.HookDefinition {Type : "command" , Command : cmd })
50- }
51- }
52- for _ , cmd := range onUserInput {
53- if strings .TrimSpace (cmd ) != "" {
54- hooks .OnUserInput = append (hooks .OnUserInput , latest.HookDefinition {Type : "command" , Command : cmd })
28+ // defsFromCommands turns a list of CLI shell commands into hook definitions,
29+ // skipping any blank entries.
30+ func defsFromCommands (cmds []string ) []latest.HookDefinition {
31+ var defs []latest.HookDefinition
32+ for _ , cmd := range cmds {
33+ if strings .TrimSpace (cmd ) == "" {
34+ continue
5535 }
36+ defs = append (defs , latest.HookDefinition {Type : "command" , Command : cmd })
5637 }
38+ return defs
39+ }
5740
58- if hooks .IsEmpty () {
41+ // matcherFromCommands wraps the result of defsFromCommands in a single
42+ // HookMatcherConfig so the commands apply to all tools (empty matcher).
43+ func matcherFromCommands (cmds []string ) []latest.HookMatcherConfig {
44+ defs := defsFromCommands (cmds )
45+ if len (defs ) == 0 {
5946 return nil
6047 }
61-
62- return hooks
48+ return []latest.HookMatcherConfig {{Hooks : defs }}
6349}
6450
6551// MergeHooks merges CLI hooks into an existing HooksConfig.
0 commit comments