github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+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  	Buildpacks     []string
    13  	Command        types.FilteredString
    14  	DiskQuota      types.NullByteSizeInMb
    15  	DockerImage    string
    16  	DockerPassword string
    17  	DockerUsername string
    18  	Domain         string
    19  	DropletPath    string
    20  	// EnvironmentVariables can be any valid json type (ie, strings not
    21  	// guaranteed, although CLI only ships strings).
    22  	EnvironmentVariables    map[string]string
    23  	HealthCheckHTTPEndpoint string
    24  	HealthCheckTimeout      int
    25  	// HealthCheckType attribute defines the number of seconds that is allocated
    26  	// for starting an application.
    27  	HealthCheckType string
    28  	Hostname        string
    29  	Instances       types.NullInt
    30  	Memory          types.NullByteSizeInMb
    31  	Name            string
    32  	NoHostname      bool
    33  	NoRoute         bool
    34  	Path            string
    35  	RandomRoute     bool
    36  	Routes          []string
    37  	RoutePath       string
    38  	Services        []string
    39  	StackName       string
    40  
    41  	DeprecatedDomain     interface{}
    42  	DeprecatedDomains    interface{}
    43  	DeprecatedHost       interface{}
    44  	DeprecatedHosts      interface{}
    45  	DeprecatedNoHostname interface{}
    46  }
    47  
    48  func (app Application) MarshalYAML() (interface{}, error) {
    49  	var m = rawManifestApplication{
    50  		Buildpack:               app.Buildpack.Value,
    51  		Buildpacks:              app.Buildpacks,
    52  		Command:                 app.Command.Value,
    53  		Docker:                  rawDockerInfo{Image: app.DockerImage, Username: app.DockerUsername},
    54  		DropletPath:             app.DropletPath,
    55  		EnvironmentVariables:    app.EnvironmentVariables,
    56  		HealthCheckHTTPEndpoint: app.HealthCheckHTTPEndpoint,
    57  		HealthCheckType:         app.HealthCheckType,
    58  		Name:                    app.Name,
    59  		NoRoute:                 app.NoRoute,
    60  		Path:                    app.Path,
    61  		Services:                app.Services,
    62  		StackName:               app.StackName,
    63  		Timeout:                 app.HealthCheckTimeout,
    64  	}
    65  
    66  	m.DiskQuota = app.DiskQuota.String()
    67  	m.Memory = app.Memory.String()
    68  
    69  	if app.Instances.IsSet {
    70  		m.Instances = &app.Instances.Value
    71  	}
    72  
    73  	for _, route := range app.Routes {
    74  		m.Routes = append(m.Routes, rawManifestRoute{Route: route})
    75  	}
    76  
    77  	return m, nil
    78  }
    79  
    80  func (app Application) String() string {
    81  	return fmt.Sprintf(
    82  		"App Name: '%s', Buildpack IsSet: %t, Buildpack: '%s',  Buildpacks: [%s], Command IsSet: %t, Command: '%s', Disk Quota: '%s', Docker Image: '%s', Droplet Path: '%s', Health Check HTTP Endpoint: '%s', Health Check Timeout: '%d', Health Check Type: '%s', Hostname: '%s', Instances IsSet: %t, Instances: '%d', Memory: '%s', No-Hostname: %t, No-Route: %t, Path: '%s', RandomRoute: %t, RoutePath: '%s', Routes: [%s], Services: [%s], Stack Name: '%s'",
    83  		app.Name,
    84  		app.Buildpack.IsSet,
    85  		app.Buildpack.Value,
    86  		strings.Join(app.Buildpacks, ", "),
    87  		app.Command.IsSet,
    88  		app.Command.Value,
    89  		app.DiskQuota,
    90  		app.DockerImage,
    91  		app.DropletPath,
    92  		app.HealthCheckHTTPEndpoint,
    93  		app.HealthCheckTimeout,
    94  		app.HealthCheckType,
    95  		app.Hostname,
    96  		app.Instances.IsSet,
    97  		app.Instances.Value,
    98  		app.Memory,
    99  		app.NoHostname,
   100  		app.NoRoute,
   101  		app.Path,
   102  		app.RandomRoute,
   103  		app.RoutePath,
   104  		strings.Join(app.Routes, ", "),
   105  		strings.Join(app.Services, ", "),
   106  		app.StackName,
   107  	)
   108  }
   109  
   110  func (app *Application) UnmarshalYAML(unmarshaller func(interface{}) error) error {
   111  	var m rawManifestApplication
   112  
   113  	err := unmarshaller(&m)
   114  	if err != nil {
   115  		return err
   116  	}
   117  	app.DeprecatedDomain = m.DeprecatedDomain
   118  	app.DeprecatedDomains = m.DeprecatedDomains
   119  	app.DeprecatedHost = m.DeprecatedHost
   120  	app.DeprecatedHosts = m.DeprecatedHosts
   121  	app.DeprecatedNoHostname = m.DeprecatedNoHostname
   122  	app.DockerImage = m.Docker.Image
   123  	app.DockerUsername = m.Docker.Username
   124  	app.DropletPath = m.DropletPath
   125  	app.HealthCheckHTTPEndpoint = m.HealthCheckHTTPEndpoint
   126  	app.HealthCheckType = m.HealthCheckType
   127  	app.Name = m.Name
   128  	app.NoRoute = m.NoRoute
   129  	app.Path = m.Path
   130  	app.RandomRoute = m.RandomRoute
   131  	app.Services = m.Services
   132  	app.StackName = m.StackName
   133  	app.HealthCheckTimeout = m.Timeout
   134  	app.EnvironmentVariables = m.EnvironmentVariables
   135  
   136  	app.Instances.ParseIntValue(m.Instances)
   137  
   138  	if fmtErr := app.DiskQuota.ParseStringValue(m.DiskQuota); fmtErr != nil {
   139  		return fmtErr
   140  	}
   141  
   142  	if fmtErr := app.Memory.ParseStringValue(m.Memory); fmtErr != nil {
   143  		return fmtErr
   144  	}
   145  
   146  	for _, route := range m.Routes {
   147  		app.Routes = append(app.Routes, route.Route)
   148  	}
   149  
   150  	// "null" values are identical to non-existant values in YAML. In order to
   151  	// detect if an explicit null is given, a manual existance check is required.
   152  	exists := map[string]interface{}{}
   153  	err = unmarshaller(&exists)
   154  	if err != nil {
   155  		return err
   156  	}
   157  
   158  	if _, ok := exists["buildpack"]; ok {
   159  		app.Buildpack.ParseValue(m.Buildpack)
   160  		app.Buildpack.IsSet = true
   161  	}
   162  
   163  	if buildpacks, ok := exists["buildpacks"]; ok {
   164  		if buildpacks == nil {
   165  			return EmptyBuildpacksError{}
   166  		}
   167  
   168  		app.Buildpacks = []string{}
   169  		for _, buildpack := range m.Buildpacks {
   170  			app.Buildpacks = append(app.Buildpacks, buildpack)
   171  		}
   172  	}
   173  
   174  	if _, ok := exists["command"]; ok {
   175  		app.Command.ParseValue(m.Command)
   176  		app.Command.IsSet = true
   177  	}
   178  
   179  	return nil
   180  }