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