github.com/madlambda/nash@v0.2.2-0.20230113003044-f2284521680b/examples_test.go (about)

     1  package nash_test
     2  
     3  import (
     4  	"os"
     5  	"io/ioutil"
     6  	
     7  	"github.com/madlambda/nash"
     8  )
     9  
    10  func Example() {
    11  
    12  	nashpath,cleanup := tmpdir()
    13  	defer cleanup()
    14  	
    15  	nashroot, cleanup := tmpdir()
    16  	defer cleanup()
    17  
    18  	nash, err := nash.New(nashpath, nashroot)
    19  
    20  	if err != nil {
    21  		panic(err)
    22  	}
    23  
    24  	// Execute a script from string
    25  	err = nash.ExecuteString("-input-", `echo Hello World`)
    26  
    27  	if err != nil {
    28  		panic(err)
    29  	}
    30  
    31  	// Output: Hello World
    32  }
    33  
    34  func tmpdir() (string, func()) {	
    35  	dir, err := ioutil.TempDir("", "nash-tests")
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  	
    40  	return dir, func() {
    41  		err := os.RemoveAll(dir)
    42  		if err != nil {
    43  			panic(err)
    44  		}
    45  	}
    46  }
    47