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

     1  package shared
     2  
     3  import (
     4  	"code.cloudfoundry.org/bytefmt"
     5  	"code.cloudfoundry.org/cli/actor/pushaction"
     6  	"code.cloudfoundry.org/cli/util/ui"
     7  )
     8  
     9  func GetApplicationChanges(appConfig pushaction.ApplicationConfig) []ui.Change {
    10  	changes := []ui.Change{
    11  		{
    12  			Header:       "name:",
    13  			CurrentValue: appConfig.CurrentApplication.Name,
    14  			NewValue:     appConfig.DesiredApplication.Name,
    15  		},
    16  	}
    17  
    18  	if appConfig.DesiredApplication.DockerImage != "" {
    19  		changes = append(changes,
    20  			ui.Change{
    21  				Header:       "docker image:",
    22  				CurrentValue: appConfig.CurrentApplication.DockerImage,
    23  				NewValue:     appConfig.DesiredApplication.DockerImage,
    24  			})
    25  
    26  		if appConfig.CurrentApplication.DockerCredentials.Username != "" || appConfig.DesiredApplication.DockerCredentials.Username != "" {
    27  			changes = append(changes,
    28  				ui.Change{
    29  					Header:       "docker username:",
    30  					CurrentValue: appConfig.CurrentApplication.DockerCredentials.Username,
    31  					NewValue:     appConfig.DesiredApplication.DockerCredentials.Username,
    32  				})
    33  		}
    34  	} else {
    35  		changes = append(changes,
    36  			ui.Change{
    37  				Header:       "path:",
    38  				CurrentValue: appConfig.Path,
    39  				NewValue:     appConfig.Path,
    40  			})
    41  	}
    42  
    43  	// Existing buildpack and existing detected buildpack are mutually exclusive
    44  	oldBuildpack := appConfig.CurrentApplication.CalculatedBuildpack()
    45  	newBuildpack := appConfig.DesiredApplication.CalculatedBuildpack()
    46  	if oldBuildpack != "" || newBuildpack != "" {
    47  		changes = append(changes,
    48  			ui.Change{
    49  				Header:       "buildpack:",
    50  				CurrentValue: oldBuildpack,
    51  				NewValue:     newBuildpack,
    52  			})
    53  	}
    54  
    55  	// Existing command and existing detected start command are mutually exclusive
    56  	oldCommand := appConfig.CurrentApplication.CalculatedCommand()
    57  	newCommand := appConfig.DesiredApplication.CalculatedCommand()
    58  	if oldCommand != "" || newCommand != "" {
    59  		changes = append(changes,
    60  			ui.Change{
    61  				Header:       "command:",
    62  				CurrentValue: oldCommand,
    63  				NewValue:     newCommand,
    64  			})
    65  	}
    66  
    67  	if appConfig.CurrentApplication.DiskQuota.IsSet || appConfig.DesiredApplication.DiskQuota.IsSet {
    68  		var currentDiskQuota string
    69  		if appConfig.CurrentApplication.DiskQuota.IsSet {
    70  			currentDiskQuota = appConfig.CurrentApplication.DiskQuota.String()
    71  		}
    72  		changes = append(changes,
    73  			ui.Change{
    74  				Header:       "disk quota:",
    75  				CurrentValue: currentDiskQuota,
    76  				NewValue:     appConfig.DesiredApplication.DiskQuota.String(),
    77  			})
    78  	}
    79  
    80  	if appConfig.CurrentApplication.HealthCheckHTTPEndpoint != "" || appConfig.DesiredApplication.HealthCheckHTTPEndpoint != "" {
    81  		changes = append(changes,
    82  			ui.Change{
    83  				Header:       "health check http endpoint:",
    84  				CurrentValue: appConfig.CurrentApplication.HealthCheckHTTPEndpoint,
    85  				NewValue:     appConfig.DesiredApplication.HealthCheckHTTPEndpoint,
    86  			})
    87  	}
    88  
    89  	if appConfig.CurrentApplication.HealthCheckTimeout != 0 || appConfig.DesiredApplication.HealthCheckTimeout != 0 {
    90  		changes = append(changes,
    91  			ui.Change{
    92  				Header:       "health check timeout:",
    93  				CurrentValue: appConfig.CurrentApplication.HealthCheckTimeout,
    94  				NewValue:     appConfig.DesiredApplication.HealthCheckTimeout,
    95  			})
    96  	}
    97  
    98  	if appConfig.CurrentApplication.HealthCheckType != "" || appConfig.DesiredApplication.HealthCheckType != "" {
    99  		changes = append(changes,
   100  			ui.Change{
   101  				Header:       "health check type:",
   102  				CurrentValue: string(appConfig.CurrentApplication.HealthCheckType),
   103  				NewValue:     string(appConfig.DesiredApplication.HealthCheckType),
   104  			})
   105  	}
   106  
   107  	if appConfig.CurrentApplication.Instances.IsSet || appConfig.DesiredApplication.Instances.IsSet {
   108  		changes = append(changes,
   109  			ui.Change{
   110  				Header:       "instances:",
   111  				CurrentValue: appConfig.CurrentApplication.Instances,
   112  				NewValue:     appConfig.DesiredApplication.Instances,
   113  			})
   114  	}
   115  
   116  	if appConfig.CurrentApplication.Memory.IsSet || appConfig.DesiredApplication.Memory.IsSet {
   117  		var currentMemory string
   118  		if appConfig.CurrentApplication.Memory.IsSet {
   119  			currentMemory = appConfig.CurrentApplication.Memory.String()
   120  		}
   121  		changes = append(changes,
   122  			ui.Change{
   123  				Header:       "memory:",
   124  				CurrentValue: currentMemory,
   125  				NewValue:     appConfig.DesiredApplication.Memory.String(),
   126  			})
   127  	}
   128  
   129  	if appConfig.CurrentApplication.Stack.Name != "" || appConfig.DesiredApplication.Stack.Name != "" {
   130  		changes = append(changes,
   131  			ui.Change{
   132  				Header:       "stack:",
   133  				CurrentValue: appConfig.CurrentApplication.Stack.Name,
   134  				NewValue:     appConfig.DesiredApplication.Stack.Name,
   135  			})
   136  	}
   137  
   138  	var oldServices []string
   139  	for name := range appConfig.CurrentServices {
   140  		oldServices = append(oldServices, name)
   141  	}
   142  
   143  	var newServices []string
   144  	for name := range appConfig.DesiredServices {
   145  		newServices = append(newServices, name)
   146  	}
   147  
   148  	changes = append(changes,
   149  		ui.Change{
   150  			Header:       "services:",
   151  			CurrentValue: oldServices,
   152  			NewValue:     newServices,
   153  		})
   154  
   155  	changes = append(changes,
   156  		ui.Change{
   157  			Header:       "env:",
   158  			CurrentValue: appConfig.CurrentApplication.EnvironmentVariables,
   159  			NewValue:     appConfig.DesiredApplication.EnvironmentVariables,
   160  		})
   161  
   162  	var currentRoutes []string
   163  	for _, route := range appConfig.CurrentRoutes {
   164  		currentRoutes = append(currentRoutes, route.String())
   165  	}
   166  
   167  	var desiredRotues []string
   168  	for _, route := range appConfig.DesiredRoutes {
   169  		desiredRotues = append(desiredRotues, route.String())
   170  	}
   171  
   172  	changes = append(changes,
   173  		ui.Change{
   174  			Header:       "routes:",
   175  			CurrentValue: currentRoutes,
   176  			NewValue:     desiredRotues,
   177  		})
   178  
   179  	return changes
   180  }
   181  
   182  func SelectNonBlankValue(str ...string) string {
   183  	for _, s := range str {
   184  		if s != "" {
   185  			return s
   186  		}
   187  	}
   188  	return ""
   189  }
   190  
   191  func MegabytesToString(value uint64) string {
   192  	return bytefmt.ByteSize(bytefmt.MEGABYTE * uint64(value))
   193  }