88 "log/slog"
99 "os"
1010 "path/filepath"
11- goruntime "runtime"
12- "runtime/pprof"
1311 "sync"
1412 "time"
1513
@@ -22,6 +20,7 @@ import (
2220 "github.com/docker/docker-agent/pkg/cli"
2321 "github.com/docker/docker-agent/pkg/config"
2422 "github.com/docker/docker-agent/pkg/paths"
23+ "github.com/docker/docker-agent/pkg/profiling"
2524 "github.com/docker/docker-agent/pkg/runtime"
2625 "github.com/docker/docker-agent/pkg/session"
2726 "github.com/docker/docker-agent/pkg/sessiontitle"
@@ -145,37 +144,16 @@ func (f *runExecFlags) runRunCommand(cmd *cobra.Command, args []string) error {
145144func (f * runExecFlags ) runOrExec (ctx context.Context , out * cli.Printer , args []string , useTUI bool ) error {
146145 slog .Debug ("Starting agent" , "agent" , f .agentName )
147146
148- // Start CPU profiling if requested
149- if f .cpuProfile != "" {
150- pf , err := os .Create (f .cpuProfile )
151- if err != nil {
152- return fmt .Errorf ("failed to create CPU profile: %w" , err )
153- }
154- if err := pprof .StartCPUProfile (pf ); err != nil {
155- pf .Close ()
156- return fmt .Errorf ("failed to start CPU profile: %w" , err )
157- }
158- defer pprof .StopCPUProfile ()
159- defer pf .Close ()
160- slog .Info ("CPU profiling enabled" , "file" , f .cpuProfile )
161- }
162-
163- // Write memory profile at exit if requested
164- if f .memProfile != "" {
165- defer func () {
166- mf , err := os .Create (f .memProfile )
167- if err != nil {
168- slog .Error ("Failed to create memory profile" , "error" , err )
169- return
170- }
171- defer mf .Close ()
172- goruntime .GC () // Get up-to-date statistics
173- if err := pprof .WriteHeapProfile (mf ); err != nil {
174- slog .Error ("Failed to write memory profile" , "error" , err )
175- }
176- slog .Info ("Memory profile written" , "file" , f .memProfile )
177- }()
147+ // Start profiling if requested
148+ stopProfiling , err := profiling .Start (f .cpuProfile , f .memProfile )
149+ if err != nil {
150+ return err
178151 }
152+ defer func () {
153+ if err := stopProfiling (); err != nil {
154+ slog .Error ("Profiling cleanup failed" , "error" , err )
155+ }
156+ }()
179157
180158 var agentFileName string
181159 if len (args ) > 0 {
@@ -271,10 +249,6 @@ func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []s
271249 }
272250 defer initialTeamCleanup ()
273251
274- if useTUI {
275- applyTheme ()
276- }
277-
278252 if f .dryRun {
279253 out .Println ("Dry run mode enabled. Agent initialized but will not execute." )
280254 return nil
@@ -284,19 +258,13 @@ func (f *runExecFlags) runOrExec(ctx context.Context, out *cli.Printer, args []s
284258 return f .handleExecMode (ctx , out , rt , sess , args )
285259 }
286260
261+ applyTheme ()
287262 opts , err := f .buildAppOpts (args )
288263 if err != nil {
289264 return err
290265 }
291266
292- var sessStore session.Store
293- switch typedRt := rt .(type ) {
294- case * runtime.LocalRuntime :
295- sessStore = typedRt .SessionStore ()
296- case * runtime.PersistentRuntime :
297- sessStore = typedRt .SessionStore ()
298- }
299-
267+ sessStore := rt .SessionStore ()
300268 return runTUI (ctx , rt , sess , f .createSessionSpawner (agentSource , sessStore ), initialTeamCleanup , opts ... )
301269}
302270
0 commit comments