diff options
author | Paul Duncan <pabs@pablotron.org> | 2022-02-27 05:02:05 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2022-02-27 05:02:05 -0500 |
commit | 92c3256b7127b7a494d6f11a94e0ffebc0fc85a5 (patch) | |
tree | 6db67c8c8950c9d1376f1316f23dd51d5c160f56 /datadir | |
parent | 163666e3966058e906b9d19687da9acc62158232 (diff) | |
download | cvez-92c3256b7127b7a494d6f11a94e0ffebc0fc85a5.tar.bz2 cvez-92c3256b7127b7a494d6f11a94e0ffebc0fc85a5.zip |
add datadir and cmd/search.go
Diffstat (limited to 'datadir')
-rw-r--r-- | datadir/datadir.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/datadir/datadir.go b/datadir/datadir.go new file mode 100644 index 0000000..9019e5d --- /dev/null +++ b/datadir/datadir.go @@ -0,0 +1,48 @@ +// data directory +package datadir + +import ( + "errors" + "io/fs" + "os" + "path/filepath" +) + +// Get data directory path. +func DataDir() (string, error) { + var dir string + + // check environment + if val, ok := os.LookupEnv("CVEZ_DATA_DIR"); ok { + return val, nil + } + + // get config dir + configDir, err := os.UserConfigDir() + if err != nil { + return dir, err + } + + // build path to data directory in config dir + dir = filepath.Join(configDir, "cvez") + if _, err = os.Stat(dir); err != nil { + if errors.Is(err, fs.ErrNotExist) { + // create data directory + return dir, os.MkdirAll(dir, 0700) + } else { + // return error + return "", err + } + } else { + return dir, nil + } +} + +// Build path to file in cvez data directory. +func Join(args ...string) (string, error) { + if dir, err := DataDir(); err != nil { + return "", err + } else { + return filepath.Join(dir, filepath.Join(args...)), nil + } +} |