@@ -4,8 +4,10 @@ import (
44 "context"
55 "encoding/json"
66 "fmt"
7+ "os"
78
89 "github.com/charmbracelet/bubbles/table"
10+ "github.com/charmbracelet/lipgloss"
911 "github.com/docker/docker/api/types"
1012 "github.com/docker/docker/client"
1113 "github.com/sourcegraph/sourcegraph/lib/errors"
@@ -51,34 +53,58 @@ func renderDockerUsageTable(ctx context.Context, cfg *Config, containers []types
5153 return errors .Wrap (err , "failed to get container info" )
5254 }
5355
54- stats , err := cfg .dockerClient .ContainerStats (ctx , container .ID , false )
55- if err != nil {
56- return errors .Wrap (err , "could not get container stats" )
57- }
58- defer stats .Body .Close ()
59-
60- var usage types.StatsJSON
61- if err := json .NewDecoder (stats .Body ).Decode (& usage ); err != nil {
62- return errors .Wrap (err , "could not decode container stats" )
56+ if cfg .container != "" {
57+ if containerInfo .Name == cfg .container {
58+ row := makeDockerUsageRow (ctx , cfg , containerInfo )
59+ rows = append (rows , row )
60+ break
61+ } else {
62+ continue
63+ }
6364 }
6465
65- row := makeDockerUsageRow (usage , containerInfo )
66+ row := makeDockerUsageRow (ctx , cfg , containerInfo )
6667 rows = append (rows , row )
6768 }
6869
70+ if len (rows ) == 0 {
71+ msg := lipgloss .NewStyle ().Foreground (lipgloss .Color ("#FFA500" ))
72+ if cfg .container == "" {
73+ fmt .Println (msg .Render (`No docker containers are running.` ))
74+ os .Exit (1 )
75+ }
76+ fmt .Println (msg .Render (
77+ fmt .Sprintf (`No container with name '%s' running.` , cfg .container ),
78+ ))
79+ os .Exit (1 )
80+ }
81+
6982 style .ResourceTable (columns , rows )
7083 return nil
7184}
7285
7386// makeDockerUsageRow generates a table row displaying CPU and memory usage for a Docker container.
74- func makeDockerUsageRow (containerUsage types.StatsJSON , containerInfo types.ContainerJSON ) table.Row {
75- cpuCores := float64 (containerInfo .HostConfig .NanoCPUs )
76- memory := float64 (containerInfo .HostConfig .Memory )
77- cpuUsage := float64 (containerUsage .CPUStats .CPUUsage .TotalUsage )
78- memoryUsage := float64 (containerUsage .MemoryStats .Usage )
87+ func makeDockerUsageRow (ctx context.Context , cfg * Config , container types.ContainerJSON ) table.Row {
88+ stats , err := cfg .dockerClient .ContainerStats (ctx , container .ID , false )
89+ if err != nil {
90+ errors .Wrap (err , "could not get container stats" )
91+ os .Exit (1 )
92+ }
93+ defer func () { _ = stats .Body .Close () }()
94+
95+ var usage types.StatsJSON
96+ if err := json .NewDecoder (stats .Body ).Decode (& usage ); err != nil {
97+ errors .Wrap (err , "could not get container stats" )
98+ os .Exit (1 )
99+ }
100+
101+ cpuCores := float64 (container .HostConfig .NanoCPUs )
102+ memory := float64 (container .HostConfig .Memory )
103+ cpuUsage := float64 (usage .CPUStats .CPUUsage .TotalUsage )
104+ memoryUsage := float64 (usage .MemoryStats .Usage )
79105
80106 return table.Row {
81- containerInfo .Name ,
107+ container .Name ,
82108 fmt .Sprintf ("%.2f" , cpuCores / 1_000_000_000 ),
83109 fmt .Sprintf ("%.2f%%" , getPercentage (cpuUsage , cpuCores )),
84110 fmt .Sprintf ("%.2fG" , memory / 1_000_000_000 ),
0 commit comments