Skip to content

Commit 52802d4

Browse files
committed
tidy a few small functions
1 parent d00ac11 commit 52802d4

3 files changed

Lines changed: 43 additions & 60 deletions

File tree

pkg/config/hooks.go

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,41 @@ import (
1111
// Each string is treated as a shell command to run.
1212
// Empty strings are silently skipped.
1313
func 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.

pkg/hooks/handler.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"slices"
13+
"strings"
1314
"sync"
1415

1516
"github.com/docker/docker-agent/pkg/shellpath"
@@ -155,23 +156,22 @@ func hookEnv(base []string, overrides map[string]string) []string {
155156
if len(env) == 0 {
156157
env = os.Environ()
157158
}
159+
160+
// Map each existing key to its position so overrides can replace in place.
158161
index := make(map[string]int, len(env))
159162
for i, entry := range env {
160-
for j, ch := range entry {
161-
if ch == '=' {
162-
index[entry[:j]] = i
163-
break
164-
}
163+
if key, _, ok := strings.Cut(entry, "="); ok {
164+
index[key] = i
165165
}
166166
}
167+
167168
for key, value := range overrides {
168169
entry := key + "=" + value
169170
if i, ok := index[key]; ok {
170171
env[i] = entry
171-
continue
172+
} else {
173+
env = append(env, entry)
172174
}
173-
index[key] = len(env)
174-
env = append(env, entry)
175175
}
176176
return env
177177
}

pkg/tui/service/supervisor/supervisor.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ func (s *Supervisor) handleRuntimeEvent(sessionID string, msg tea.Msg) {
167167
case *runtime.StreamStoppedEvent:
168168
runner.IsRunning = false
169169
runner.PendingEvent = nil // Clear any pending attention event since stream ended
170-
if runner.NeedsAttn {
171-
runner.NeedsAttn = false
172-
}
170+
runner.NeedsAttn = false
173171
s.notifyTabsUpdated()
174172

175173
case *runtime.SessionTitleEvent:
@@ -350,14 +348,14 @@ func (s *Supervisor) Spawner() SessionSpawner {
350348
}
351349

352350
// CloseSession closes a session and removes it from the supervisor.
353-
func (s *Supervisor) CloseSession(sessionID string) (nextActiveID string) {
351+
func (s *Supervisor) CloseSession(sessionID string) string {
354352
s.mu.Lock()
355353

356354
runner, ok := s.runners[sessionID]
357355
if !ok {
358-
nextActiveID = s.activeID
356+
activeID := s.activeID
359357
s.mu.Unlock()
360-
return nextActiveID
358+
return activeID
361359
}
362360

363361
// Cancel the session context
@@ -379,24 +377,23 @@ func (s *Supervisor) CloseSession(sessionID string) (nextActiveID string) {
379377
// If this was the active session, switch to the previous tab (or the
380378
// first one when closing the first tab).
381379
if s.activeID == sessionID {
382-
if len(s.order) > 0 {
383-
prevIdx := max(closedIdx-1, 0)
384-
s.activeID = s.order[prevIdx]
385-
} else {
380+
if len(s.order) == 0 {
386381
s.activeID = ""
382+
} else {
383+
s.activeID = s.order[max(closedIdx-1, 0)]
387384
}
388385
}
389386

390387
s.notifyTabsUpdated()
391-
nextActiveID = s.activeID
388+
activeID := s.activeID
392389
s.mu.Unlock()
393390

394391
// Run cleanup outside the lock so it can't deadlock.
395392
if cleanup != nil {
396393
go cleanup()
397394
}
398395

399-
return nextActiveID
396+
return activeID
400397
}
401398

402399
// Count returns the number of sessions.

0 commit comments

Comments
 (0)