Remove store.DataDir and store the data dir in Evaler.

This commit is contained in:
Qi Xiao 2017-01-27 21:25:32 +00:00
parent 20fdf1f8ed
commit a2fcfcfae3
4 changed files with 15 additions and 21 deletions

View File

@ -5,16 +5,18 @@ package eval
// Builtin special forms.
import (
"errors"
"fmt"
"os"
"strings"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/store"
)
type compileBuiltin func(*compiler, *parse.Form) OpFunc
var ErrNoDataDir = errors.New("There is no data directory")
var builtinSpecials map[string]compileBuiltin
// BuiltinSpecialNames contains all names of builtin special forms. It is
@ -169,9 +171,10 @@ func use(ec *EvalCtx, modname string, pfilename *string) {
maybeThrow(err)
} else {
// No filename; defaulting to $datadir/$modname.elv.
dataDir, err := store.DataDir()
maybeThrow(err)
filename = dataDir + "/" + strings.Replace(modname, ":", "/", -1) + ".elv"
if ec.DataDir == "" {
throw(ErrNoDataDir)
}
filename = ec.DataDir + "/" + strings.Replace(modname, ":", "/", -1) + ".elv"
if _, err := os.Stat(filename); os.IsNotExist(err) {
// File does not exist. Try loading from the table of builtin
// modules.

View File

@ -39,6 +39,7 @@ type Evaler struct {
Modules map[string]Namespace
Store *store.Store
Editor Editor
DataDir string
intCh chan struct{}
}
@ -64,8 +65,8 @@ func (ec *EvalCtx) falsify() {
}
// NewEvaler creates a new Evaler.
func NewEvaler(st *store.Store) *Evaler {
return &Evaler{Namespace{}, map[string]Namespace{}, st, nil, nil}
func NewEvaler(st *store.Store, dataDir string) *Evaler {
return &Evaler{Namespace{}, map[string]Namespace{}, st, nil, dataDir, nil}
}
func (e *Evaler) searchPaths() []string {

View File

@ -164,11 +164,8 @@ func interact(ev *eval.Evaler, st *store.Store) {
ed := edit.NewEditor(os.Stdin, sigch, ev, st)
// Source rc.elv.
datadir, err := store.EnsureDataDir()
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
source(ev, datadir+"/rc.elv", true)
if ev.DataDir != "" {
source(ev, ev.DataDir+"/rc.elv", true)
}
// Build readLine function.
@ -258,7 +255,7 @@ func newEvalerAndStore() (*eval.Evaler, *store.Store) {
}
}
return eval.NewEvaler(st), st
return eval.NewEvaler(st, dataDir), st
}
func printError(err error, errtype string) {

View File

@ -15,17 +15,10 @@ var ErrEmptyHOME = errors.New("environment variable HOME is empty")
// necessary. It returns the path to the data directory (never with a
// trailing slash) and possible error.
func EnsureDataDir() (string, error) {
ddir, err := DataDir()
if err != nil {
return "", err
}
return ddir, os.MkdirAll(ddir, 0700)
}
func DataDir() (string, error) {
home, err := util.GetHome("")
if err != nil {
return "", err
}
return home + "/.elvish", nil
ddir := home + "/.elvish"
return ddir, os.MkdirAll(ddir, 0700)
}