github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/harness/downgrader/yaml/parse.go (about)

     1  package yaml
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"io/ioutil"
     7  	"os"
     8  
     9  	v1 "github.com/drone/spec/dist/go"
    10  )
    11  
    12  // Parse parses the configuration from io.Reader r.
    13  func Parse(r io.Reader) ([]*v1.Config, error) {
    14  	var resources []*v1.Config
    15  
    16  	// Read all data from the reader
    17  	b, err := ioutil.ReadAll(r)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	docs := bytes.Split(b, []byte("\n---\n"))
    23  
    24  	for _, doc := range docs {
    25  		r := bytes.NewReader(doc)
    26  		resource, err := v1.Parse(r)
    27  		if err != nil {
    28  			return nil, err
    29  		}
    30  		resources = append(resources, resource)
    31  	}
    32  
    33  	return resources, nil
    34  }
    35  
    36  // ParseBytes parses the configuration from bytes b.
    37  func ParseBytes(b []byte) ([]*v1.Config, error) {
    38  	return Parse(
    39  		bytes.NewBuffer(b),
    40  	)
    41  }
    42  
    43  // ParseString parses the configuration from string s.
    44  func ParseString(s string) ([]*v1.Config, error) {
    45  	return ParseBytes(
    46  		[]byte(s),
    47  	)
    48  }
    49  
    50  // ParseFile parses the configuration from path p.
    51  func ParseFile(p string) ([]*v1.Config, error) {
    52  	f, err := os.Open(p)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	defer f.Close()
    57  	return Parse(f)
    58  }