2014-10-01 06:57:38 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
2016-02-12 06:43:48 +08:00
|
|
|
|
2016-02-17 02:14:05 +08:00
|
|
|
"github.com/elves/elvish/util"
|
2014-10-01 06:57:38 +08:00
|
|
|
)
|
|
|
|
|
2016-02-08 06:23:16 +08:00
|
|
|
// ErrEmptyHOME is the error returned by EnsureDataDir when the environmental
|
|
|
|
// variable HOME is empty.
|
|
|
|
var ErrEmptyHOME = errors.New("environment variable HOME is empty")
|
2014-10-01 06:57:38 +08:00
|
|
|
|
2015-02-25 04:53:34 +08:00
|
|
|
// EnsureDataDir ensures Elvish's data directory exists, creating it if
|
2014-10-01 06:57:38 +08:00
|
|
|
// necessary. It returns the path to the data directory (never with a
|
|
|
|
// trailing slash) and possible error.
|
2015-02-09 20:59:05 +08:00
|
|
|
func EnsureDataDir() (string, error) {
|
2016-02-17 06:14:53 +08:00
|
|
|
ddir, err := DataDir()
|
2016-02-12 06:43:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2014-10-01 06:57:38 +08:00
|
|
|
}
|
|
|
|
return ddir, os.MkdirAll(ddir, 0700)
|
|
|
|
}
|
2016-02-17 06:14:53 +08:00
|
|
|
|
|
|
|
func DataDir() (string, error) {
|
|
|
|
home, err := util.GetHome("")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return home + "/.elvish", nil
|
|
|
|
}
|