github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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.containsInheritanceField() {
    22  		return InheritanceFieldError{}
    23  	}
    24  
    25  	if globals := raw.containsGlobalFields(); len(globals) > 0 {
    26  		return GlobalFieldsError{Fields: globals}
    27  	}
    28  
    29  	manifest.Applications = raw.Applications
    30  	return nil
    31  }
    32  
    33  // ReadAndMergeManifests reads the manifest at provided path and returns a
    34  // fully merged set of applications.
    35  func ReadAndMergeManifests(pathToManifest string) ([]Application, error) {
    36  	raw, err := ioutil.ReadFile(pathToManifest)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	var manifest Manifest
    42  	err = yaml.Unmarshal(raw, &manifest)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	// turns the relative path into an absolute path
    48  	for i, app := range manifest.Applications {
    49  		if app.Path != "" && !filepath.IsAbs(app.Path) {
    50  			manifest.Applications[i].Path = filepath.Join(filepath.Dir(pathToManifest), app.Path)
    51  		}
    52  	}
    53  
    54  	return manifest.Applications, err
    55  }
    56  
    57  // WriteApplicationManifest writes the provided application to the given
    58  // filepath. If the filepath does not exist, it will create it.
    59  func WriteApplicationManifest(application Application, filePath string) error {
    60  	manifest := Manifest{Applications: []Application{application}}
    61  	manifestBytes, err := yaml.Marshal(manifest)
    62  	if err != nil {
    63  		return ManifestCreationError{Err: err}
    64  	}
    65  
    66  	err = ioutil.WriteFile(filePath, manifestBytes, 0644)
    67  	if err != nil {
    68  		return ManifestCreationError{Err: err}
    69  	}
    70  
    71  	return nil
    72  }