Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Check parameter presence
Browse files Browse the repository at this point in the history
Older bundles don't have the inspect format parameter, we don't set it
for them, we set it only on bundles that define that parameter.

Signed-off-by: Djordje Lukic <djordje.lukic@docker.com>
  • Loading branch information
rumpl committed Oct 4, 2019
1 parent dbd0ee1 commit d8c9ffb
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 26 deletions.
1 change: 0 additions & 1 deletion e2e/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ func TestInspectApp(t *testing.T) {
cmd.Command = dockerCli.Command("app", "inspect", "simple-app:1.0.0")
cmd.Dir = dir.Path()
output := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined()
fmt.Println(output)
golden.Assert(t, output, "app-inspect.golden")

}
Expand Down
18 changes: 11 additions & 7 deletions internal/commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ type inspectOptions struct {
func inspectCmd(dockerCli command.Cli) *cobra.Command {
var opts inspectOptions
cmd := &cobra.Command{
Use: "inspect [APP_NAME] [OPTIONS]",
Short: "Shows metadata, parameters and a summary of the Compose file for a given application",
Example: `$ docker app inspect my-app`,
Args: cli.RequiresMaxArgs(1),
Use: "inspect [APP_NAME] [OPTIONS]",
Short: "Shows metadata, parameters and a summary of the Compose file for a given application",
Example: `$ docker app inspect my-installed-app
$docker app inspect my-app:1.0.0`,
Args: cli.RequiresMaxArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runInspect(dockerCli, firstOrEmpty(args), opts)
},
Expand All @@ -42,15 +43,16 @@ func runInspect(dockerCli command.Cli, appname string, opts inspectOptions) erro
if err != nil {
return err
}
bundle, ref, err := getLocalBundle(dockerCli, bundleStore, appname, false)
bndl, ref, err := getLocalBundle(dockerCli, bundleStore, appname, false)

if err != nil {
return err
}
installation, err := appstore.NewInstallation("custom-action", ref.String())
if err != nil {
return err
}
installation.Bundle = bundle
installation.Bundle = bndl

driverImpl, errBuf := prepareDriver(dockerCli, bindMount{}, nil)
a := &action.RunCustom{
Expand All @@ -62,7 +64,9 @@ func runInspect(dockerCli command.Cli, appname string, opts inspectOptions) erro
if opts.pretty {
format = "pretty"
}
installation.Parameters[internal.ParameterInspectFormatName] = format

installation.SetParameter(internal.ParameterInspectFormatName, format)

if err := a.Run(&installation.Claim, nil, nil); err != nil {
return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func pushInvocationImage(dockerCli command.Cli, retag retagResult) error {
func pushBundle(dockerCli command.Cli, opts pushOptions, bndl *bundle.Bundle, retag retagResult) error {
insecureRegistries, err := insecureRegistriesFromEngine(dockerCli)
if err != nil {
return errors.Wrap(err, "could not retrive insecure registries")
return errors.Wrap(err, "could not retrieve insecure registries")
}
resolver := remotes.CreateResolver(dockerCli.ConfigFile(), insecureRegistries...)
var display fixupDisplay = &plainDisplay{out: os.Stdout}
Expand Down
42 changes: 28 additions & 14 deletions internal/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,47 +45,62 @@ type appInfo struct {

// Inspect dumps the metadata of an app
func Inspect(out io.Writer, app *types.App, argParameters map[string]string, imageMap map[string]bundle.Image) error {
outputFormat := os.Getenv(internal.DockerInspectFormatEnvVar)

// Render the compose file
config, err := render.Render(app, argParameters, imageMap)
if err != nil {
return err
}

// Collect all the relevant information about the application
appInfo, err := getAppInfo(app, config, argParameters)
if err != nil {
return err
}

if outputFormat == "json" {
js, err := json.MarshalIndent(appInfo, "", " ")
if err != nil {
return err
}
fmt.Fprintln(out, string(js))
return nil
outputFormat := os.Getenv(internal.DockerInspectFormatEnvVar)
return printAppInfo(out, appInfo, outputFormat)
}

func printAppInfo(out io.Writer, app appInfo, format string) error {
switch format {
case "pretty":
return printTable(out, app)
case "json":
default:
return printJSON(out, app)
}
return nil
}

func printJSON(out io.Writer, appInfo appInfo) error {
js, err := json.MarshalIndent(appInfo, "", " ")
if err != nil {
return err
}
fmt.Fprintln(out, string(js))
return nil
}

func printTable(out io.Writer, appInfo appInfo) error {
// Add Meta data
printMetadata(out, appInfo)

// Add Service section
printSection(out, len(config.Services), func(w io.Writer) {
printSection(out, len(appInfo.Services), func(w io.Writer) {
for _, service := range appInfo.Services {
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n", service.Name, service.Replicas, service.Ports, service.Image)
}
}, "Service", "Replicas", "Ports", "Image")

// Add Network section
printSection(out, len(config.Networks), func(w io.Writer) {
printSection(out, len(appInfo.Networks), func(w io.Writer) {
for _, name := range appInfo.Networks {
fmt.Fprintln(w, name)
}
}, "Network")

// Add Volume section
printSection(out, len(config.Volumes), func(w io.Writer) {
printSection(out, len(appInfo.Volumes), func(w io.Writer) {
for _, name := range appInfo.Volumes {
fmt.Fprintln(w, name)
}
Expand All @@ -105,14 +120,13 @@ func Inspect(out io.Writer, app *types.App, argParameters map[string]string, ima
}
}, "Parameter", "Value")

// // Add Attachments section
// Add Attachments section
printSection(out, len(appInfo.Attachments), func(w io.Writer) {
for _, attachment := range appInfo.Attachments {
sizeString := units.HumanSize(float64(attachment.Size))
fmt.Fprintf(w, "%s\t%s\n", attachment.Path, sizeString)
}
}, "Attachment", "Size")

return nil
}

Expand Down
7 changes: 4 additions & 3 deletions internal/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ const (
ParameterOrchestratorName = Namespace + "orchestrator"
// ParameterKubernetesNamespaceName is the name of the parameter containing the kubernetes namespace
ParameterKubernetesNamespaceName = Namespace + "kubernetes-namespace"
// ParameterRenderFormatName is the name of the parameter containing the kubernetes namespace
// ParameterRenderFormatName is the name of the parameter containing the render format
ParameterRenderFormatName = Namespace + "render-format"
// ParameterRenderFormatName is the name of the parameter containing the kubernetes namespace
// ParameterInspectFormatName is the name of the parameter containing the inspect format
ParameterInspectFormatName = Namespace + "inspect-format"
// ParameterShareRegistryCredsName is the name of the parameter which indicates if credentials should be shared
ParameterShareRegistryCredsName = Namespace + "share-registry-creds"
Expand All @@ -66,7 +66,8 @@ const (
// DockerRenderFormatEnvVar is the environment variable set by the CNAB runtime to select
// the render output format.
DockerRenderFormatEnvVar = "DOCKER_RENDER_FORMAT"
// DockerInspectFormatEnvVar ...
// DockerInspectFormatEnvVar is the environment variable set by the CNAB runtim to select
// the inspect output format.
DockerInspectFormatEnvVar = "DOCKER_INSPECT_FORMAT"
)

Expand Down
8 changes: 8 additions & 0 deletions internal/store/installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func NewInstallation(name string, reference string) (*Installation, error) {
}, nil
}

// SetParameters sets the parameter value if the installation bundle has
// a defined parameter with that name.
func (i Installation) SetParameter(name string, value string) {
if _, ok := i.Bundle.Parameters[name]; ok {
i.Parameters[name] = value
}
}

var _ InstallationStore = &installationStore{}

type installationStore struct {
Expand Down

0 comments on commit d8c9ffb

Please sign in to comment.