Skip to content

Commit 9446280

Browse files
committed
Initial commit with changes to apps, app and get-health-check commands
Signed-off-by: João Pereira <[email protected]>
1 parent 07a5dd3 commit 9446280

8 files changed

+369
-10
lines changed

cf/cmd/cmd.go

+19
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,22 @@ func handleVerbose(args []string) ([]string, bool) {
226226

227227
return args, verbose
228228
}
229+
230+
func handleJSON(args []string) ([]string, bool) {
231+
var isJSON bool
232+
idx := -1
233+
234+
for i, arg := range args {
235+
if arg == "--json" {
236+
idx = i
237+
break
238+
}
239+
}
240+
241+
if idx != -1 && len(args) > 1 {
242+
isJSON = true
243+
args = append(args[:idx], args[idx+1:]...)
244+
}
245+
246+
return args, isJSON
247+
}

command/v7/app_command.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package v7
22

33
import (
4+
"code.cloudfoundry.org/cli/actor/v7action"
45
"code.cloudfoundry.org/cli/command/flag"
56
"code.cloudfoundry.org/cli/command/v7/shared"
67
)
78

9+
type AppDisplayer interface {
10+
AppDisplay(summary v7action.DetailedApplicationSummary, displayStartCommand bool)
11+
}
12+
813
type AppCommand struct {
914
BaseCommand
1015

1116
RequiredArgs flag.AppName `positional-args:"yes"`
1217
GUID bool `long:"guid" description:"Retrieve and display the given app's guid. All other health and status output for the app is suppressed."`
1318
usage interface{} `usage:"CF_NAME app APP_NAME [--guid]"`
1419
relatedCommands interface{} `related_commands:"apps, events, logs, map-route, unmap-route, push"`
20+
JSONOutput bool `long:"json" description:"Output in json form"`
1521
}
1622

1723
func (cmd AppCommand) Execute(args []string) error {
@@ -29,15 +35,18 @@ func (cmd AppCommand) Execute(args []string) error {
2935
return err
3036
}
3137

32-
cmd.UI.DisplayTextWithFlavor("Showing health and status for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
33-
"AppName": cmd.RequiredArgs.AppName,
34-
"OrgName": cmd.Config.TargetedOrganization().Name,
35-
"SpaceName": cmd.Config.TargetedSpace().Name,
36-
"Username": user.Name,
37-
})
38-
cmd.UI.DisplayNewline()
38+
var appSummaryDisplayer AppDisplayer = shared.NewAppSummaryJSONDisplayer(cmd.UI)
39+
if !cmd.JSONOutput {
40+
cmd.UI.DisplayTextWithFlavor("Showing health and status for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
41+
"AppName": cmd.RequiredArgs.AppName,
42+
"OrgName": cmd.Config.TargetedOrganization().Name,
43+
"SpaceName": cmd.Config.TargetedSpace().Name,
44+
"Username": user.Name,
45+
})
46+
cmd.UI.DisplayNewline()
47+
appSummaryDisplayer = shared.NewAppSummaryDisplayer(cmd.UI)
48+
}
3949

40-
appSummaryDisplayer := shared.NewAppSummaryDisplayer(cmd.UI)
4150
summary, warnings, err := cmd.Actor.GetDetailedAppSummary(cmd.RequiredArgs.AppName, cmd.Config.TargetedSpace().GUID, false)
4251
cmd.UI.DisplayWarnings(warnings)
4352
if err != nil {
@@ -55,6 +64,10 @@ func (cmd AppCommand) displayAppGUID() error {
5564
return err
5665
}
5766

67+
if cmd.JSONOutput {
68+
return cmd.UI.DisplayJSON("", map[string]string{"guid": app.GUID})
69+
}
70+
5871
cmd.UI.DisplayText(app.GUID)
5972
return nil
6073
}

command/v7/apps_command.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package v7
33
import (
44
"strings"
55

6+
"code.cloudfoundry.org/cli/actor/v7action"
67
"code.cloudfoundry.org/cli/resources"
78
"code.cloudfoundry.org/cli/util/ui"
89
)
@@ -13,8 +14,9 @@ type AppsCommand struct {
1314
usage interface{} `usage:"CF_NAME apps [--labels SELECTOR]\n\nEXAMPLES:\n CF_NAME apps\n CF_NAME apps --labels 'environment in (production,staging),tier in (backend)'\n CF_NAME apps --labels 'env=dev,!chargeback-code,tier in (backend,worker)'"`
1415
relatedCommands interface{} `related_commands:"events, logs, map-route, push, scale, start, stop, restart"`
1516

16-
Labels string `long:"labels" description:"Selector to filter apps by labels"`
17-
OmitStats bool `long:"no-stats" description:"Do not retrieve process stats"`
17+
Labels string `long:"labels" description:"Selector to filter apps by labels"`
18+
OmitStats bool `long:"no-stats" description:"Do not retrieve process stats"`
19+
JSONOutput bool `long:"json" description:"Output in json form"`
1820
}
1921

2022
func (cmd AppsCommand) Execute(args []string) error {
@@ -41,6 +43,10 @@ func (cmd AppsCommand) Execute(args []string) error {
4143
return err
4244
}
4345

46+
if cmd.JSONOutput {
47+
return cmd.UI.DisplayJSON("", map[string][]v7action.ApplicationSummary{"apps": summaries})
48+
}
49+
4450
if len(summaries) == 0 {
4551
cmd.UI.DisplayText("No apps found")
4652
return nil

command/v7/get_health_check_command.go

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type GetHealthCheckCommand struct {
1313

1414
RequiredArgs flag.AppName `positional-args:"yes"`
1515
usage interface{} `usage:"CF_NAME get-health-check APP_NAME"`
16+
JSONOutput bool `long:"json" description:"Output in json form"`
1617
}
1718

1819
func (cmd GetHealthCheckCommand) Execute(args []string) error {
@@ -39,6 +40,10 @@ func (cmd GetHealthCheckCommand) Execute(args []string) error {
3940
return err
4041
}
4142

43+
if cmd.JSONOutput {
44+
return cmd.UI.DisplayJSON("", map[string][]v7action.ProcessHealthCheck{"app": processHealthChecks})
45+
}
46+
4247
cmd.UI.DisplayNewline()
4348

4449
if len(processHealthChecks) == 0 {

command/v7/shared/app_summary_displayer.go

+14
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,17 @@ func (display AppSummaryDisplayer) displayBuildpackTable(buildpacks []resources.
238238
display.UI.DisplayTableWithHeader("\t", keyValueTable, ui.DefaultTableSpacePadding)
239239
}
240240
}
241+
242+
type AppSummaryJSONDisplayer struct {
243+
UI command.UI
244+
}
245+
246+
func NewAppSummaryJSONDisplayer(ui command.UI) *AppSummaryJSONDisplayer {
247+
return &AppSummaryJSONDisplayer{
248+
UI: ui,
249+
}
250+
}
251+
252+
func (display AppSummaryJSONDisplayer) AppDisplay(summary v7action.DetailedApplicationSummary, displayStartCommand bool) {
253+
display.UI.DisplayJSON("", summary)
254+
}

0 commit comments

Comments
 (0)