github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/command/v2/shared/application_info.go (about)

     1  package shared
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/bytefmt"
     9  	"code.cloudfoundry.org/cli/actor/v2action"
    10  	"code.cloudfoundry.org/cli/command"
    11  )
    12  
    13  // DisplayAppSummary displays the application summary to the UI, and optionally
    14  // the command to start the app.
    15  func DisplayAppSummary(ui command.UI, appSummary v2action.ApplicationSummary, displayStartCommand bool) {
    16  	instances := fmt.Sprintf("%d/%d", appSummary.StartingOrRunningInstanceCount(), appSummary.Instances.Value)
    17  
    18  	usage := ui.TranslateText(
    19  		"{{.MemorySize}} x {{.NumInstances}} instances",
    20  		map[string]interface{}{
    21  			"MemorySize":   appSummary.Memory,
    22  			"NumInstances": appSummary.Instances.Value,
    23  		})
    24  
    25  	formattedRoutes := []string{}
    26  	for _, route := range appSummary.Routes {
    27  		formattedRoutes = append(formattedRoutes, route.String())
    28  	}
    29  	routes := strings.Join(formattedRoutes, ", ")
    30  
    31  	table := [][]string{
    32  		{ui.TranslateText("name:"), appSummary.Name},
    33  		{ui.TranslateText("requested state:"), strings.ToLower(string(appSummary.State))},
    34  		{ui.TranslateText("instances:"), instances},
    35  		{ui.TranslateText("usage:"), usage},
    36  		{ui.TranslateText("routes:"), routes},
    37  		{ui.TranslateText("last uploaded:"), ui.UserFriendlyDate(appSummary.PackageUpdatedAt)},
    38  		{ui.TranslateText("stack:"), appSummary.Stack.Name},
    39  	}
    40  
    41  	if appSummary.DockerImage == "" {
    42  		table = append(table, []string{ui.TranslateText("buildpack:"), appSummary.Application.CalculatedBuildpack()})
    43  	} else {
    44  		table = append(table, []string{ui.TranslateText("docker image:"), appSummary.DockerImage})
    45  	}
    46  
    47  	if displayStartCommand {
    48  		table = append(table, []string{ui.TranslateText("start command:"), appSummary.Application.CalculatedCommand()})
    49  	}
    50  
    51  	if appSummary.IsolationSegment != "" {
    52  		table = append(table[:3], append([][]string{
    53  			{ui.TranslateText("isolation segment:"), appSummary.IsolationSegment},
    54  		}, table[3:]...)...)
    55  	}
    56  
    57  	ui.DisplayKeyValueTableForApp(table)
    58  	ui.DisplayNewline()
    59  
    60  	if len(appSummary.RunningInstances) == 0 {
    61  		ui.DisplayText("There are no running instances of this app.")
    62  	} else {
    63  		displayAppInstances(ui, appSummary.RunningInstances)
    64  	}
    65  }
    66  
    67  func displayAppInstances(ui command.UI, instances []v2action.ApplicationInstanceWithStats) {
    68  	table := [][]string{
    69  		{
    70  			"",
    71  			ui.TranslateText("state"),
    72  			ui.TranslateText("since"),
    73  			ui.TranslateText("cpu"),
    74  			ui.TranslateText("memory"),
    75  			ui.TranslateText("disk"),
    76  			ui.TranslateText("details"),
    77  		},
    78  	}
    79  
    80  	for _, instance := range instances {
    81  		table = append(
    82  			table,
    83  			[]string{
    84  				fmt.Sprintf("#%d", instance.ID),
    85  				ui.TranslateText(strings.ToLower(string(instance.State))),
    86  				zuluDate(instance.TimeSinceCreation()),
    87  				fmt.Sprintf("%.1f%%", instance.CPU*100),
    88  				fmt.Sprintf("%s of %s", bytefmt.ByteSize(uint64(instance.Memory)), bytefmt.ByteSize(uint64(instance.MemoryQuota))),
    89  				fmt.Sprintf("%s of %s", bytefmt.ByteSize(uint64(instance.Disk)), bytefmt.ByteSize(uint64(instance.DiskQuota))),
    90  				instance.Details,
    91  			})
    92  	}
    93  
    94  	ui.DisplayInstancesTableForApp(table)
    95  }
    96  
    97  // zuluDate converts the time to UTC and then formats it to ISO8601.
    98  func zuluDate(input time.Time) string {
    99  	// "2006-01-02T15:04:05Z07:00"
   100  	return input.UTC().Format(time.RFC3339)
   101  }