1
1
package commands
2
2
3
3
import (
4
+ "bytes"
5
+ "encoding/json"
4
6
"fmt"
5
7
"os"
6
- "strings"
7
8
8
9
"github.com/deislabs/cnab-go/action"
10
+ "github.com/deislabs/cnab-go/bundle"
9
11
"github.com/docker/app/internal"
12
+ "github.com/docker/app/internal/cliopts"
10
13
"github.com/docker/app/internal/cnab"
11
14
"github.com/docker/app/internal/inspect"
12
15
"github.com/docker/cli/cli"
13
16
"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"
17
17
"github.com/spf13/cobra"
18
- "github.com/spf13/pflag"
19
18
)
20
19
21
20
type inspectOptions struct {
22
21
credentialOptions
22
+ cliopts.InstallerContextOptions
23
23
pretty bool
24
24
orchestrator string
25
25
kubeNamespace string
@@ -29,10 +29,10 @@ func inspectCmd(dockerCli command.Cli) *cobra.Command {
29
29
var opts inspectOptions
30
30
cmd := & cobra.Command {
31
31
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 " ,
33
33
Example : `$ docker app inspect my-running-app
34
34
$ docker app inspect my-running-app:1.0.0` ,
35
- Args : cli .RequiresMaxArgs (1 ),
35
+ Args : cli .ExactArgs (1 ),
36
36
Hidden : true ,
37
37
RunE : func (cmd * cobra.Command , args []string ) error {
38
38
return runInspect (dockerCli , firstOrEmpty (args ), opts )
@@ -42,6 +42,7 @@ $ docker app inspect my-running-app:1.0.0`,
42
42
cmd .Flags ().StringVar (& opts .orchestrator , "orchestrator" , "" , "Orchestrator where the App is running on (swarm, kubernetes)" )
43
43
cmd .Flags ().StringVar (& opts .kubeNamespace , "namespace" , "default" , "Kubernetes namespace in which to find the App" )
44
44
opts .credentialOptions .addFlags (cmd .Flags ())
45
+ opts .InstallerContextOptions .AddFlags (cmd .Flags ())
45
46
return cmd
46
47
}
47
48
@@ -50,18 +51,9 @@ func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOpt
50
51
if err != nil {
51
52
return err
52
53
}
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 )
61
54
62
- inspectOptions .SetDefaultTargetContext (dockerCli )
63
55
defer muteDockerCli (dockerCli )()
64
- _ , installationStore , credentialStore , err := prepareStores (inspectOptions . targetContext )
56
+ _ , installationStore , credentialStore , err := prepareStores (dockerCli . CurrentContext () )
65
57
if err != nil {
66
58
return err
67
59
}
@@ -75,48 +67,53 @@ func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOpt
75
67
orchestratorName = string (orchestrator )
76
68
}
77
69
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 {
86
72
return err
87
73
}
88
74
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 )
100
77
if err != nil {
101
78
return err
102
79
}
103
80
104
- driverImpl , errBuf := cnab .PrepareDriver (dockerCli , bind , nil )
105
81
a := & action.RunCustom {
106
- Action : actionName ,
107
82
Driver : driverImpl ,
108
83
}
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" )
113
90
}
114
-
115
- installation .SetParameter (internal .ParameterInspectFormatName , format )
116
- println ()
117
91
if err := a .Run (& installation .Claim , creds , nil ); err != nil {
118
92
return fmt .Errorf ("inspect failed: %s\n %s" , err , errBuf )
119
93
}
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
+ }
120
117
return nil
121
118
}
122
119
@@ -132,3 +129,12 @@ func getContextOrchestrator(dockerCli command.Cli, orchestratorFlag string) (com
132
129
}
133
130
return orchestrator , nil
134
131
}
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