github.com/masahide/goansible@v0.0.0-20160116054156-01eac649e9f2/environment.go (about)

     1  package goansible
     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 = &CLIReporter{out: os.Stdout, Debug: cfg.Debug, Output: cfg.ShowCommandOutput}
    21  	e.Vars = s
    22  	e.config = cfg
    23  
    24  	d, err := ioutil.TempDir("", "goansible")
    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  func (e *Environment) ReportStruct() error {
    38  	hostname, _ := os.Hostname()
    39  	e.report = &StructReporter{
    40  		Hostname: hostname,
    41  	}
    42  	return nil
    43  }
    44  
    45  var eNoTmpDir = errors.New("No tempdir available")
    46  
    47  func (e *Environment) TempFile(prefix string) (*os.File, error) {
    48  	if e.tmpDir == "" {
    49  		return nil, eNoTmpDir
    50  	}
    51  
    52  	dest, err := ioutil.TempFile(e.tmpDir, prefix)
    53  	return dest, err
    54  }
    55  
    56  func (e *Environment) Cleanup() {
    57  	os.RemoveAll(e.tmpDir)
    58  }
    59  
    60  func (e *Environment) SetPaths(n Paths) Paths {
    61  	cur := e.Paths
    62  	e.Paths = n
    63  	return cur
    64  }