github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/util/manifestparser/application.go (about)

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