github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/get_health_check_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	"code.cloudfoundry.org/cli/command/v7/shared"
    11  	"code.cloudfoundry.org/cli/util/ui"
    12  	"code.cloudfoundry.org/clock"
    13  )
    14  
    15  //go:generate counterfeiter . GetHealthCheckActor
    16  
    17  type GetHealthCheckActor interface {
    18  	CloudControllerAPIVersion() string
    19  	GetApplicationProcessHealthChecksByNameAndSpace(appName string, spaceGUID string) ([]v7action.ProcessHealthCheck, v7action.Warnings, error)
    20  }
    21  
    22  type GetHealthCheckCommand struct {
    23  	RequiredArgs flag.AppName `positional-args:"yes"`
    24  	usage        interface{}  `usage:"CF_NAME get-health-check APP_NAME"`
    25  
    26  	UI          command.UI
    27  	Config      command.Config
    28  	SharedActor command.SharedActor
    29  	Actor       GetHealthCheckActor
    30  }
    31  
    32  func (cmd *GetHealthCheckCommand) Setup(config command.Config, ui command.UI) error {
    33  	cmd.UI = ui
    34  	cmd.Config = config
    35  	cmd.SharedActor = sharedaction.NewActor(config)
    36  
    37  	ccClient, _, err := shared.GetNewClientsAndConnectToCF(config, ui, "")
    38  	if err != nil {
    39  		return err
    40  	}
    41  	cmd.Actor = v7action.NewActor(ccClient, config, nil, nil, clock.NewClock())
    42  
    43  	return nil
    44  }
    45  
    46  func (cmd GetHealthCheckCommand) Execute(args []string) error {
    47  	err := cmd.SharedActor.CheckTarget(true, true)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	user, err := cmd.Config.CurrentUser()
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	cmd.UI.DisplayTextWithFlavor("Getting health check type for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
    58  		"AppName":   cmd.RequiredArgs.AppName,
    59  		"OrgName":   cmd.Config.TargetedOrganization().Name,
    60  		"SpaceName": cmd.Config.TargetedSpace().Name,
    61  		"Username":  user.Name,
    62  	})
    63  
    64  	processHealthChecks, warnings, err := cmd.Actor.GetApplicationProcessHealthChecksByNameAndSpace(cmd.RequiredArgs.AppName, cmd.Config.TargetedSpace().GUID)
    65  	cmd.UI.DisplayWarnings(warnings)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	cmd.UI.DisplayNewline()
    71  
    72  	if len(processHealthChecks) == 0 {
    73  		cmd.UI.DisplayText("App has no processes")
    74  		return nil
    75  	}
    76  
    77  	return cmd.DisplayProcessTable(processHealthChecks)
    78  }
    79  
    80  func (cmd GetHealthCheckCommand) DisplayProcessTable(processHealthChecks []v7action.ProcessHealthCheck) error {
    81  	table := [][]string{
    82  		{
    83  			cmd.UI.TranslateText("process"),
    84  			cmd.UI.TranslateText("health check"),
    85  			cmd.UI.TranslateText("endpoint (for http)"),
    86  			cmd.UI.TranslateText("invocation timeout"),
    87  		},
    88  	}
    89  
    90  	for _, healthCheck := range processHealthChecks {
    91  		invocationTimeout := healthCheck.InvocationTimeout
    92  		if invocationTimeout == 0 {
    93  			invocationTimeout = 1
    94  		}
    95  
    96  		table = append(table, []string{
    97  			healthCheck.ProcessType,
    98  			string(healthCheck.HealthCheckType),
    99  			healthCheck.Endpoint,
   100  			fmt.Sprint(invocationTimeout),
   101  		})
   102  	}
   103  
   104  	cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
   105  
   106  	return nil
   107  }