~f4814n/frost

ref: 8ce23f36008a3001c34aac6bb8751d0d91bc336c frost/cmd/frost/platform_android.go -rw-r--r-- 1.3 KiB
8ce23f36Fabian Geiselhart gofmt -w ./.. 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// +build android

package main

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

var (
	baseDir        = "/data/data/de.f4814n.frost"
	dataDir        = baseDir + "/data"
	configFilePath = baseDir + "/config.json"
	cacheDir       = baseDir + "/cache"
)

type Platform struct {
	Logger *logrus.Logger
	Cache  cache.Cache
}

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

	// Initialize Logfile
	logFile, err := os.OpenFile(baseDir+"/frost.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		panic(err.Error())
	}

	log := logrus.New()
	log.SetLevel(logrus.DebugLevel)
	log.Out = logFile

	return Platform{Cache: cache, Logger: log}
}

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

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

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

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

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