~f4814n/frost

0a5b4ee6671dd07dd0900e550ee4d0448378fc63 — Fabian Geiselhart 11 months ago b0243ab
Windows: Init
1 files changed, 73 insertions(+), 0 deletions(-)

A cmd/frost/platform_windows.go
A cmd/frost/platform_windows.go => cmd/frost/platform_windows.go +73 -0
@@ 0,0 1,73 @@
// +build windows

package main

import (
	"encoding/json"
	"git.sr.ht/~f4814n/matrix"
	backend "git.sr.ht/~f4814n/matrix/backend/memory"
	"git.sr.ht/~f4814n/matrix/util/cache"
	"github.com/sirupsen/logrus"
	"os"
)

type Platform struct {
	cacheDir, dataDir, configFile string

	Logger *logrus.Logger
	Cache  cache.Cache
}

func InitPlatform() Platform {
	dataDir, err := os.UserConfigDir()
	if err != nil {
		panic(err.Error())
	}
	dataDir += "\\frost"

	cacheDir, err := os.UserCacheDir()
	if err != nil {
		panic(err.Error())
	}
	cacheDir += "\\frost"

	configFile := dataDir + "/config.json"

	// Create b.Path directory
	if _, err := os.Stat(dataDir); os.IsNotExist(err) {
		os.Mkdir(dataDir, os.ModePerm) // XXX Correct permissions
	}

	// Create Cache
	cache, err := cache.NewFSCache(cacheDir)
	if err != nil {
		// XXX Fallback to memory cache, warn
		panic(err.Error())
	}

	return Platform{Cache: cache, cacheDir: cacheDir, dataDir: dataDir, configFile: configFile, Logger: logrus.New()}
}

func (p Platform) LoadConfig() (cfg AppConfig, err error) {
	f, err := os.Open(p.configFile)
	if err != nil {
		return
	}

	err = json.NewDecoder(f).Decode(&cfg)
	return
}

func (p Platform) FlushConfig(cfg AppConfig) (err error) {
	f, err := os.Create(p.configFile)
	if err != nil {
		return
	}

	err = json.NewEncoder(f).Encode(cfg)
	return
}

func (p Platform) NewBackend() matrix.Backend {
	return backend.New()
}