github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/util/manifest/application.go (about)

     1  package manifest
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/types"
     8  )
     9  
    10  type Application struct {
    11  	Buildpack types.FilteredString
    12  	Command   types.FilteredString
    13  	// DiskQuota is the disk size in megabytes.
    14  	DiskQuota      types.NullByteSizeInMb
    15  	DockerImage    string
    16  	DockerUsername string
    17  	DockerPassword string
    18  	// EnvironmentVariables can be any valid json type (ie, strings not
    19  	// guaranteed, although CLI only ships strings).
    20  	EnvironmentVariables    map[string]string
    21  	HealthCheckHTTPEndpoint string
    22  	// HealthCheckType attribute defines the number of seconds that is allocated
    23  	// for starting an application.
    24  	HealthCheckTimeout int
    25  	HealthCheckType    string
    26  	Instances          types.NullInt
    27  	// Memory is the amount of memory in megabytes.
    28  	Memory    types.NullByteSizeInMb
    29  	Name      string
    30  	NoRoute   bool
    31  	Path      string
    32  	Routes    []string
    33  	Services  []string
    34  	StackName string
    35  }
    36  
    37  func (app Application) String() string {
    38  	return fmt.Sprintf(
    39  		"App Name: '%s', Buildpack IsSet: %t, Buildpack: '%s', Command IsSet: %t, Command: '%s', Disk Quota: '%s', Docker Image: '%s', Health Check HTTP Endpoint: '%s', Health Check Timeout: '%d', Health Check Type: '%s', Instances IsSet: %t, Instances: '%d', Memory: '%s', No-route: %t, Path: '%s', Routes: [%s], Services: [%s], Stack Name: '%s'",
    40  		app.Name,
    41  		app.Buildpack.IsSet,
    42  		app.Buildpack.Value,
    43  		app.Command.IsSet,
    44  		app.Command.Value,
    45  		app.DiskQuota,
    46  		app.DockerImage,
    47  		app.HealthCheckHTTPEndpoint,
    48  		app.HealthCheckTimeout,
    49  		app.HealthCheckType,
    50  		app.Instances.IsSet,
    51  		app.Instances.Value,
    52  		app.Memory,
    53  		app.NoRoute,
    54  		app.Path,
    55  		strings.Join(app.Routes, ", "),
    56  		strings.Join(app.Services, ", "),
    57  		app.StackName,
    58  	)
    59  }
    60  
    61  func (app Application) MarshalYAML() (interface{}, error) {
    62  	var m = rawManifestApplication{
    63  		Buildpack:               app.Buildpack.Value,
    64  		Command:                 app.Command.Value,
    65  		Docker:                  rawDockerInfo{Image: app.DockerImage, Username: app.DockerUsername},
    66  		EnvironmentVariables:    app.EnvironmentVariables,
    67  		HealthCheckHTTPEndpoint: app.HealthCheckHTTPEndpoint,
    68  		HealthCheckType:         app.HealthCheckType,
    69  		Name:                    app.Name,
    70  		NoRoute:                 app.NoRoute,
    71  		Path:                    app.Path,
    72  		Services:                app.Services,
    73  		StackName:               app.StackName,
    74  		Timeout:                 app.HealthCheckTimeout,
    75  	}
    76  	m.DiskQuota = app.DiskQuota.String()
    77  	m.Memory = app.Memory.String()
    78  
    79  	if app.Instances.IsSet {
    80  		m.Instances = &app.Instances.Value
    81  	}
    82  
    83  	for _, route := range app.Routes {
    84  		m.Routes = append(m.Routes, rawManifestRoute{Route: route})
    85  	}
    86  
    87  	return m, nil
    88  }
    89  
    90  func (app *Application) UnmarshalYAML(unmarshaller func(interface{}) error) error {
    91  	var m rawManifestApplication
    92  
    93  	err := unmarshaller(&m)
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	app.DockerImage = m.Docker.Image
    99  	app.DockerUsername = m.Docker.Username
   100  	app.HealthCheckHTTPEndpoint = m.HealthCheckHTTPEndpoint
   101  	app.HealthCheckType = m.HealthCheckType
   102  	app.Name = m.Name
   103  	app.NoRoute = m.NoRoute
   104  	app.Path = m.Path
   105  	app.Services = m.Services
   106  	app.StackName = m.StackName
   107  	app.HealthCheckTimeout = m.Timeout
   108  	app.EnvironmentVariables = m.EnvironmentVariables
   109  
   110  	app.Instances.ParseIntValue(m.Instances)
   111  
   112  	if fmtErr := app.DiskQuota.ParseStringValue(m.DiskQuota); fmtErr != nil {
   113  		return fmtErr
   114  	}
   115  
   116  	if fmtErr := app.Memory.ParseStringValue(m.Memory); fmtErr != nil {
   117  		return fmtErr
   118  	}
   119  
   120  	for _, route := range m.Routes {
   121  		app.Routes = append(app.Routes, route.Route)
   122  	}
   123  
   124  	// "null" values are identical to non-existant values in YAML. In order to
   125  	// detect if an explicit null is given, a manual existance check is required.
   126  	exists := map[string]interface{}{}
   127  	err = unmarshaller(&exists)
   128  	if err != nil {
   129  		return err
   130  	}
   131  
   132  	if _, ok := exists["buildpack"]; ok {
   133  		app.Buildpack.ParseValue(m.Buildpack)
   134  		app.Buildpack.IsSet = true
   135  	}
   136  
   137  	if _, ok := exists["command"]; ok {
   138  		app.Command.ParseValue(m.Command)
   139  		app.Command.IsSet = true
   140  	}
   141  
   142  	return nil
   143  }