2020-06-18 16:54:48 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2020-06-22 11:56:55 -04:00
|
|
|
"strings"
|
2020-06-18 16:54:48 -04:00
|
|
|
|
|
|
|
"github.com/makeworld-the-better-one/amfora/cache"
|
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var amforaAppData string // Where amfora files are stored on Windows - cached here
|
2020-06-21 17:35:19 -04:00
|
|
|
var configDir string
|
|
|
|
var configPath string
|
2020-06-18 16:54:48 -04:00
|
|
|
|
|
|
|
var TofuStore = viper.New()
|
2020-06-21 17:35:19 -04:00
|
|
|
var tofuDBDir string
|
|
|
|
var tofuDBPath string
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-06-23 20:07:25 -04:00
|
|
|
// Bookmarks
|
|
|
|
var BkmkStore = viper.New()
|
|
|
|
var bkmkDir string
|
|
|
|
var bkmkPath string
|
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
func Init() error {
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-23 20:07:25 -04:00
|
|
|
// Store AppData path
|
2020-06-18 16:54:48 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
appdata, ok := os.LookupEnv("APPDATA")
|
|
|
|
if ok {
|
|
|
|
amforaAppData = filepath.Join(appdata, "amfora")
|
|
|
|
} else {
|
|
|
|
amforaAppData = filepath.Join(home, filepath.FromSlash("AppData/Roaming/amfora/"))
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 20:07:25 -04:00
|
|
|
|
|
|
|
// Store config directory and file paths
|
2020-06-21 17:35:19 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
configDir = amforaAppData
|
|
|
|
} else {
|
|
|
|
// Unix / POSIX system
|
2020-06-22 11:56:55 -04:00
|
|
|
xdg_config, ok := os.LookupEnv("XDG_CONFIG_HOME")
|
|
|
|
if ok && strings.TrimSpace(xdg_config) != "" {
|
|
|
|
configDir = filepath.Join(xdg_config, "amfora")
|
|
|
|
} else {
|
|
|
|
// Default to ~/.config/amfora
|
|
|
|
configDir = filepath.Join(home, ".config", "amfora")
|
|
|
|
}
|
2020-06-21 17:35:19 -04:00
|
|
|
}
|
|
|
|
configPath = filepath.Join(configDir, "config.toml")
|
|
|
|
|
2020-06-23 20:07:25 -04:00
|
|
|
// Store TOFU db directory and file paths
|
2020-06-21 17:35:19 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
2020-06-22 11:56:55 -04:00
|
|
|
// Windows just stores it in APPDATA along with other stuff
|
2020-06-21 17:35:19 -04:00
|
|
|
tofuDBDir = amforaAppData
|
|
|
|
} else {
|
|
|
|
// XDG cache dir on POSIX systems
|
2020-06-22 11:56:55 -04:00
|
|
|
xdg_cache, ok := os.LookupEnv("XDG_CACHE_HOME")
|
|
|
|
if ok && strings.TrimSpace(xdg_cache) != "" {
|
|
|
|
tofuDBDir = filepath.Join(xdg_cache, "amfora")
|
|
|
|
} else {
|
|
|
|
// Default to ~/.cache/amfora
|
|
|
|
tofuDBDir = filepath.Join(home, ".cache", "amfora")
|
|
|
|
}
|
2020-06-21 17:35:19 -04:00
|
|
|
}
|
|
|
|
tofuDBPath = filepath.Join(tofuDBDir, "tofu.toml")
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-06-23 20:07:25 -04:00
|
|
|
// Store bookmarks dir and path
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Windows just keeps it in APPDATA along with other Amfora files
|
|
|
|
bkmkDir = amforaAppData
|
|
|
|
} else {
|
|
|
|
// XDG data dir on POSIX systems
|
|
|
|
xdg_data, ok := os.LookupEnv("XDG_DATA_HOME")
|
|
|
|
if ok && strings.TrimSpace(xdg_data) != "" {
|
|
|
|
bkmkDir = filepath.Join(xdg_data, "amfora")
|
|
|
|
} else {
|
|
|
|
// Default to ~/.local/share/amfora
|
|
|
|
bkmkDir = filepath.Join(home, ".local", "share", "amfora")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bkmkPath = filepath.Join(bkmkDir, "bookmarks.toml")
|
|
|
|
|
|
|
|
// Create necessary files and folders
|
|
|
|
|
|
|
|
// Config
|
2020-06-21 17:35:19 -04:00
|
|
|
err = os.MkdirAll(configDir, 0755)
|
2020-06-18 16:54:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-21 17:35:19 -04:00
|
|
|
f, err := os.OpenFile(configPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
|
2020-06-18 16:54:48 -04:00
|
|
|
if err == nil {
|
|
|
|
// Config file doesn't exist yet, write the default one
|
|
|
|
_, err = f.Write(defaultConf)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
2020-06-23 20:07:25 -04:00
|
|
|
// TOFU
|
2020-06-21 17:35:19 -04:00
|
|
|
err = os.MkdirAll(tofuDBDir, 0755)
|
2020-06-18 16:54:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-21 17:35:19 -04:00
|
|
|
os.OpenFile(tofuDBPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
|
2020-06-23 20:07:25 -04:00
|
|
|
// Bookmarks
|
|
|
|
err = os.MkdirAll(bkmkDir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
os.OpenFile(bkmkPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
|
|
|
|
|
|
|
|
// Setup vipers
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-06-21 17:35:19 -04:00
|
|
|
TofuStore.SetConfigFile(tofuDBPath)
|
2020-06-18 16:54:48 -04:00
|
|
|
TofuStore.SetConfigType("toml")
|
|
|
|
err = TofuStore.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-23 20:07:25 -04:00
|
|
|
BkmkStore.SetConfigFile(bkmkPath)
|
|
|
|
BkmkStore.SetConfigType("toml")
|
|
|
|
err = BkmkStore.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
BkmkStore.Set("DO NOT TOUCH", true)
|
|
|
|
err = BkmkStore.WriteConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
viper.SetDefault("a-general.home", "gemini.circumlunar.space")
|
|
|
|
viper.SetDefault("a-general.http", "default")
|
|
|
|
viper.SetDefault("a-general.search", "gus.guru/search")
|
|
|
|
viper.SetDefault("a-general.color", true)
|
|
|
|
viper.SetDefault("a-general.bullets", true)
|
2020-06-21 16:53:12 -04:00
|
|
|
viper.SetDefault("a-general.left_margin", 0.15)
|
|
|
|
viper.SetDefault("a-general.max_width", 100)
|
2020-06-18 16:54:48 -04:00
|
|
|
viper.SetDefault("cache.max_size", 0)
|
|
|
|
viper.SetDefault("cache.max_pages", 20)
|
|
|
|
|
2020-06-21 17:35:19 -04:00
|
|
|
viper.SetConfigFile(configPath)
|
2020-06-18 16:54:48 -04:00
|
|
|
viper.SetConfigType("toml")
|
|
|
|
err = viper.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup cache from config
|
|
|
|
cache.SetMaxSize(viper.GetInt("cache.max_size"))
|
|
|
|
cache.SetMaxPages(viper.GetInt("cache.max_pages"))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|