1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 21:29:02 +08:00
nali/internal/constant/path.go

50 lines
942 B
Go
Raw Permalink Normal View History

2021-08-02 12:01:25 +08:00
package constant
import (
"log"
"os"
"path/filepath"
"github.com/adrg/xdg"
2021-08-02 12:01:25 +08:00
)
var (
ConfigDirPath string
DataDirPath string
2021-08-02 12:01:25 +08:00
)
func init() {
if naliHome := os.Getenv("NALI_HOME"); len(naliHome) != 0 {
ConfigDirPath = naliHome
DataDirPath = naliHome
} else {
ConfigDirPath = os.Getenv("NALI_CONFIG_HOME")
if len(ConfigDirPath) == 0 {
ConfigDirPath = filepath.Join(xdg.ConfigHome, "nali")
}
DataDirPath = os.Getenv("NALI_DB_HOME")
if len(DataDirPath) == 0 {
DataDirPath = filepath.Join(xdg.DataHome, "nali")
2021-08-02 12:01:25 +08:00
}
}
prepareDir(ConfigDirPath)
prepareDir(DataDirPath)
_ = os.Chdir(DataDirPath)
}
func prepareDir(dir string) {
stat, err := os.Stat(dir)
if os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0755); err != nil {
log.Fatal("can not create config dir:", dir)
}
} else if err != nil {
log.Fatal(err)
} else if !stat.IsDir() {
log.Fatal("path already exists, but not a dir:", dir)
}
2021-08-02 12:01:25 +08:00
}