Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions cmd/nerdctl/container/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (

"github.com/spf13/cobra"

"github.com/containerd/log"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
"github.com/containerd/nerdctl/v2/pkg/api/types"
Expand Down Expand Up @@ -108,13 +106,7 @@ func inspectAction(cmd *cobra.Command, args []string) error {
return err
}

// Display
if len(entries) > 0 {
if formatErr := formatter.FormatSlice(opt.Format, opt.Stdout, entries); formatErr != nil {
log.G(ctx).Error(formatErr)
}
}
return err
return formatter.FormatInspectSlice(opt.Format, opt.Stdout, entries)
}

func containerInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
10 changes: 1 addition & 9 deletions cmd/nerdctl/image/image_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (

"github.com/spf13/cobra"

"github.com/containerd/log"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
"github.com/containerd/nerdctl/v2/pkg/api/types"
Expand Down Expand Up @@ -110,13 +108,7 @@ func imageInspectAction(cmd *cobra.Command, args []string) error {
return err
}

// Display
if len(entries) > 0 {
if formatErr := formatter.FormatSlice(options.Format, options.Stdout, entries); formatErr != nil {
log.G(ctx).Error(formatErr)
}
}
return err
return formatter.FormatInspectSlice(options.Format, options.Stdout, entries)
}

func imageInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
8 changes: 1 addition & 7 deletions cmd/nerdctl/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (

"github.com/spf13/cobra"

"github.com/containerd/log"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
containercmd "github.com/containerd/nerdctl/v2/cmd/nerdctl/container"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
Expand Down Expand Up @@ -176,11 +174,7 @@ func inspectAction(cmd *cobra.Command, args []string) error {
return fmt.Errorf("%d errors: %v", len(errs), errs)
}

if formatErr := formatter.FormatSlice(format, cmd.OutOrStdout(), entries); formatErr != nil {
log.G(ctx).Error(formatErr)
}

return nil
return formatter.FormatInspectSlice(format, cmd.OutOrStdout(), entries)
}

func inspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
5 changes: 1 addition & 4 deletions pkg/cmd/network/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ func Inspect(ctx context.Context, client *containerd.Client, options types.Netwo
}

if len(result) > 0 {
if formatErr := formatter.FormatSlice(options.Format, options.Stdout, result); formatErr != nil {
log.G(ctx).Error(formatErr)
}
err = nil
err = formatter.FormatInspectSlice(options.Format, options.Stdout, result)
} else {
err = errors.New("unable to find any network matching the provided request")
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/formatter/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ func FormatSlice(format string, writer io.Writer, x []interface{}) error {
return nil
}

// FormatInspectSlice formats inspect results and propagates template errors back
// to the caller so CLI commands can fail with a non-zero exit code.
func FormatInspectSlice(format string, writer io.Writer, x []interface{}) error {
if len(x) == 0 {
return nil
}
return FormatSlice(format, writer, x)
}

func tryRawFormat(b *bytes.Buffer, f interface{}, tmpl *template.Template) error {
m, err := json.MarshalIndent(f, "", " ")
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions pkg/formatter/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package formatter

import (
"bytes"
"testing"
"time"

Expand Down Expand Up @@ -237,3 +238,37 @@ func TestEllipsis(t *testing.T) {
})
}
}

func TestFormatInspectSlice(t *testing.T) {
t.Parallel()

t.Run("empty slice is ignored", func(t *testing.T) {
t.Parallel()

var buf bytes.Buffer
err := FormatInspectSlice("{{.ID}}", &buf, nil)
assert.NilError(t, err)
assert.Equal(t, "", buf.String())
})

t.Run("malformed template returns error", func(t *testing.T) {
t.Parallel()

var buf bytes.Buffer
err := FormatInspectSlice("{{bad", &buf, []interface{}{
map[string]string{"ID": "abc"},
})
assert.ErrorContains(t, err, "template")
})

t.Run("default format still works", func(t *testing.T) {
t.Parallel()

var buf bytes.Buffer
err := FormatInspectSlice("", &buf, []interface{}{
map[string]string{"ID": "abc"},
})
assert.NilError(t, err)
assert.Assert(t, buf.Len() > 0)
})
}
Loading