55 "fmt"
66 "io"
77 "path/filepath"
8- "regexp"
98 "sort"
109 "strconv"
1110 "strings"
@@ -176,37 +175,15 @@ func NewRemoteClient(f map[string][]string, host Host) (Client, error) {
176175 return NewClient (cli , filterArgs , host ), nil
177176}
178177
178+ // Finds a container by id, skipping the filters
179179func (d * httpClient ) FindContainer (id string ) (Container , error ) {
180180 log .Debugf ("finding container with id: %s" , id )
181- var container Container
182- containers , err := d .ListContainers ()
183- if err != nil {
184- return container , err
185- }
186-
187- found := false
188- for _ , c := range containers {
189- if c .ID == id {
190- container = c
191- found = true
192- break
193- }
194- }
195- if ! found {
196- return container , fmt .Errorf ("unable to find container with id: %s" , id )
197- }
198-
199- if json , err := d .cli .ContainerInspect (context .Background (), container .ID ); err == nil {
200- container .Tty = json .Config .Tty
201- if startedAt , err := time .Parse (time .RFC3339Nano , json .State .StartedAt ); err == nil {
202- utc := startedAt .UTC ()
203- container .StartedAt = & utc
204- }
181+ if json , err := d .cli .ContainerInspect (context .Background (), id ); err == nil {
182+ return newContainerFromJSON (json , d .host .ID ), nil
205183 } else {
206- return container , err
184+ return Container {} , err
207185 }
208186
209- return container , nil
210187}
211188
212189func (d * httpClient ) ContainerActions (action ContainerAction , containerID string ) error {
@@ -223,6 +200,7 @@ func (d *httpClient) ContainerActions(action ContainerAction, containerID string
223200}
224201
225202func (d * httpClient ) ListContainers () ([]Container , error ) {
203+ log .Debugf ("listing containers with filters: %v" , d .filters )
226204 containerListOptions := container.ListOptions {
227205 Filters : d .filters ,
228206 All : true ,
@@ -234,33 +212,7 @@ func (d *httpClient) ListContainers() ([]Container, error) {
234212
235213 var containers = make ([]Container , 0 , len (list ))
236214 for _ , c := range list {
237- name := "no name"
238- if len (c .Names ) > 0 {
239- name = strings .TrimPrefix (c .Names [0 ], "/" )
240- }
241-
242- group := ""
243- if c .Labels ["dev.dozzle.group" ] != "" {
244- group = c .Labels ["dev.dozzle.group" ]
245- }
246-
247- container := Container {
248- ID : c .ID [:12 ],
249- Names : c .Names ,
250- Name : name ,
251- Image : c .Image ,
252- ImageID : c .ImageID ,
253- Command : c .Command ,
254- Created : time .Unix (c .Created , 0 ),
255- State : c .State ,
256- Status : c .Status ,
257- Host : d .host .ID ,
258- Health : findBetweenParentheses (c .Status ),
259- Labels : c .Labels ,
260- Stats : utils.NewRingBuffer [ContainerStat ](300 ), // 300 seconds of stats
261- Group : group ,
262- }
263- containers = append (containers , container )
215+ containers = append (containers , newContainer (c , d .host .ID ))
264216 }
265217
266218 sort .Slice (containers , func (i , j int ) bool {
@@ -398,11 +350,69 @@ func (d *httpClient) SystemInfo() system.Info {
398350 return d .info
399351}
400352
401- var PARENTHESIS_RE = regexp .MustCompile (`\(([a-zA-Z]+)\)` )
353+ func newContainer (c types.Container , host string ) Container {
354+ name := "no name"
355+ if len (c .Names ) > 0 {
356+ name = strings .TrimPrefix (c .Names [0 ], "/" )
357+ }
402358
403- func findBetweenParentheses (s string ) string {
404- if results := PARENTHESIS_RE .FindStringSubmatch (s ); results != nil {
405- return results [1 ]
359+ group := ""
360+ if c .Labels ["dev.dozzle.group" ] != "" {
361+ group = c .Labels ["dev.dozzle.group" ]
362+ }
363+ return Container {
364+ ID : c .ID [:12 ],
365+ Name : name ,
366+ Image : c .Image ,
367+ ImageID : c .ImageID ,
368+ Command : c .Command ,
369+ Created : time .Unix (c .Created , 0 ),
370+ State : c .State ,
371+ Host : host ,
372+ Labels : c .Labels ,
373+ Stats : utils.NewRingBuffer [ContainerStat ](300 ), // 300 seconds of stats
374+ Group : group ,
406375 }
407- return ""
376+ }
377+
378+ func newContainerFromJSON (c types.ContainerJSON , host string ) Container {
379+ name := "no name"
380+ if len (c .Name ) > 0 {
381+ name = strings .TrimPrefix (c .Name , "/" )
382+ }
383+
384+ group := ""
385+ if c .Config .Labels ["dev.dozzle.group" ] != "" {
386+ group = c .Config .Labels ["dev.dozzle.group" ]
387+ }
388+
389+ container := Container {
390+ ID : c .ID [:12 ],
391+ Name : name ,
392+ Image : c .Image ,
393+ ImageID : c .Image ,
394+ Command : strings .Join (c .Config .Entrypoint , " " ) + " " + strings .Join (c .Config .Cmd , " " ),
395+ State : c .State .Status ,
396+ Host : host ,
397+ Labels : c .Config .Labels ,
398+ Stats : utils.NewRingBuffer [ContainerStat ](300 ), // 300 seconds of stats
399+ Group : group ,
400+ Tty : c .Config .Tty ,
401+ }
402+
403+ if startedAt , err := time .Parse (time .RFC3339Nano , c .State .StartedAt ); err == nil {
404+ utc := startedAt .UTC ()
405+ container .StartedAt = & utc
406+ }
407+
408+ if createdAt , err := time .Parse (time .RFC3339Nano , c .Created ); err == nil {
409+ utc := createdAt .UTC ()
410+ container .Created = utc
411+ }
412+
413+ if c .State .Health != nil {
414+ container .Health = strings .ToLower (c .State .Health .Status )
415+ }
416+
417+ return container
408418}
0 commit comments