2020-04-14 07:05:10 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/fsutil"
|
2020-04-14 07:05:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Paths keeps all paths required for the Elvish runtime.
|
|
|
|
type Paths struct {
|
2020-12-06 10:17:19 +08:00
|
|
|
RunDir string
|
|
|
|
Sock string
|
2020-04-14 07:05:10 +08:00
|
|
|
|
|
|
|
DataDir string
|
|
|
|
Db string
|
|
|
|
Rc string
|
|
|
|
LibDir string
|
|
|
|
}
|
|
|
|
|
2020-04-16 23:39:44 +08:00
|
|
|
// MakePaths makes a populated Paths, using the given overrides.
|
|
|
|
func MakePaths(stderr io.Writer, overrides Paths) Paths {
|
|
|
|
p := overrides
|
2020-04-14 07:05:10 +08:00
|
|
|
setDir(&p.RunDir, "secure run directory", getSecureRunDir, stderr)
|
|
|
|
if p.RunDir != "" {
|
|
|
|
setChild(&p.Sock, p.RunDir, "sock")
|
|
|
|
}
|
|
|
|
|
|
|
|
setDir(&p.DataDir, "data directory", ensureDataDir, stderr)
|
|
|
|
if p.DataDir != "" {
|
|
|
|
setChild(&p.Db, p.DataDir, "db")
|
|
|
|
setChild(&p.Rc, p.DataDir, "rc.elv")
|
|
|
|
setChild(&p.LibDir, p.DataDir, "lib")
|
|
|
|
}
|
|
|
|
|
2020-04-16 23:39:44 +08:00
|
|
|
return p
|
2020-04-14 07:05:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func setDir(p *string, what string, f func() (string, error), stderr io.Writer) {
|
|
|
|
if *p == "" {
|
|
|
|
dir, err := f()
|
|
|
|
if err == nil {
|
|
|
|
*p = dir
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(stderr, "warning: cannot create %v: %v\n", what, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setChild(p *string, d, name string) {
|
|
|
|
if *p == "" {
|
|
|
|
*p = filepath.Join(d, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures Elvish's data directory exists, creating it if necessary. It returns
|
|
|
|
// the path to the data directory (never with a trailing slash) and possible
|
|
|
|
// error.
|
|
|
|
func ensureDataDir() (string, error) {
|
2020-09-03 12:27:18 +08:00
|
|
|
home, err := fsutil.GetHome("")
|
2020-04-14 07:05:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ddir := home + "/.elvish"
|
|
|
|
return ddir, os.MkdirAll(ddir, 0700)
|
|
|
|
}
|