shithub: hell

ref: 63ac58e166f42011ad4300eae48ebfeb33fcd63e
dir: /config.go/

View raw version
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"time"
	"strings"
)

const configFileName = ".hell.conf"

type Hellprefs struct {
	Apidelay float32 `json:"apidelay"`
	Save_Location string `json:"save_location"`
	Browser string `json:"browser"`
	ImageViewer string `json:"image_viewer"`
	MediaImport string `json:"media_importer"`
}

type account struct {
	MASTODON_CLIENT_ID     string    `json:"MASTODON_CLIENT_ID"`
	MASTODON_CLIENT_SECRET string    `json:"MASTODON_CLIENT_SECRET"`
	MASTODON_ACCESS_TOKEN  string    `json:"MASTODON_ACCESS_TOKEN"`
	URL                    string    `json:"URL"`
	Preferences            Hellprefs `json:"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 := json.MarshalIndent(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 = json.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 = json.MarshalIndent(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)
}