2017-06-29 19:16:06 +00:00
package main
import (
2017-08-14 19:32:00 +00:00
"github.com/kubernetes-incubator/cri-o/cmd/kpod/formats"
2017-07-23 23:01:37 +00:00
"github.com/kubernetes-incubator/cri-o/libkpod"
2017-08-31 15:17:21 +00:00
"github.com/kubernetes-incubator/cri-o/libpod/images"
2017-06-29 19:16:06 +00:00
"github.com/pkg/errors"
"github.com/urfave/cli"
)
const (
inspectTypeContainer = "container"
inspectTypeImage = "image"
inspectAll = "all"
)
var (
inspectFlags = [ ] cli . Flag {
cli . StringFlag {
Name : "type, t" ,
Value : inspectAll ,
Usage : "Return JSON for specified type, (e.g image, container or task)" ,
} ,
cli . StringFlag {
Name : "format, f" ,
2017-08-14 19:32:00 +00:00
Usage : "Change the output format to a Go template" ,
2017-06-29 19:16:06 +00:00
} ,
cli . BoolFlag {
Name : "size" ,
Usage : "Display total file size if the type is container" ,
} ,
}
inspectDescription = "This displays the low-level information on containers and images identified by name or ID. By default, this will render all results in a JSON array. If the container and image have the same name, this will return container JSON for unspecified type."
inspectCommand = cli . Command {
Name : "inspect" ,
Usage : "Displays the configuration of a container or image" ,
Description : inspectDescription ,
Flags : inspectFlags ,
Action : inspectCmd ,
ArgsUsage : "CONTAINER-OR-IMAGE" ,
}
)
func inspectCmd ( c * cli . Context ) error {
args := c . Args ( )
if len ( args ) == 0 {
return errors . Errorf ( "container or image name must be specified: kpod inspect [options [...]] name" )
}
if len ( args ) > 1 {
return errors . Errorf ( "too many arguments specified" )
}
2017-09-28 18:44:48 +00:00
if err := validateFlags ( c , inspectFlags ) ; err != nil {
return err
}
2017-06-29 19:16:06 +00:00
itemType := c . String ( "type" )
size := c . Bool ( "size" )
switch itemType {
case inspectTypeContainer :
case inspectTypeImage :
case inspectAll :
default :
return errors . Errorf ( "the only recognized types are %q, %q, and %q" , inspectTypeContainer , inspectTypeImage , inspectAll )
}
name := args [ 0 ]
2017-07-27 17:18:07 +00:00
config , err := getConfig ( c )
if err != nil {
return errors . Wrapf ( err , "Could not get config" )
}
2017-08-01 15:28:50 +00:00
server , err := libkpod . New ( config )
2017-06-29 19:16:06 +00:00
if err != nil {
2017-08-08 14:23:32 +00:00
return errors . Wrapf ( err , "could not get container server" )
}
2017-08-17 14:23:54 +00:00
defer server . Shutdown ( )
2017-08-08 14:23:32 +00:00
if err = server . Update ( ) ; err != nil {
return errors . Wrapf ( err , "could not update list of containers" )
2017-06-29 19:16:06 +00:00
}
2017-08-14 19:32:00 +00:00
outputFormat := c . String ( "format" )
2017-06-29 19:16:06 +00:00
var data interface { }
switch itemType {
case inspectTypeContainer :
2017-08-01 15:28:50 +00:00
data , err = server . GetContainerData ( name , size )
2017-06-29 19:16:06 +00:00
if err != nil {
return errors . Wrapf ( err , "error parsing container data" )
}
case inspectTypeImage :
2017-08-31 15:17:21 +00:00
data , err = images . GetData ( server . Store ( ) , name )
2017-06-29 19:16:06 +00:00
if err != nil {
return errors . Wrapf ( err , "error parsing image data" )
}
case inspectAll :
2017-08-01 15:28:50 +00:00
ctrData , err := server . GetContainerData ( name , size )
2017-06-29 19:16:06 +00:00
if err != nil {
2017-08-31 15:17:21 +00:00
imgData , err := images . GetData ( server . Store ( ) , name )
2017-06-29 19:16:06 +00:00
if err != nil {
2017-08-08 14:23:32 +00:00
return errors . Wrapf ( err , "error parsing container or image data" )
2017-06-29 19:16:06 +00:00
}
data = imgData
} else {
data = ctrData
}
}
2017-08-14 19:32:00 +00:00
var out formats . Writer
if outputFormat != "" && outputFormat != formats . JSONString {
//template
out = formats . StdoutTemplate { Output : data , Template : outputFormat }
} else {
// default is json output
out = formats . JSONStruct { Output : data }
2017-06-29 19:16:06 +00:00
}
2017-08-14 19:32:00 +00:00
formats . Writer ( out ) . Out ( )
return nil
2017-06-29 19:16:06 +00:00
}