️ Replace dir & path funcs with cached strings

This commit is contained in:
makeworld 2020-06-21 17:35:19 -04:00
parent 61e2c51e40
commit dd6ebf9254
2 changed files with 30 additions and 43 deletions

View File

@ -9,6 +9,4 @@
- Text background not reset on ANSI pages - Text background not reset on ANSI pages
- Filed [issue 25](https://gitlab.com/tslocum/cview/-/issues/25) - Filed [issue 25](https://gitlab.com/tslocum/cview/-/issues/25)
- Modal styling messed up when wrapped - example occurence is the error modal for a long unsupported scheme URL - Modal styling messed up when wrapped - example occurence is the error modal for a long unsupported scheme URL
- Filed [issue 26](https://gitlab.com/tslocum/cview/-/issues/26) - Filed [issue 26](https://gitlab.com/tslocum/cview/-/issues/26)
- Newtab page doesn't get margin
- Config funcs should become variables

View File

@ -11,48 +11,19 @@ import (
) )
var amforaAppData string // Where amfora files are stored on Windows - cached here var amforaAppData string // Where amfora files are stored on Windows - cached here
var configDir string
func configDir() string { var configPath string
home, err := homedir.Dir()
if err != nil {
panic(err)
}
if runtime.GOOS == "windows" {
return amforaAppData
}
// Unix / POSIX system
return filepath.Join(home, ".config", "amfora")
}
func configPath() string {
return filepath.Join(configDir(), "config.toml")
}
var TofuStore = viper.New() var TofuStore = viper.New()
var tofuDBDir string
func tofuDBDir() string { var tofuDBPath string
home, err := homedir.Dir()
if err != nil {
panic(err)
}
// Windows just stores it in APPDATA along with other stuff
if runtime.GOOS == "windows" {
return amforaAppData
}
// XDG cache dir on POSIX systems
return filepath.Join(home, ".cache", "amfora")
}
func tofuDBPath() string {
return filepath.Join(tofuDBDir(), "tofu.toml")
}
func Init() error { func Init() error {
home, err := homedir.Dir() home, err := homedir.Dir()
if err != nil { if err != nil {
panic(err) panic(err)
} }
// Cache AppData path
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
appdata, ok := os.LookupEnv("APPDATA") appdata, ok := os.LookupEnv("APPDATA")
if ok { if ok {
@ -61,12 +32,30 @@ func Init() error {
amforaAppData = filepath.Join(home, filepath.FromSlash("AppData/Roaming/amfora/")) amforaAppData = filepath.Join(home, filepath.FromSlash("AppData/Roaming/amfora/"))
} }
} }
// Cache config directory and file paths
if runtime.GOOS == "windows" {
configDir = amforaAppData
} else {
// Unix / POSIX system
configDir = filepath.Join(home, ".config", "amfora")
}
configPath = filepath.Join(configDir, "config.toml")
err = os.MkdirAll(configDir(), 0755) // Cache TOFU db directory and file paths
// Windows just stores it in APPDATA along with other stuff
if runtime.GOOS == "windows" {
tofuDBDir = amforaAppData
} else {
// XDG cache dir on POSIX systems
tofuDBDir = filepath.Join(home, ".cache", "amfora")
}
tofuDBPath = filepath.Join(tofuDBDir, "tofu.toml")
err = os.MkdirAll(configDir, 0755)
if err != nil { if err != nil {
return err return err
} }
f, err := os.OpenFile(configPath(), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) f, err := os.OpenFile(configPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err == nil { if err == nil {
// Config file doesn't exist yet, write the default one // Config file doesn't exist yet, write the default one
_, err = f.Write(defaultConf) _, err = f.Write(defaultConf)
@ -77,13 +66,13 @@ func Init() error {
f.Close() f.Close()
} }
err = os.MkdirAll(tofuDBDir(), 0755) err = os.MkdirAll(tofuDBDir, 0755)
if err != nil { if err != nil {
return err return err
} }
os.OpenFile(tofuDBPath(), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) os.OpenFile(tofuDBPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
TofuStore.SetConfigFile(tofuDBPath()) TofuStore.SetConfigFile(tofuDBPath)
TofuStore.SetConfigType("toml") TofuStore.SetConfigType("toml")
err = TofuStore.ReadInConfig() err = TofuStore.ReadInConfig()
if err != nil { if err != nil {
@ -100,7 +89,7 @@ func Init() error {
viper.SetDefault("cache.max_size", 0) viper.SetDefault("cache.max_size", 0)
viper.SetDefault("cache.max_pages", 20) viper.SetDefault("cache.max_pages", 20)
viper.SetConfigFile(configPath()) viper.SetConfigFile(configPath)
viper.SetConfigType("toml") viper.SetConfigType("toml")
err = viper.ReadInConfig() err = viper.ReadInConfig()
if err != nil { if err != nil {