github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/v3_apps_command.go (about)

     1  package v3
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/actor/sharedaction"
     8  	"code.cloudfoundry.org/cli/actor/v2action"
     9  	"code.cloudfoundry.org/cli/actor/v3action"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    12  	"code.cloudfoundry.org/cli/command"
    13  	"code.cloudfoundry.org/cli/command/translatableerror"
    14  	sharedV2 "code.cloudfoundry.org/cli/command/v2/shared"
    15  	"code.cloudfoundry.org/cli/command/v3/shared"
    16  	"code.cloudfoundry.org/cli/util/ui"
    17  )
    18  
    19  //go:generate counterfeiter . V3AppsActor
    20  
    21  type V3AppsActor interface {
    22  	CloudControllerAPIVersion() string
    23  	GetApplicationsWithProcessesBySpace(spaceGUID string) ([]v3action.ApplicationWithProcessSummary, v3action.Warnings, error)
    24  }
    25  
    26  type V3AppsCommand struct {
    27  	usage interface{} `usage:"CF_NAME v3-apps"`
    28  
    29  	UI              command.UI
    30  	Config          command.Config
    31  	Actor           V3AppsActor
    32  	V2AppRouteActor shared.V2AppRouteActor
    33  	SharedActor     command.SharedActor
    34  }
    35  
    36  func (cmd *V3AppsCommand) Setup(config command.Config, ui command.UI) error {
    37  	cmd.UI = ui
    38  	cmd.Config = config
    39  	cmd.SharedActor = sharedaction.NewActor(config)
    40  
    41  	ccClient, _, err := shared.NewClients(config, ui, true)
    42  	if err != nil {
    43  		if v3Err, ok := err.(ccerror.V3UnexpectedResponseError); ok && v3Err.ResponseCode == http.StatusNotFound {
    44  			return translatableerror.MinimumAPIVersionNotMetError{MinimumVersion: ccversion.MinVersionV3}
    45  		}
    46  
    47  		return err
    48  	}
    49  	cmd.Actor = v3action.NewActor(ccClient, config, nil, nil)
    50  
    51  	ccClientV2, uaaClientV2, err := sharedV2.NewClients(config, ui, true)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	cmd.V2AppRouteActor = v2action.NewActor(ccClientV2, uaaClientV2, config)
    57  
    58  	return nil
    59  }
    60  
    61  func (cmd V3AppsCommand) Execute(args []string) error {
    62  	cmd.UI.DisplayWarning(command.ExperimentalWarning)
    63  
    64  	err := command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionV3)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	err = cmd.SharedActor.CheckTarget(true, true)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	user, err := cmd.Config.CurrentUser()
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	cmd.UI.DisplayTextWithFlavor("Getting apps in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    80  		"OrgName":   cmd.Config.TargetedOrganization().Name,
    81  		"SpaceName": cmd.Config.TargetedSpace().Name,
    82  		"Username":  user.Name,
    83  	})
    84  	cmd.UI.DisplayNewline()
    85  
    86  	summaries, warnings, err := cmd.Actor.GetApplicationsWithProcessesBySpace(cmd.Config.TargetedSpace().GUID)
    87  	cmd.UI.DisplayWarnings(warnings)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	if len(summaries) == 0 {
    93  		cmd.UI.DisplayText("No apps found")
    94  		return nil
    95  	}
    96  
    97  	table := [][]string{
    98  		{
    99  			cmd.UI.TranslateText("name"),
   100  			cmd.UI.TranslateText("requested state"),
   101  			cmd.UI.TranslateText("processes"),
   102  			cmd.UI.TranslateText("routes"),
   103  		},
   104  	}
   105  
   106  	for _, summary := range summaries {
   107  		var routesList string
   108  		if len(summary.ProcessSummaries) > 0 {
   109  			routes, warnings, err := cmd.V2AppRouteActor.GetApplicationRoutes(summary.GUID)
   110  			cmd.UI.DisplayWarnings(warnings)
   111  			if err != nil {
   112  				return err
   113  			}
   114  			routesList = routes.Summary()
   115  		}
   116  
   117  		table = append(table, []string{
   118  			summary.Name,
   119  			cmd.UI.TranslateText(strings.ToLower(string(summary.State))),
   120  			summary.ProcessSummaries.String(),
   121  			routesList,
   122  		})
   123  	}
   124  
   125  	cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
   126  
   127  	return nil
   128  }