Skip to content

Commit 8c39b5b

Browse files
committed
align --format flag and UX with docker cli
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent bc568ee commit 8c39b5b

19 files changed

+83
-47
lines changed

cmd/compose/images.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import (
3535

3636
type imageOptions struct {
3737
*projectOptions
38-
Quiet bool
38+
Quiet bool
39+
Format string
3940
}
4041

4142
func imagesCommand(p *projectOptions, backend api.Service) *cobra.Command {
@@ -50,6 +51,7 @@ func imagesCommand(p *projectOptions, backend api.Service) *cobra.Command {
5051
}),
5152
ValidArgsFunction: completeServiceNames(p),
5253
}
54+
imgCmd.Flags().StringVar(&opts.Format, "format", "table", "Format the output. Values: [table | json].")
5355
imgCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
5456
return imgCmd
5557
}
@@ -88,7 +90,7 @@ func runImages(ctx context.Context, backend api.Service, opts imageOptions, serv
8890
return images[i].ContainerName < images[j].ContainerName
8991
})
9092

91-
return formatter.Print(images, formatter.PRETTY, os.Stdout,
93+
return formatter.Print(images, opts.Format, os.Stdout,
9294
func(w io.Writer) {
9395
for _, img := range images {
9496
id := stringid.TruncateID(img.ID)
@@ -104,5 +106,5 @@ func runImages(ctx context.Context, backend api.Service, opts imageOptions, serv
104106
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, repo, tag, id, size)
105107
}
106108
},
107-
"Container", "Repository", "Tag", "Image Id", "Size")
109+
"CONTAINER", "REPOSITORY", "TAG", "IMAGE ID", "SIZE")
108110
}

cmd/compose/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func listCommand(backend api.Service) *cobra.Command {
4949
Args: cobra.NoArgs,
5050
ValidArgsFunction: noCompletion(),
5151
}
52-
lsCmd.Flags().StringVar(&lsOpts.Format, "format", "pretty", "Format the output. Values: [pretty | json].")
52+
lsCmd.Flags().StringVar(&lsOpts.Format, "format", "table", "Format the output. Values: [table | json].")
5353
lsCmd.Flags().BoolVarP(&lsOpts.Quiet, "quiet", "q", false, "Only display IDs.")
5454
lsCmd.Flags().Var(&lsOpts.Filter, "filter", "Filter output based on conditions provided.")
5555
lsCmd.Flags().BoolVarP(&lsOpts.All, "all", "a", false, "Show all stopped Compose projects")

cmd/compose/ps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func psCommand(p *projectOptions, backend api.Service) *cobra.Command {
8383
ValidArgsFunction: completeServiceNames(p),
8484
}
8585
flags := psCmd.Flags()
86-
flags.StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json]")
86+
flags.StringVar(&opts.Format, "format", "table", "Format the output. Values: [table | json]")
8787
flags.StringVar(&opts.Filter, "filter", "", "Filter services by a property (supported filters: status).")
8888
flags.StringArrayVar(&opts.Status, "status", []string{}, "Filter services by status. Values: [paused | restarting | removing | running | dead | created | exited]")
8989
flags.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")

cmd/compose/ps_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ import (
2828
"github.com/stretchr/testify/assert"
2929
)
3030

31-
func TestPsPretty(t *testing.T) {
31+
func TestPsTable(t *testing.T) {
3232
ctx := context.Background()
3333
origStdout := os.Stdout
3434
t.Cleanup(func() {
3535
os.Stdout = origStdout
3636
})
3737
dir := t.TempDir()
38-
f, err := os.Create(filepath.Join(dir, "output.txt"))
38+
out := filepath.Join(dir, "output.txt")
39+
f, err := os.Create(out)
3940
if err != nil {
4041
t.Fatal("could not create output file")
4142
}
@@ -51,8 +52,9 @@ func TestPsPretty(t *testing.T) {
5152
DoAndReturn(func(ctx context.Context, projectName string, options api.PsOptions) ([]api.ContainerSummary, error) {
5253
return []api.ContainerSummary{
5354
{
54-
ID: "abc123",
55-
Name: "ABC",
55+
ID: "abc123",
56+
Name: "ABC",
57+
Image: "foo/bar",
5658
Publishers: api.PortPublishers{
5759
{
5860
TargetPort: 8080,
@@ -76,8 +78,7 @@ func TestPsPretty(t *testing.T) {
7678
_, err = f.Seek(0, 0)
7779
assert.NoError(t, err)
7880

79-
output := make([]byte, 256)
80-
_, err = f.Read(output)
81+
output, err := os.ReadFile(out)
8182
assert.NoError(t, err)
8283

8384
assert.Contains(t, string(output), "8080/tcp, 8443/tcp")

cmd/formatter/consts.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
package formatter
1818

1919
const (
20-
// JSON is the constant for Json formats on list commands
20+
// JSON Print in JSON format
2121
JSON = "json"
2222
// TemplateLegacyJSON the legacy json formatting value using go template
2323
TemplateLegacyJSON = "{{json.}}"
2424
// PRETTY is the constant for default formats on list commands
25+
// Deprecated: use TABLE
2526
PRETTY = "pretty"
27+
// TABLE Print output in table format with column headers (default)
28+
TABLE = "table"
2629
)

cmd/formatter/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
// Print prints formatted lists in different formats
3131
func Print(toJSON interface{}, format string, outWriter io.Writer, writerFn func(w io.Writer), headers ...string) error {
3232
switch strings.ToLower(format) {
33-
case PRETTY, "":
33+
case TABLE, PRETTY, "":
3434
return PrintPrettySection(outWriter, writerFn, headers...)
3535
case TemplateLegacyJSON:
3636
switch reflect.TypeOf(toJSON).Kind() {

docs/reference/compose_images.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ List images used by the created containers
77

88
| Name | Type | Default | Description |
99
| --- | --- | --- | --- |
10+
| `--format` | `string` | `table` | Format the output. Values: [table \| json]. |
1011
| `-q`, `--quiet` | | | Only display IDs |
1112

1213

docs/reference/compose_ls.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ List running compose projects
99
| --- | --- | --- | --- |
1010
| `-a`, `--all` | | | Show all stopped Compose projects |
1111
| `--filter` | `filter` | | Filter output based on conditions provided. |
12-
| `--format` | `string` | `pretty` | Format the output. Values: [pretty \| json]. |
12+
| `--format` | `string` | `table` | Format the output. Values: [table \| json]. |
1313
| `-q`, `--quiet` | | | Only display IDs. |
1414

1515

docs/reference/compose_ps.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ List containers
99
| --- | --- | --- | --- |
1010
| `-a`, `--all` | | | Show all stopped containers (including those created by the run command) |
1111
| [`--filter`](#filter) | `string` | | Filter services by a property (supported filters: status). |
12-
| [`--format`](#format) | `string` | `pretty` | Format the output. Values: [pretty \| json] |
12+
| [`--format`](#format) | `string` | `table` | Format the output. Values: [table \| json] |
1313
| `-q`, `--quiet` | | | Only display IDs |
1414
| `--services` | | | Display services |
1515
| [`--status`](#status) | `stringArray` | | Filter services by status. Values: [paused \| restarting \| removing \| running \| dead \| created \| exited] |

docs/reference/docker_compose_images.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ usage: docker compose images [OPTIONS] [SERVICE...]
55
pname: docker compose
66
plink: docker_compose.yaml
77
options:
8+
- option: format
9+
value_type: string
10+
default_value: table
11+
description: 'Format the output. Values: [table | json].'
12+
deprecated: false
13+
hidden: false
14+
experimental: false
15+
experimentalcli: false
16+
kubernetes: false
17+
swarm: false
818
- option: quiet
919
shorthand: q
1020
value_type: bool

0 commit comments

Comments
 (0)