github.com/mavryk-network/mvgo@v1.19.9/internal/compose/yaml.go (about)

     1  // Copyright (c) 2023 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc, abdul@blockwatch.cc
     3  
     4  package compose
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"gopkg.in/yaml.v3"
    12  )
    13  
    14  var (
    15  	yamlExts = map[string]struct{}{
    16  		".yml":  {},
    17  		".yaml": {},
    18  	}
    19  )
    20  
    21  // ParseFile parses file based on path given to spec
    22  func ParseFile[T any](fpath string) (*T, error) {
    23  	ext := filepath.Ext(fpath)
    24  	if _, ok := yamlExts[ext]; !ok {
    25  		return nil, fmt.Errorf("file extension %q is not supported", ext)
    26  	}
    27  	buf, err := os.ReadFile(fpath)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	var spec T
    32  	err = yaml.Unmarshal(buf, &spec)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return &spec, nil
    37  }