MCPcopy Create free account
hub / github.com/containerd/nerdctl / formatAndPrintContainerInfo

Function formatAndPrintContainerInfo

cmd/nerdctl/container/container_list.go:157–235  ·  view source on GitHub ↗
(containers []container.ListItem, options FormattingAndPrintingOptions)

Source from the content-addressed store, hash-verified

155}
156
157func formatAndPrintContainerInfo(containers []container.ListItem, options FormattingAndPrintingOptions) error {
158 w := options.Stdout
159 var (
160 wide bool
161 tmpl *template.Template
162 )
163 switch options.Format {
164 case "", "table":
165 w = tabwriter.NewWriter(w, 4, 8, 4, ' ', 0)
166 if !options.Quiet {
167 printHeader := "CONTAINER ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS\tNAMES"
168 if options.Size {
169 printHeader += "\tSIZE"
170 }
171 fmt.Fprintln(w, printHeader)
172 }
173 case "raw":
174 return errors.New("unsupported format: \"raw\"")
175 case "wide":
176 w = tabwriter.NewWriter(w, 4, 8, 4, ' ', 0)
177 if !options.Quiet {
178 fmt.Fprintln(w, "CONTAINER ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS\tNAMES\tRUNTIME\tPLATFORM\tSIZE")
179 wide = true
180 }
181 default:
182 if options.Quiet {
183 return errors.New("format and quiet must not be specified together")
184 }
185 var err error
186 tmpl, err = formatter.ParseTemplate(options.Format)
187 if err != nil {
188 return err
189 }
190 }
191
192 for _, c := range containers {
193 if tmpl != nil {
194 var b bytes.Buffer
195 if err := tmpl.Execute(&b, &c); err != nil {
196 return err
197 }
198 if _, err := fmt.Fprintln(w, b.String()); err != nil {
199 return err
200 }
201 } else if options.Quiet {
202 if _, err := fmt.Fprintln(w, c.ID); err != nil {
203 return err
204 }
205 } else {
206 format := "%s\t%s\t%s\t%s\t%s\t%s\t%s"
207 args := []interface{}{
208 c.ID,
209 c.Image,
210 c.Command,
211 formatter.TimeSinceInHuman(c.CreatedAt),
212 c.Status,
213 c.Ports,
214 c.Names,

Callers 1

psActionFunction · 0.85

Calls 4

ParseTemplateFunction · 0.92
TimeSinceInHumanFunction · 0.92
FlushMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…