Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PoC] Add --json flag to cli commands #3249

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cf/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,22 @@ func handleVerbose(args []string) ([]string, bool) {

return args, verbose
}

func handleJSON(args []string) ([]string, bool) {
var isJSON bool
idx := -1

for i, arg := range args {
if arg == "--json" {
idx = i
break
}
}

if idx != -1 && len(args) > 1 {
isJSON = true
args = append(args[:idx], args[idx+1:]...)
}

return args, isJSON
}
29 changes: 21 additions & 8 deletions command/v7/app_command.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package v7

import (
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/v7/shared"
)

type AppDisplayer interface {
AppDisplay(summary v7action.DetailedApplicationSummary, displayStartCommand bool)
}

type AppCommand struct {
BaseCommand

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

func (cmd AppCommand) Execute(args []string) error {
Expand All @@ -29,15 +35,18 @@ func (cmd AppCommand) Execute(args []string) error {
return err
}

cmd.UI.DisplayTextWithFlavor("Showing health and status for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
"AppName": cmd.RequiredArgs.AppName,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"Username": user.Name,
})
cmd.UI.DisplayNewline()
var appSummaryDisplayer AppDisplayer = shared.NewAppSummaryJSONDisplayer(cmd.UI)
if !cmd.JSONOutput {
cmd.UI.DisplayTextWithFlavor("Showing health and status for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
"AppName": cmd.RequiredArgs.AppName,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"Username": user.Name,
})
cmd.UI.DisplayNewline()
appSummaryDisplayer = shared.NewAppSummaryDisplayer(cmd.UI)
}

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

if cmd.JSONOutput {
return cmd.UI.DisplayJSON("", map[string]string{"guid": app.GUID})
}

cmd.UI.DisplayText(app.GUID)
return nil
}
10 changes: 8 additions & 2 deletions command/v7/apps_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v7
import (
"strings"

"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/resources"
"code.cloudfoundry.org/cli/util/ui"
)
Expand All @@ -13,8 +14,9 @@ type AppsCommand struct {
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)'"`
relatedCommands interface{} `related_commands:"events, logs, map-route, push, scale, start, stop, restart"`

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

func (cmd AppsCommand) Execute(args []string) error {
Expand All @@ -41,6 +43,10 @@ func (cmd AppsCommand) Execute(args []string) error {
return err
}

if cmd.JSONOutput {
return cmd.UI.DisplayJSON("", map[string][]v7action.ApplicationSummary{"apps": summaries})
}

if len(summaries) == 0 {
cmd.UI.DisplayText("No apps found")
return nil
Expand Down
5 changes: 5 additions & 0 deletions command/v7/get_health_check_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type GetHealthCheckCommand struct {

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

func (cmd GetHealthCheckCommand) Execute(args []string) error {
Expand All @@ -39,6 +40,10 @@ func (cmd GetHealthCheckCommand) Execute(args []string) error {
return err
}

if cmd.JSONOutput {
return cmd.UI.DisplayJSON("", map[string][]v7action.ProcessHealthCheck{"app": processHealthChecks})
}

cmd.UI.DisplayNewline()

if len(processHealthChecks) == 0 {
Expand Down
14 changes: 14 additions & 0 deletions command/v7/shared/app_summary_displayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,17 @@ func (display AppSummaryDisplayer) displayBuildpackTable(buildpacks []resources.
display.UI.DisplayTableWithHeader("\t", keyValueTable, ui.DefaultTableSpacePadding)
}
}

type AppSummaryJSONDisplayer struct {
UI command.UI
}

func NewAppSummaryJSONDisplayer(ui command.UI) *AppSummaryJSONDisplayer {
return &AppSummaryJSONDisplayer{
UI: ui,
}
}

func (display AppSummaryJSONDisplayer) AppDisplay(summary v7action.DetailedApplicationSummary, displayStartCommand bool) {
display.UI.DisplayJSON("", summary)
}
Loading
Loading