@@ -114,47 +114,13 @@ func NewDockerClient() (*DockerClient, error) {
114114
115115// ExecContainer executes a command inside a running container by name and returns the combined stdout/stderr.
116116func (dc * DockerClient ) ExecContainer (containerName string , command []string ) (string , error ) {
117- L .Info ().Strs ("Command" , command ).Str ("ContainerName" , containerName ).Msg ("Executing command" )
118- ctx := context .Background ()
119- containers , err := dc .cli .ContainerList (ctx , container.ListOptions {
120- All : true ,
121- })
122- if err != nil {
123- return "" , fmt .Errorf ("failed to list containers: %w" , err )
124- }
125- var containerID string
126- for _ , cont := range containers {
127- for _ , name := range cont .Names {
128- if name == "/" + containerName {
129- containerID = cont .ID
130- break
131- }
132- }
133- }
134- if containerID == "" {
135- return "" , fmt .Errorf ("container with name '%s' not found" , containerName )
136- }
137-
138117 execConfig := container.ExecOptions {
139118 Cmd : command ,
140119 AttachStdout : true ,
141120 AttachStderr : true ,
142121 }
143- execID , err := dc .cli .ContainerExecCreate (ctx , containerID , execConfig )
144- if err != nil {
145- return "" , fmt .Errorf ("failed to create exec instance: %w" , err )
146- }
147- resp , err := dc .cli .ContainerExecAttach (ctx , execID .ID , container.ExecStartOptions {})
148- if err != nil {
149- return "" , fmt .Errorf ("failed to attach to exec instance: %w" , err )
150- }
151- defer resp .Close ()
152- output , err := io .ReadAll (resp .Reader )
153- if err != nil {
154- return "" , fmt .Errorf ("failed to read exec output: %w" , err )
155- }
156- L .Info ().Str ("Output" , string (output )).Msg ("Command output" )
157- return string (output ), nil
122+
123+ return dc .ExecContainerOptions (containerName , execConfig )
158124}
159125
160126// ExecContainer executes a command inside a running container by name and returns the combined stdout/stderr.
0 commit comments