Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: make inspect output an array #1119

Merged
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
2 changes: 1 addition & 1 deletion cli/image_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (i *ImageInspectCommand) runInspect(args []string) error {
return apiClient.ImageInspect(ctx, ref)
}

return inspect.MultiInspect(os.Stdout, args, i.format, getRefFunc)
return inspect.Inspect(os.Stdout, args, i.format, getRefFunc)
}

// example shows examples in inspect command, and is used in auto-generated cli docs.
Expand Down
60 changes: 31 additions & 29 deletions cli/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,40 @@ func (p *InspectCommand) runInspect(args []string) error {
return apiClient.ContainerGet(ctx, ref)
}

return inspect.MultiInspect(os.Stdout, args, p.format, getRefFunc)
return inspect.Inspect(os.Stdout, args, p.format, getRefFunc)
}

// inspectExample shows examples in inspect command, and is used in auto-generated cli docs.
func inspectExample() string {
return `$ pouch inspect 08e
{
"Id": "08ee444faa3c6634ecdecea26de46e8a6a16efefd9afb72eb3457320b333fc60",
"Created": "2017-12-04 14:48:59",
"Path": "",
"Args": null,
"State": {
"StartedAt": "0001-01-01T00:00:00Z",
"Status": 0,
"FinishedAt": "0001-01-01T00:00:00Z",
"Pid": 25006,
"ExitCode": 0,
"Error": ""
},
"Image": "registry.docker-cn.com/library/centos:latest",
"ResolvConfPath": "",
"HostnamePath": "",
"HostsPath": "",
"LogPath": "",
"Name": "08ee44",
"RestartCount": 0,
"Driver": "",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": null,
"HostRootPath": ""
}`
[
{
"Id": "08ee444faa3c6634ecdecea26de46e8a6a16efefd9afb72eb3457320b333fc60",
"Created": "2017-12-04 14:48:59",
"Path": "",
"Args": null,
"State": {
"StartedAt": "0001-01-01T00:00:00Z",
"Status": 0,
"FinishedAt": "0001-01-01T00:00:00Z",
"Pid": 25006,
"ExitCode": 0,
"Error": ""
},
"Image": "registry.docker-cn.com/library/centos:latest",
"ResolvConfPath": "",
"HostnamePath": "",
"HostsPath": "",
"LogPath": "",
"Name": "08ee44",
"RestartCount": 0,
"Driver": "",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": null,
"HostRootPath": ""
}
]`
}
40 changes: 16 additions & 24 deletions cli/inspect/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/alibaba/pouch/pkg/utils/templates"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Inspector defines an interface to implement to process elements.
Expand Down Expand Up @@ -56,37 +55,30 @@ type GetRefFunc func(ref string) (interface{}, error)

// Inspect fetches objects by reference and writes the json representation
// to the output writer.
func Inspect(out io.Writer, ref string, tmplStr string, getRef GetRefFunc) error {
func Inspect(out io.Writer, refs []string, tmplStr string, getRef GetRefFunc) error {
var errs []error

inspector, err := NewTemplateInspectorFromString(out, tmplStr)
if err != nil {
return err
}

element, err := getRef(ref)
if err != nil {
return errors.Errorf("Fetch object error: %v", err)
}
for _, ref := range refs {
element, err := getRef(ref)
if err != nil {
errs = append(errs, errors.Errorf("Fetch object error: %v", err))
continue
}

if err := inspector.Inspect(element); err != nil {
return err
if err := inspector.Inspect(element); err != nil {
errs = append(errs, err)
}
}

if err := inspector.Flush(); err != nil {
logrus.Errorf("%s\n", err)
return err
}

return nil
}

// MultiInspect fetches objects with multiple references.
func MultiInspect(out io.Writer, refs []string, tmplStr string, getRef GetRefFunc) error {
var errs []error
for _, ref := range refs {
err := Inspect(out, ref, tmplStr, getRef)
if err != nil {
errs = append(errs, err)
}
}
if len(errs) == 0 {
return nil
}
Expand Down Expand Up @@ -127,7 +119,7 @@ func (i *TemplateInspector) Flush() error {
// IndentedInspector uses a buffer to stop the indented representation of an element.
type IndentedInspector struct {
outputStream io.Writer
elements interface{}
elements []interface{}
rawElements [][]byte
}

Expand All @@ -141,15 +133,15 @@ func NewIndentedInspector(outputStream io.Writer) Inspector {
// Inspect writes the raw element with an indented json format.
func (i *IndentedInspector) Inspect(typedElement interface{}) error {
// TODO handle raw elements
i.elements = typedElement
i.elements = append(i.elements, typedElement)
return nil
}

// Flush writes the result of inspecting all elements into the output stream.
func (i *IndentedInspector) Flush() error {
// TODO handle raw elements
if i.elements == nil {
_, err := io.WriteString(i.outputStream, "\n")
_, err := io.WriteString(i.outputStream, "[]\n")
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cli/inspect/inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestInspect(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := &bytes.Buffer{}
if err := Inspect(out, tt.args.references, tt.args.tmplStr, tt.args.getRef); (err != nil) != tt.wantErr {
if err := Inspect(out, []string{tt.args.references}, tt.args.tmplStr, tt.args.getRef); (err != nil) != tt.wantErr {
t.Errorf("Inspect() error = %v, wantErr %v", err, tt.wantErr)
return
}
Expand Down
2 changes: 1 addition & 1 deletion cli/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (n *NetworkInspectCommand) runNetworkInspect(args []string) error {
return apiClient.NetworkInspect(ctx, ref)
}

return inspect.MultiInspect(os.Stdout, args, n.format, getRefFunc)
return inspect.Inspect(os.Stdout, args, n.format, getRefFunc)
}

// networkInspectExample shows examples in network inspect command, and is used in auto-generated cli docs.
Expand Down
2 changes: 1 addition & 1 deletion cli/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (v *VolumeInspectCommand) runVolumeInspect(args []string) error {
return apiClient.VolumeInspect(ctx, ref)
}

return inspect.MultiInspect(os.Stdout, args, v.format, getRefFunc)
return inspect.Inspect(os.Stdout, args, v.format, getRefFunc)
}

// volumeInspectExample shows examples in volume inspect command, and is used in auto-generated cli docs.
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func DeDuplicate(input []string) []string {
// format error message
type FormatErrMsgFunc func(idx int, err error) (string, error)

// CombineErrors is a function which used by MultiInspect to merge multiple errors
// CombineErrors is a function which used by Inspect to merge multiple errors
// into one error.
func CombineErrors(errs []error, formatErrMsg FormatErrMsgFunc) error {
var errMsgs []string
Expand Down
Loading