github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/util/manifestparser/application.go (about)

     1  package manifestparser
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     8  )
     9  
    10  type Docker struct {
    11  	Image    string `yaml:"image,omitempty"`
    12  	Username string `yaml:"username,omitempty"`
    13  }
    14  
    15  // ApplicationModel can be accessed through the top level Application struct To
    16  // add a field for the CLI to extract from the manifest, just add it to this
    17  // struct.
    18  type Application struct {
    19  	Name                    string                   `yaml:"name"`
    20  	DiskQuota               string                   `yaml:"disk-quota,omitempty"`
    21  	Docker                  *Docker                  `yaml:"docker,omitempty"`
    22  	HealthCheckType         constant.HealthCheckType `yaml:"health-check-type,omitempty"`
    23  	HealthCheckEndpoint     string                   `yaml:"health-check-http-endpoint,omitempty"`
    24  	HealthCheckTimeout      int64                    `yaml:"timeout,omitempty"`
    25  	Instances               *int                     `yaml:"instances,omitempty"`
    26  	Path                    string                   `yaml:"path,omitempty"`
    27  	Processes               []Process                `yaml:"processes,omitempty"`
    28  	Memory                  string                   `yaml:"memory,omitempty"`
    29  	NoRoute                 bool                     `yaml:"no-route,omitempty"`
    30  	RandomRoute             bool                     `yaml:"random-route,omitempty"`
    31  	DefaultRoute            bool                     `yaml:"default-route,omitempty"`
    32  	Stack                   string                   `yaml:"stack,omitempty"`
    33  	RemainingManifestFields map[string]interface{}   `yaml:"-,inline"`
    34  }
    35  
    36  func (application Application) HasBuildpacks() bool {
    37  	_, ok := application.RemainingManifestFields["buildpacks"]
    38  	return ok
    39  }
    40  
    41  func (application *Application) SetBuildpacks(buildpacks []string) {
    42  	if application.RemainingManifestFields == nil {
    43  		application.RemainingManifestFields = map[string]interface{}{}
    44  	}
    45  
    46  	application.RemainingManifestFields["buildpacks"] = buildpacks
    47  }
    48  
    49  func (application *Application) SetStartCommand(command string) {
    50  	if application.RemainingManifestFields == nil {
    51  		application.RemainingManifestFields = map[string]interface{}{}
    52  	}
    53  
    54  	if command == "" {
    55  		application.RemainingManifestFields["command"] = nil
    56  	} else {
    57  		application.RemainingManifestFields["command"] = command
    58  	}
    59  }
    60  
    61  func (application *Application) UnmarshalYAML(unmarshal func(v interface{}) error) error {
    62  	// This prevents infinite recursion. The alias type does not implement the unmarshaller interface
    63  	// so by casting application to a alias pointer, it will unmarshal in to the same memory without calling
    64  	// UnmarshalYAML on itself infinite times
    65  	type Alias Application
    66  	aliasPntr := (*Alias)(application)
    67  
    68  	err := unmarshal(aliasPntr)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	err = unmarshal(&application.RemainingManifestFields)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	value := reflect.ValueOf(*application)
    79  	removeDuplicateMapKeys(value, application.RemainingManifestFields)
    80  	// old style was `disk_quota` (underscore not hyphen)
    81  	// we maintain backwards-compatibility by supporting both flavors
    82  	if application.RemainingManifestFields["disk_quota"] != nil {
    83  		if application.DiskQuota != "" {
    84  			return errors.New("cannot define both `disk_quota` and `disk-quota`")
    85  		}
    86  		diskQuota, ok := application.RemainingManifestFields["disk_quota"].(string)
    87  		if !ok {
    88  			return errors.New("`disk_quota` must be a string")
    89  		}
    90  		application.DiskQuota = diskQuota
    91  		delete(application.RemainingManifestFields, "disk_quota")
    92  	}
    93  
    94  	return nil
    95  }