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

Commit

Permalink
Merge pull request #753 from zappy-shu/APP-321-inspect-cnab-bundles
Browse files Browse the repository at this point in the history
Image inspect non-app CNABs
  • Loading branch information
rumpl authored Nov 21, 2019
2 parents 9249440 + 8908b28 commit b621499
Show file tree
Hide file tree
Showing 14 changed files with 402 additions and 67 deletions.
16 changes: 8 additions & 8 deletions e2e/testdata/app-inspect.golden
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"Metadata": {
"version": "1.1.0-beta1",
"name": "simple",
"description": "new fancy webapp with microservices",
"maintainers": [
"Version": "1.1.0-beta1",
"Name": "simple",
"Description": "new fancy webapp with microservices",
"Maintainers": [
{
"name": "John Developer",
"email": "john.dev@example.com"
"Name": "John Developer",
"Email": "john.dev@example.com"
},
{
"name": "Jane Developer",
"email": "jane.dev@example.com"
"Name": "Jane Developer",
"Email": "jane.dev@example.com"
}
]
},
Expand Down
38 changes: 23 additions & 15 deletions internal/commands/image/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/deislabs/cnab-go/action"
"github.com/docker/app/internal"
"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/inspect"
appstore "github.com/docker/app/internal/store"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -68,28 +69,35 @@ func runInspect(dockerCli command.Cli, appname string, opts inspectOptions) erro
if err != nil {
return err
}
installation, err := appstore.NewInstallation("custom-action", ref.String(), bndl)
if err != nil {
return err
}
driverImpl, errBuf, err := cnab.SetupDriver(installation, dockerCli, opts.InstallerContextOptions, os.Stdout)
if err != nil {
return err
}
a := &action.RunCustom{
Action: internal.ActionInspectName,
Driver: driverImpl,
}

format := "json"
if opts.pretty {
format = "pretty"
}

installation.SetParameter(internal.ParameterInspectFormatName, format)
installation, err := appstore.NewInstallation("custom-action", ref.String(), bndl)
if err != nil {
return err
}

if _, hasAction := installation.Bundle.Actions[internal.ActionInspectName]; hasAction {
driverImpl, errBuf, err := cnab.SetupDriver(installation, dockerCli, opts.InstallerContextOptions, os.Stdout)
if err != nil {
return err
}
a := &action.RunCustom{
Action: internal.ActionInspectName,
Driver: driverImpl,
}

if err := a.Run(&installation.Claim, nil); err != nil {
return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
installation.SetParameter(internal.ParameterInspectFormatName, format)
if err = a.Run(&installation.Claim, nil); err != nil {
return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
}
} else {
if err = inspect.ImageInspectCNAB(os.Stdout, bndl.Bundle, format); err != nil {
return fmt.Errorf("inspect failed: %s", err)
}
}
return nil
}
8 changes: 2 additions & 6 deletions internal/commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOpt
}

func hasAction(bndl *bundle.Bundle, actionName string) bool {
for key := range bndl.Actions {
if key == actionName {
return true
}
}
return false
_, ok := bndl.Actions[actionName]
return ok
}
75 changes: 65 additions & 10 deletions internal/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,54 @@ func ImageInspect(out io.Writer, app *types.App, argParameters map[string]string
}

outputFormat := os.Getenv(internal.DockerInspectFormatEnvVar)
return printImageAppInfo(out, appInfo, outputFormat)
return printImageAppInfo(out, appInfo, outputFormat, true)
}

func ImageInspectCNAB(out io.Writer, bndl *bundle.Bundle, outputFormat string) error {
meta := metadata.AppMetadata{
Description: bndl.Description,
Name: bndl.Name,
Version: bndl.Version,
Maintainers: []metadata.Maintainer{},
}
for _, m := range bndl.Maintainers {
meta.Maintainers = append(meta.Maintainers, metadata.Maintainer{
Name: m.Name,
Email: m.Email,
})
}

paramKeys := []string{}
params := map[string]string{}
for _, v := range bndl.Parameters {
paramKeys = append(paramKeys, v.Definition)
if d, ok := bndl.Definitions[v.Definition]; ok && d.Default != nil {
params[v.Definition] = fmt.Sprint(d.Default)
} else {
params[v.Definition] = ""
}
}
sort.Strings(paramKeys)

services := []Service{}
for k, v := range bndl.Images {
services = append(services, Service{
Name: k,
Image: v.Image,
})
}
sort.SliceStable(services, func(i, j int) bool {
return services[i].Name < services[j].Name
})

appInfo := ImageAppInfo{
Metadata: meta,
parametersKeys: paramKeys,
Parameters: params,
Services: services,
}

return printImageAppInfo(out, appInfo, outputFormat, false)
}

func printAppInfo(out io.Writer, app AppInfo, format string) error {
Expand All @@ -124,10 +171,10 @@ func printAppInfo(out io.Writer, app AppInfo, format string) error {
}
}

func printImageAppInfo(out io.Writer, app ImageAppInfo, format string) error {
func printImageAppInfo(out io.Writer, app ImageAppInfo, format string, isApp bool) error {
switch format {
case "pretty":
return printTable(out, app)
return printTable(out, app, isApp)
case "json":
return printJSON(out, app)
default:
Expand Down Expand Up @@ -165,16 +212,24 @@ func printAppTable(out io.Writer, info AppInfo) error {
return nil
}

func printTable(out io.Writer, appInfo ImageAppInfo) error {
func printTable(out io.Writer, appInfo ImageAppInfo, isApp bool) error {
// Add Meta data
printYAML(out, appInfo.Metadata)

// Add Service section
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")
if isApp {
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")
} else {
printSection(out, len(appInfo.Services), func(w io.Writer) {
for _, service := range appInfo.Services {
fmt.Fprintf(w, "%s\t%s\n", service.Name, service.Image)
}
}, "SERVICE", "IMAGE")
}

// Add Network section
printSection(out, len(appInfo.Networks), func(w io.Writer) {
Expand Down Expand Up @@ -325,7 +380,7 @@ func extractParameters(app *types.App, argParameters map[string]string) ([]strin
for k := range allParameters {
parametersKeys = append(parametersKeys, k)
}
sort.Slice(parametersKeys, func(i, j int) bool { return parametersKeys[i] < parametersKeys[j] })
sort.Strings(parametersKeys)
return parametersKeys, allParameters, nil
}

Expand Down
25 changes: 25 additions & 0 deletions internal/inspect/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package inspect

import (
"bytes"
"encoding/json"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -132,6 +133,30 @@ text: hello`),
})
}

func TestImageInspectCNABFormatJSON(t *testing.T) {
testImageInspectCNAB(t, "json")
}

func TestImageInspectCNABFormatPretty(t *testing.T) {
testImageInspectCNAB(t, "pretty")
}

func testImageInspectCNAB(t *testing.T, format string) {
s := golden.Get(t, "bundle-json.golden")
var bndl bundle.Bundle
err := json.Unmarshal(s, &bndl)
assert.NilError(t, err)

expected := golden.Get(t, fmt.Sprintf("inspect-bundle-%s.golden", format))

outBuffer := new(bytes.Buffer)
err = ImageInspectCNAB(outBuffer, &bndl, format)
assert.NilError(t, err)

result := outBuffer.String()
assert.Equal(t, string(expected), result)
}

func testImageInspect(t *testing.T, dir *fs.Dir, testcase inspectTestCase, suffix string) {
app, err := types.NewAppFromDefaultFiles(dir.Join(testcase.name))
assert.NilError(t, err)
Expand Down
Loading

0 comments on commit b621499

Please sign in to comment.