github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/elvish/runtime/runtime.go (about)

     1  // Package runtime assembles the Elvish runtime.
     2  package runtime
     3  
     4  import (
     5  	"errors"
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/u-root/u-root/cmds/elvish/eval"
    11  	"github.com/u-root/u-root/cmds/elvish/store/storedefs"
    12  	"github.com/u-root/u-root/cmds/elvish/util"
    13  )
    14  
    15  var logger = util.GetLogger("[runtime] ")
    16  
    17  // InitRuntime initializes the runtime. The caller is responsible for calling
    18  // CleanupRuntime at some point.
    19  func InitRuntime(binpath, sockpath, dbpath string) (*eval.Evaler, string) {
    20  	var dataDir string
    21  	var err error
    22  
    23  	// Determine data directory.
    24  	dataDir, err = storedefs.EnsureDataDir()
    25  	if err != nil {
    26  		fmt.Fprintln(os.Stderr, "warning: cannot create data directory ~/.elvish")
    27  	}
    28  
    29  	// Determine runtime directory.
    30  	_, err = getSecureRunDir()
    31  	if err != nil {
    32  		fmt.Fprintln(os.Stderr, "cannot get runtime dir /tmp/elvish-$uid, falling back to data dir ~/.elvish:", err)
    33  	}
    34  
    35  	ev := eval.NewEvaler()
    36  	ev.SetLibDir(filepath.Join(dataDir, "lib"))
    37  	return ev, dataDir
    38  }
    39  
    40  // CleanupRuntime cleans up the runtime.
    41  func CleanupRuntime(ev *eval.Evaler) {
    42  	ev.Close()
    43  }
    44  
    45  var (
    46  	ErrBadOwner      = errors.New("bad owner")
    47  	ErrBadPermission = errors.New("bad permission")
    48  )