Skip to content

Commit 7bf4337

Browse files
committed
persistance package
1 parent c8ca50e commit 7bf4337

9 files changed

Lines changed: 128 additions & 5 deletions

File tree

app/app.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package app
2+
3+
import (
4+
"encoding/json"
5+
"path/filepath"
6+
7+
"github.com/promignis/knack/constants"
8+
"github.com/promignis/knack/fs"
9+
"github.com/promignis/knack/utils"
10+
)
11+
12+
type Manifest struct {
13+
AppName string
14+
}
15+
16+
var manifest Manifest
17+
18+
func parseManifest() Manifest {
19+
if manifest == (Manifest{}) {
20+
root := utils.GetRootPath()
21+
manifestData := fs.GetFileData(filepath.Join(root, constants.Manifest))
22+
json.Unmarshal(manifestData, &manifest)
23+
}
24+
return manifest
25+
}
26+
27+
func GetAppName() string {
28+
return parseManifest().AppName
29+
}
30+
31+
func GetUserDataPath() string {
32+
return globalSettingFolder
33+
}

app/config.windows.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package app
2+
3+
import "os"
4+
5+
var systemSettingFolders = []string{os.Getenv("PROGRAMDATA")}
6+
var globalSettingFolder = os.Getenv("APPDATA")

app/config_darwin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package app
2+
3+
import "os"
4+
5+
var systemSettingFolders = []string{"/Library/Application Support"}
6+
var globalSettingFolder = os.Getenv("HOME") + "/Library/Application Support"

app/config_xdg.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// +build !windows,!darwin
2+
3+
package app
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
)
10+
11+
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
12+
13+
var systemSettingFolders []string
14+
var globalSettingFolder string
15+
16+
func init() {
17+
if os.Getenv("XDG_CONFIG_HOME") != "" {
18+
globalSettingFolder = os.Getenv("XDG_CONFIG_HOME")
19+
} else {
20+
globalSettingFolder = filepath.Join(os.Getenv("HOME"), ".config")
21+
}
22+
if os.Getenv("XDG_CONFIG_DIRS") != "" {
23+
systemSettingFolders = strings.Split(os.Getenv("XDG_CONFIG_DIRS"), ":")
24+
} else {
25+
systemSettingFolders = []string{"/etc/xdg"}
26+
}
27+
}

js-runtime/runtime.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ function FileWalker(filePath, cb) {
110110
this.filePath = filePath
111111

112112
this.cbId = _runtime.getCbId(cb)
113-
114-
this.fileStat = new FileStat(filePath, (fileStat) => {
115-
this.fileStat = fileStat
113+
// TODO: Add webpack instead of using _this so that arrow functions can be used in windows
114+
_this = this
115+
this.fileStat = new FileStat(filePath, function(fileStat, _this){
116+
_this.fileStat = fileStat
116117
})
117118
this.walk()
118119
}
@@ -128,7 +129,8 @@ function FileStat(filePath, cb) {
128129
}
129130

130131
FileStat.prototype.getFileStat = function() {
131-
sendAction({type: 'file_stat', filePath: this.filePath, callbackId: this.cbId}, (fileStat) => {
132-
this.fileStat = fileStat
132+
_this = this
133+
sendAction({type: 'file_stat', filePath: this.filePath, callbackId: this.cbId}, function(fileStat){
134+
_this.fileStat = fileStat
133135
})
134136
}

manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"appName": "test-app"
3+
}

persistance/jsonPersistance.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package persistance
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"sync"
7+
8+
"github.com/promignis/knack/app"
9+
"github.com/promignis/knack/fs"
10+
)
11+
12+
var lock sync.Mutex
13+
var userdataFilePath = filepath.Join(app.GetUserDataPath(), "/", app.GetAppName())
14+
15+
func init() {
16+
17+
// In case the directory does not exist , create it before we start creating files inside it
18+
if _, err := os.Stat(userdataFilePath); os.IsNotExist(err) {
19+
err = os.MkdirAll(userdataFilePath, os.ModePerm)
20+
if err != nil {
21+
panic(err)
22+
}
23+
}
24+
}
25+
26+
func Set(filename string, stringifiedJson string) {
27+
// Putting this here so that read and write on the same file can not happen. Can be clenaed up in future.
28+
lock.Lock()
29+
defer lock.Unlock()
30+
name := filename + ".json"
31+
path := filepath.Join(userdataFilePath, name)
32+
fs.WriteFileData(path, []byte(stringifiedJson))
33+
}
34+
35+
func Get(filename string) string {
36+
lock.Lock()
37+
defer lock.Unlock()
38+
name := filename + ".json"
39+
path := filepath.Join(userdataFilePath, name)
40+
stringifiedJson := fs.GetFileData(path)
41+
return string(stringifiedJson)
42+
}

run-app-dev.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go build -o output-dev.exe
2+
output-dev.exe

run-app.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go build -ldflags="-H windowsgui" -o output.exe
2+
output.exe

0 commit comments

Comments
 (0)