github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/util/manifest/manifest.go (about)

     1  package manifest
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  
     7  	yaml "gopkg.in/yaml.v2"
     8  )
     9  
    10  type Manifest struct {
    11  	Applications []Application `yaml:"applications"`
    12  }
    13  
    14  func (manifest *Manifest) UnmarshalYAML(unmarshal func(interface{}) error) error {
    15  	var raw rawManifest
    16  	err := unmarshal(&raw)
    17  	if err != nil {
    18  		return err
    19  	}
    20  
    21  	if raw.containsDeprecatedFields() {
    22  		return UnsupportedFieldsError{}
    23  	}
    24  
    25  	manifest.Applications = raw.Applications
    26  	return nil
    27  }
    28  
    29  // ReadAndMergeManifests reads the manifest at provided path and returns a
    30  // fully merged set of applications.
    31  func ReadAndMergeManifests(pathToManifest string) ([]Application, error) {
    32  	// Read all manifest files
    33  	raw, err := ioutil.ReadFile(pathToManifest)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	var manifest Manifest
    39  	err = yaml.Unmarshal(raw, &manifest)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	for i, app := range manifest.Applications {
    45  		if app.Path != "" && !filepath.IsAbs(app.Path) {
    46  			manifest.Applications[i].Path = filepath.Join(filepath.Dir(pathToManifest), app.Path)
    47  		}
    48  	}
    49  
    50  	// Merge all manifest files
    51  	return manifest.Applications, err
    52  }
    53  
    54  // WriteApplicationManifest writes the provided application to the given
    55  // filepath. If the filepath does not exist, it will create it.
    56  func WriteApplicationManifest(application Application, filePath string) error {
    57  	manifest := Manifest{Applications: []Application{application}}
    58  	manifestBytes, err := yaml.Marshal(manifest)
    59  	if err != nil {
    60  		return ManifestCreationError{Err: err}
    61  	}
    62  
    63  	err = ioutil.WriteFile(filePath, manifestBytes, 0644)
    64  	if err != nil {
    65  		return ManifestCreationError{Err: err}
    66  	}
    67  
    68  	return nil
    69  }