github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/util/manifestparser/process.go (about)

     1  package manifestparser
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  )
    10  
    11  type Process struct {
    12  	DiskQuota               string                   `yaml:"disk_quota,omitempty"`
    13  	HealthCheckEndpoint     string                   `yaml:"health-check-http-endpoint,omitempty"`
    14  	HealthCheckType         constant.HealthCheckType `yaml:"health-check-type,omitempty"`
    15  	HealthCheckTimeout      int64                    `yaml:"timeout,omitempty"`
    16  	Instances               *int                     `yaml:"instances,omitempty"`
    17  	Memory                  string                   `yaml:"memory,omitempty"`
    18  	Type                    string                   `yaml:"type"`
    19  	LogRateLimit            string                   `yaml:"log-rate-limit-per-second,omitempty"`
    20  	RemainingManifestFields map[string]interface{}   `yaml:"-,inline"`
    21  }
    22  
    23  func (process *Process) SetStartCommand(command string) {
    24  	if process.RemainingManifestFields == nil {
    25  		process.RemainingManifestFields = map[string]interface{}{}
    26  	}
    27  
    28  	if command == "" {
    29  		process.RemainingManifestFields["command"] = nil
    30  	} else {
    31  		process.RemainingManifestFields["command"] = command
    32  	}
    33  }
    34  
    35  func (process *Process) UnmarshalYAML(unmarshal func(v interface{}) error) error {
    36  	// This prevents infinite recursion. The Alias type does not implement the unmarshaller interface
    37  	// so by casting application to a alias pointer, it will unmarshal into the same memory without calling
    38  	// UnmarshalYAML on itself infinite times
    39  	type Alias Process
    40  	aliasPntr := (*Alias)(process)
    41  
    42  	err := unmarshal(aliasPntr)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	err = unmarshal(&process.RemainingManifestFields)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	value := reflect.ValueOf(*process)
    53  	removeDuplicateMapKeys(value, process.RemainingManifestFields)
    54  	// old style was `disk_quota` (underscore not hyphen)
    55  	// we maintain backwards-compatibility by supporting both flavors
    56  	if process.RemainingManifestFields["disk-quota"] != nil {
    57  		if process.DiskQuota != "" {
    58  			return errors.New("cannot define both `disk_quota` and `disk-quota`")
    59  		}
    60  		diskQuota, ok := process.RemainingManifestFields["disk-quota"].(string)
    61  		if !ok {
    62  			return errors.New("`disk-quota` must be a string")
    63  		}
    64  		process.DiskQuota = diskQuota
    65  		delete(process.RemainingManifestFields, "disk-quota")
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func removeDuplicateMapKeys(model reflect.Value, fieldMap map[string]interface{}) {
    72  	for i := 0; i < model.NumField(); i++ {
    73  		structField := model.Type().Field(i)
    74  
    75  		yamlTag := strings.Split(structField.Tag.Get("yaml"), ",")
    76  		yamlKey := yamlTag[0]
    77  
    78  		if yamlKey == "" {
    79  			yamlKey = structField.Name
    80  		}
    81  
    82  		delete(fieldMap, yamlKey)
    83  	}
    84  }