github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/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  	LogRateLimit            string                   `yaml:"log-rate-limit-per-second,omitempty"`
    34  	RemainingManifestFields map[string]interface{}   `yaml:"-,inline"`
    35  }
    36  
    37  func (application Application) HasBuildpacks() bool {
    38  	_, ok := application.RemainingManifestFields["buildpacks"]
    39  	return ok
    40  }
    41  
    42  func (application *Application) SetBuildpacks(buildpacks []string) {
    43  	if application.RemainingManifestFields == nil {
    44  		application.RemainingManifestFields = map[string]interface{}{}
    45  	}
    46  
    47  	application.RemainingManifestFields["buildpacks"] = buildpacks
    48  }
    49  
    50  func (application *Application) SetStartCommand(command string) {
    51  	if application.RemainingManifestFields == nil {
    52  		application.RemainingManifestFields = map[string]interface{}{}
    53  	}
    54  
    55  	if command == "" {
    56  		application.RemainingManifestFields["command"] = nil
    57  	} else {
    58  		application.RemainingManifestFields["command"] = command
    59  	}
    60  }
    61  
    62  func (application *Application) UnmarshalYAML(unmarshal func(v interface{}) error) error {
    63  	// This prevents infinite recursion. The alias type does not implement the unmarshaller interface
    64  	// so by casting application to a alias pointer, it will unmarshal into the same memory without calling
    65  	// UnmarshalYAML on itself infinite times
    66  	type Alias Application
    67  	aliasPntr := (*Alias)(application)
    68  
    69  	err := unmarshal(aliasPntr)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	err = unmarshal(&application.RemainingManifestFields)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	value := reflect.ValueOf(*application)
    80  	removeDuplicateMapKeys(value, application.RemainingManifestFields)
    81  	// old style was `disk_quota` (underscore not hyphen)
    82  	// we maintain backwards-compatibility by supporting both flavors
    83  	if application.RemainingManifestFields["disk_quota"] != nil {
    84  		if application.DiskQuota != "" {
    85  			return errors.New("cannot define both `disk_quota` and `disk-quota`")
    86  		}
    87  		diskQuota, ok := application.RemainingManifestFields["disk_quota"].(string)
    88  		if !ok {
    89  			return errors.New("`disk_quota` must be a string")
    90  		}
    91  		application.DiskQuota = diskQuota
    92  		delete(application.RemainingManifestFields, "disk_quota")
    93  	}
    94  
    95  	return nil
    96  }