github.com/mem/u-root@v2.0.1-0.20181004165302-9b18b4636a33+incompatible/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/eval/re" 12 "github.com/u-root/u-root/cmds/elvish/eval/str" 13 "github.com/u-root/u-root/cmds/elvish/store/storedefs" 14 "github.com/u-root/u-root/cmds/elvish/util" 15 ) 16 17 var logger = util.GetLogger("[runtime] ") 18 19 // InitRuntime initializes the runtime. The caller is responsible for calling 20 // CleanupRuntime at some point. 21 func InitRuntime(binpath, sockpath, dbpath string) (*eval.Evaler, string) { 22 var dataDir string 23 var err error 24 25 // Determine data directory. 26 dataDir, err = storedefs.EnsureDataDir() 27 if err != nil { 28 fmt.Fprintln(os.Stderr, "warning: cannot create data directory ~/.elvish") 29 } 30 31 // Determine runtime directory. 32 _, err = getSecureRunDir() 33 if err != nil { 34 fmt.Fprintln(os.Stderr, "cannot get runtime dir /tmp/elvish-$uid, falling back to data dir ~/.elvish:", err) 35 } 36 37 ev := eval.NewEvaler() 38 ev.SetLibDir(filepath.Join(dataDir, "lib")) 39 ev.InstallModule("re", re.Ns) 40 ev.InstallModule("str", str.Ns) 41 return ev, dataDir 42 } 43 44 // CleanupRuntime cleans up the runtime. 45 func CleanupRuntime(ev *eval.Evaler) { 46 ev.Close() 47 } 48 49 var ( 50 ErrBadOwner = errors.New("bad owner") 51 ErrBadPermission = errors.New("bad permission") 52 )