github.com/bilus/oya@v0.0.3-0.20190301162104-da4acbd394c6/cmd/internal/render.go (about)

     1  package internal
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"path/filepath"
     7  
     8  	"github.com/bilus/oya/pkg/project"
     9  	"github.com/bilus/oya/pkg/template"
    10  )
    11  
    12  type ErrNoScope struct {
    13  	Scope       string
    14  	OyafilePath string
    15  }
    16  
    17  func (err ErrNoScope) Error() string {
    18  	return fmt.Sprintf("Scope not found in %v: %q missing or cannot be used as a scope", err.OyafilePath, err.Scope)
    19  }
    20  
    21  func Render(oyafilePath, templatePath string, excludedPaths []string, outputPath string,
    22  	autoScope bool, scopePath string, stdout, stderr io.Writer) error {
    23  	installDir, err := installDir()
    24  	if err != nil {
    25  		return err
    26  	}
    27  	oyafileFullPath, err := filepath.Abs(oyafilePath)
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	proj, err := project.Detect(oyafileFullPath, installDir)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	o, found, err := proj.Oyafile(oyafilePath)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	dt, err := proj.Deps()
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	err = o.Build(dt)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	var values template.Scope
    53  	if found {
    54  		if autoScope && scopePath == "" {
    55  			scopePath, _ = lookupOyaScope()
    56  		}
    57  		if scopePath != "" {
    58  			values, err = o.Values.GetScopeAt(scopePath)
    59  		} else {
    60  			values = o.Values
    61  		}
    62  		if err != nil {
    63  			// BUG(bilus): Ignoring err.
    64  			return ErrNoScope{Scope: scopePath, OyafilePath: oyafilePath}
    65  		}
    66  	}
    67  
    68  	return template.RenderAll(templatePath, excludedPaths, outputPath, values)
    69  }