cli/command/inspect: use errors.Join
Use stdlib multi-errors instead of creating our own Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
7147e85f63
commit
f431f61568
@ -6,13 +6,13 @@ package inspect
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/docker/cli/cli"
|
"github.com/docker/cli/cli"
|
||||||
"github.com/docker/cli/templates"
|
"github.com/docker/cli/templates"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ func NewTemplateInspectorFromString(out io.Writer, tmplStr string) (Inspector, e
|
|||||||
|
|
||||||
tmpl, err := templates.Parse(tmplStr)
|
tmpl, err := templates.Parse(tmplStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Errorf("template parsing error: %s", err)
|
return nil, fmt.Errorf("template parsing error: %w", err)
|
||||||
}
|
}
|
||||||
return NewTemplateInspector(out, tmpl), nil
|
return NewTemplateInspector(out, tmpl), nil
|
||||||
}
|
}
|
||||||
@ -70,16 +70,16 @@ func Inspect(out io.Writer, references []string, tmplStr string, getRef GetRefFu
|
|||||||
return cli.StatusError{StatusCode: 64, Status: err.Error()}
|
return cli.StatusError{StatusCode: 64, Status: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
var inspectErrs []string
|
var errs []error
|
||||||
for _, ref := range references {
|
for _, ref := range references {
|
||||||
element, raw, err := getRef(ref)
|
element, raw, err := getRef(ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
inspectErrs = append(inspectErrs, err.Error())
|
errs = append(errs, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := inspector.Inspect(element, raw); err != nil {
|
if err := inspector.Inspect(element, raw); err != nil {
|
||||||
inspectErrs = append(inspectErrs, err.Error())
|
errs = append(errs, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,10 +87,10 @@ func Inspect(out io.Writer, references []string, tmplStr string, getRef GetRefFu
|
|||||||
logrus.Error(err)
|
logrus.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(inspectErrs) != 0 {
|
if err := errors.Join(errs...); err != nil {
|
||||||
return cli.StatusError{
|
return cli.StatusError{
|
||||||
StatusCode: 1,
|
StatusCode: 1,
|
||||||
Status: strings.Join(inspectErrs, "\n"),
|
Status: err.Error(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -103,7 +103,7 @@ func (i *TemplateInspector) Inspect(typedElement any, rawElement []byte) error {
|
|||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
if err := i.tmpl.Execute(buffer, typedElement); err != nil {
|
if err := i.tmpl.Execute(buffer, typedElement); err != nil {
|
||||||
if rawElement == nil {
|
if rawElement == nil {
|
||||||
return errors.Errorf("template parsing error: %v", err)
|
return fmt.Errorf("template parsing error: %w", err)
|
||||||
}
|
}
|
||||||
return i.tryRawInspectFallback(rawElement)
|
return i.tryRawInspectFallback(rawElement)
|
||||||
}
|
}
|
||||||
@ -121,13 +121,13 @@ func (i *TemplateInspector) tryRawInspectFallback(rawElement []byte) error {
|
|||||||
dec := json.NewDecoder(rdr)
|
dec := json.NewDecoder(rdr)
|
||||||
dec.UseNumber()
|
dec.UseNumber()
|
||||||
|
|
||||||
if rawErr := dec.Decode(&raw); rawErr != nil {
|
if err := dec.Decode(&raw); err != nil {
|
||||||
return errors.Errorf("unable to read inspect data: %v", rawErr)
|
return fmt.Errorf("unable to read inspect data: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tmplMissingKey := i.tmpl.Option("missingkey=error")
|
tmplMissingKey := i.tmpl.Option("missingkey=error")
|
||||||
if rawErr := tmplMissingKey.Execute(buffer, raw); rawErr != nil {
|
if err := tmplMissingKey.Execute(buffer, raw); err != nil {
|
||||||
return errors.Errorf("template parsing error: %v", rawErr)
|
return fmt.Errorf("template parsing error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
i.buffer.Write(buffer.Bytes())
|
i.buffer.Write(buffer.Bytes())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user