ref: 482ed92dbc236887d206a1b3f3d14c095ecdc2f4
dir: /config.go/
package main
import (
"fmt"
"io/ioutil"
"os"
"time"
"strings"
"github.com/BurntSushi/toml"
)
const configFileName = ".hell.conf"
type Hellprefs struct {
Apidelay float32 `toml:"apidelay"`
Save_Location string `toml:"save_location"`
Browser string `toml:"browser"`
ImageViewer string `toml:"image_viewer"`
MediaImport string `toml:"media_importer"`
}
type account struct {
MASTODON_CLIENT_ID string `toml:"MASTODON_CLIENT_ID"`
MASTODON_CLIENT_SECRET string `toml:"MASTODON_CLIENT_SECRET"`
MASTODON_ACCESS_TOKEN string `toml:"MASTODON_ACCESS_TOKEN"`
URL string `toml:"URL"`
Preferences Hellprefs `toml:"preferences"`
}
func (prefs *Hellprefs) ApiDelayTime() time.Duration {
delayms := prefs.Apidelay * 1000
i := int64(delayms)
return time.Duration(i) * time.Millisecond
}
func fixPrefs(prefs *Hellprefs) (*Hellprefs, bool) {
var fixed bool
defaultprefs := initPrefs()
if prefs == nil {
return &defaultprefs, true
}
if prefs.Apidelay == 0 {
prefs.Apidelay = defaultprefs.Apidelay
fixed = true
}
if prefs.Save_Location == "" {
prefs.Save_Location = defaultprefs.Save_Location
fixed = true
}
if prefs.Browser == "" {
prefs.Browser = defaultprefs.Browser
fixed = true
}
if prefs.ImageViewer == "" {
prefs.ImageViewer = defaultprefs.ImageViewer
fixed = true
}
if prefs.MediaImport == "" {
prefs.MediaImport = defaultprefs.MediaImport
fixed = true
}
return prefs, fixed
}
func initPrefs() Hellprefs {
prefs := &Hellprefs{
Apidelay: 2,
Save_Location: "~/Downloads",
Browser: "xdg-open '%s'",
ImageViewer: "xdg-open '%s'",
MediaImport: "xdg-open '%s'",
}
return *prefs
}
func loadConfig() (*account, error) {
config := &account{}
homedir, err := os.UserHomeDir()
var configpath string
if err == nil {
configpath = homedir + "/" + configFileName
} else {
//If go can't find us a home dir just use the current dir
//Probably not a normal multiuser system
configpath = configFileName
}
// Check if the config file exists
if _, err = os.Stat(configpath); os.IsNotExist(err) {
// no config, create account
client := ConfigureClient()
config.URL = "https://eldritch.cafe"
config.MASTODON_CLIENT_ID = client.Config.ClientID
config.MASTODON_CLIENT_SECRET = client.Config.ClientSecret
config.MASTODON_ACCESS_TOKEN = client.Config.AccessToken
config.Preferences = initPrefs()
data, err := toml.Marshal(config)
if err != nil {
return nil, fmt.Errorf("error marshalling config: %w", err)
}
err = ioutil.WriteFile(configpath, data, 0644)
if err != nil {
return nil, fmt.Errorf("error writing config file: %w", err)
}
return config, nil
}
// File exists, load it
data, err := ioutil.ReadFile(configpath)
if err != nil {
return nil, fmt.Errorf("error reading config file: %w", err)
}
err = toml.Unmarshal(data, config)
if err != nil {
return nil, fmt.Errorf("error unmarshalling config: %w", err)
}
prefs := &config.Preferences
prefs, fixed := fixPrefs(&config.Preferences)
config.Preferences = *prefs
if fixed {
data, err = toml.Marshal(config)
if err != nil {
return nil, fmt.Errorf("error marshalling config: %w", err)
}
err = ioutil.WriteFile(configpath, data, 0644)
if err != nil {
return nil, fmt.Errorf("error writing config file: %w", err)
}
}
config.Preferences.Save_Location = expandDir(config.Preferences.Save_Location)
return config, nil
}
func expandDir(dir string) string {
dir = strings.ReplaceAll(dir, "~", "$HOME")
return os.ExpandEnv(dir)
}