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