11package commands
22
33import (
4+ "bytes"
5+ "encoding/json"
46 "fmt"
57 "os"
6- "strings"
78
89 "github.com/deislabs/cnab-go/action"
10+ "github.com/deislabs/cnab-go/bundle"
911 "github.com/docker/app/internal"
12+ "github.com/docker/app/internal/cliopts"
1013 "github.com/docker/app/internal/cnab"
1114 "github.com/docker/app/internal/inspect"
1215 "github.com/docker/cli/cli"
1316 "github.com/docker/cli/cli/command"
14- "github.com/docker/cli/cli/command/stack"
15- "github.com/docker/cli/cli/command/stack/options"
16- "github.com/docker/cli/opts"
1717 "github.com/spf13/cobra"
18- "github.com/spf13/pflag"
1918)
2019
2120type inspectOptions struct {
2221 credentialOptions
22+ cliopts.InstallerContextOptions
2323 pretty bool
2424 orchestrator string
2525 kubeNamespace string
@@ -29,10 +29,10 @@ func inspectCmd(dockerCli command.Cli) *cobra.Command {
2929 var opts inspectOptions
3030 cmd := & cobra.Command {
3131 Use : "inspect [OPTIONS] RUNNING_APP" ,
32- Short : "Shows installation and application metadata, parameters and the containers list of a running application " ,
32+ Short : "Shows installation and App metadata, parameters and the service list of a running App " ,
3333 Example : `$ docker app inspect my-running-app
3434$ docker app inspect my-running-app:1.0.0` ,
35- Args : cli .RequiresMaxArgs (1 ),
35+ Args : cli .ExactArgs (1 ),
3636 Hidden : true ,
3737 RunE : func (cmd * cobra.Command , args []string ) error {
3838 return runInspect (dockerCli , firstOrEmpty (args ), opts )
@@ -42,6 +42,7 @@ $ docker app inspect my-running-app:1.0.0`,
4242 cmd .Flags ().StringVar (& opts .orchestrator , "orchestrator" , "" , "Orchestrator where the App is running on (swarm, kubernetes)" )
4343 cmd .Flags ().StringVar (& opts .kubeNamespace , "namespace" , "default" , "Kubernetes namespace in which to find the App" )
4444 opts .credentialOptions .addFlags (cmd .Flags ())
45+ opts .InstallerContextOptions .AddFlags (cmd .Flags ())
4546 return cmd
4647}
4748
@@ -50,18 +51,9 @@ func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOpt
5051 if err != nil {
5152 return err
5253 }
53- services , err := stack .GetServices (dockerCli , pflag .NewFlagSet ("" , pflag .ContinueOnError ), orchestrator , options.Services {
54- Filter : opts .NewFilterOpt (),
55- Namespace : inspectOptions .kubeNamespace ,
56- })
57- if err != nil {
58- return err
59- }
60- println (services )
6154
62- inspectOptions .SetDefaultTargetContext (dockerCli )
6355 defer muteDockerCli (dockerCli )()
64- _ , installationStore , credentialStore , err := prepareStores (inspectOptions . targetContext )
56+ _ , installationStore , credentialStore , err := prepareStores (dockerCli . CurrentContext () )
6557 if err != nil {
6658 return err
6759 }
@@ -75,48 +67,53 @@ func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOpt
7567 orchestratorName = string (orchestrator )
7668 }
7769
78- format := "json"
79- actionName := internal .ActionStatusJSONName
80- if inspectOptions .pretty {
81- format = "pretty"
82- actionName = internal .ActionStatusName
83- }
84-
85- if err := inspect .Inspect (os .Stdout , installation .Claim , format , orchestratorName ); err != nil {
70+ creds , err := prepareCredentialSet (installation .Bundle , inspectOptions .CredentialSetOpts (dockerCli , credentialStore )... )
71+ if err != nil {
8672 return err
8773 }
8874
89- var statusAction bool
90- for key := range installation .Bundle .Actions {
91- if strings .HasPrefix (key , "io.cnab.status" ) {
92- statusAction = true
93- }
94- }
95- if ! statusAction {
96- return nil
97- }
98-
99- bind , err := cnab .RequiredBindMount (inspectOptions .targetContext , orchestratorName , dockerCli .ContextStore ())
75+ var buf bytes.Buffer
76+ driverImpl , errBuf , err := cnab .SetupDriver (installation , dockerCli , inspectOptions .InstallerContextOptions , & buf )
10077 if err != nil {
10178 return err
10279 }
10380
104- driverImpl , errBuf := cnab .PrepareDriver (dockerCli , bind , nil )
10581 a := & action.RunCustom {
106- Action : actionName ,
10782 Driver : driverImpl ,
10883 }
109-
110- creds , err := prepareCredentialSet (installation .Bundle , inspectOptions .CredentialSetOpts (dockerCli , credentialStore )... )
111- if err != nil {
112- return err
84+ if inspectOptions .pretty && hasAction (installation .Bundle , internal .ActionStatusName ) {
85+ a .Action = internal .ActionStatusName
86+ } else if hasAction (installation .Bundle , internal .ActionStatusJSONName ) {
87+ a .Action = internal .ActionStatusJSONName
88+ } else {
89+ return fmt .Errorf ("inspect failed: status action is not supported by the App" )
11390 }
114-
115- installation .SetParameter (internal .ParameterInspectFormatName , format )
116- println ()
11791 if err := a .Run (& installation .Claim , creds , nil ); err != nil {
11892 return fmt .Errorf ("inspect failed: %s\n %s" , err , errBuf )
11993 }
94+
95+ if inspectOptions .pretty {
96+ if err := inspect .Inspect (os .Stdout , installation , "pretty" , orchestratorName ); err != nil {
97+ return err
98+ }
99+ fmt .Fprint (os .Stdout , buf .String ())
100+ } else {
101+ var statusJSON interface {}
102+ if err := json .Unmarshal (buf .Bytes (), & statusJSON ); err != nil {
103+ return err
104+ }
105+ js , err := json .MarshalIndent (struct {
106+ AppInfo inspect.AppInfo `json:",omitempty"`
107+ Services interface {} `json:",omitempty"`
108+ }{
109+ inspect .GetAppInfo (installation , orchestratorName ),
110+ statusJSON ,
111+ }, "" , " " )
112+ if err != nil {
113+ return err
114+ }
115+ fmt .Fprint (os .Stdout , string (js ))
116+ }
120117 return nil
121118}
122119
@@ -132,3 +129,12 @@ func getContextOrchestrator(dockerCli command.Cli, orchestratorFlag string) (com
132129 }
133130 return orchestrator , nil
134131}
132+
133+ func hasAction (bndl * bundle.Bundle , actionName string ) bool {
134+ for key := range bndl .Actions {
135+ if key == actionName {
136+ return true
137+ }
138+ }
139+ return false
140+ }
0 commit comments