Create local store functions

This commit is contained in:
comp500 2022-05-14 19:38:32 +01:00
parent 0c5ff0b7bb
commit 5a48ddeb66
2 changed files with 51 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"github.com/packwiz/packwiz/core"
"os"
"path/filepath"
@ -40,12 +41,12 @@ func init() {
rootCmd.PersistentFlags().StringVar(&modsFolder, "mods-folder", "mods", "The default folder to store mod metadata files in")
_ = viper.BindPFlag("mods-folder", rootCmd.PersistentFlags().Lookup("mods-folder"))
file, err := os.UserConfigDir()
file, err := core.GetPackwizLocalStore()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
file = filepath.Join(file, "packwiz", ".packwiz.toml")
file = filepath.Join(file, ".packwiz.toml")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "The config file to use (default \""+file+"\")")
}
@ -55,13 +56,13 @@ func initConfig() {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
dir, err := os.UserConfigDir()
dir, err := core.GetPackwizLocalStore()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
viper.AddConfigPath(filepath.Join(dir, "packwiz"))
viper.AddConfigPath(dir)
viper.SetConfigName(".packwiz")
}

46
core/storeutil.go Normal file
View File

@ -0,0 +1,46 @@
package core
import (
"os"
"path/filepath"
"runtime"
)
func GetPackwizLocalStore() (string, error) {
if //goland:noinspection GoBoolExpressions
runtime.GOOS == "linux" {
// Prefer $XDG_DATA_HOME over $XDG_CACHE_HOME
dataHome := os.Getenv("XDG_DATA_HOME")
if dataHome != "" {
return filepath.Join(dataHome, "packwiz"), nil
}
}
userConfigDir, err := os.UserConfigDir()
if err != nil {
return "", err
}
return filepath.Join(userConfigDir, "packwiz"), nil
}
func GetPackwizInstallBinPath() (string, error) {
localStore, err := GetPackwizLocalStore()
if err != nil {
return "", err
}
return filepath.Join(localStore, "bin"), nil
}
func GetPackwizInstallBinFile() (string, error) {
binPath, err := GetPackwizInstallBinPath()
if err != nil {
return "", err
}
var exeName string
if //goland:noinspection GoBoolExpressions
runtime.GOOS == "windows" {
exeName = "packwiz.exe"
} else {
exeName = "packwiz"
}
return filepath.Join(binPath, exeName), nil
}