github.com/vektra/tachyon@v0.0.0-20150921164542-0da4f3861aef/environment.go (about)

     1  package tachyon
     2  
     3  import (
     4  	"errors"
     5  	"io/ioutil"
     6  	"os"
     7  )
     8  
     9  type Environment struct {
    10  	Vars   Scope
    11  	report Reporter
    12  	config *Config
    13  	tmpDir string
    14  
    15  	Paths Paths
    16  }
    17  
    18  func NewEnv(s Scope, cfg *Config) *Environment {
    19  	e := new(Environment)
    20  	e.report = sCLIReporter
    21  	e.Vars = s
    22  	e.config = cfg
    23  
    24  	d, err := ioutil.TempDir("", "tachyon")
    25  	if err == nil {
    26  		e.tmpDir = d
    27  	}
    28  
    29  	e.Paths = SimplePath{"."}
    30  
    31  	return e
    32  }
    33  
    34  func (e *Environment) ReportJSON() {
    35  	e.report = sJsonChunkReporter
    36  }
    37  
    38  var eNoTmpDir = errors.New("No tempdir available")
    39  
    40  func (e *Environment) TempFile(prefix string) (*os.File, error) {
    41  	if e.tmpDir == "" {
    42  		return nil, eNoTmpDir
    43  	}
    44  
    45  	dest, err := ioutil.TempFile(e.tmpDir, prefix)
    46  	return dest, err
    47  }
    48  
    49  func (e *Environment) Cleanup() {
    50  	os.RemoveAll(e.tmpDir)
    51  }
    52  
    53  func (e *Environment) SetPaths(n Paths) Paths {
    54  	cur := e.Paths
    55  	e.Paths = n
    56  	return cur
    57  }