Skip to content

Commit

Permalink
persistance package
Browse files Browse the repository at this point in the history
  • Loading branch information
revant117 committed Oct 9, 2018
1 parent c8ca50e commit 7bf4337
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 5 deletions.
33 changes: 33 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package app

import (
"encoding/json"
"path/filepath"

"github.com/promignis/knack/constants"
"github.com/promignis/knack/fs"
"github.com/promignis/knack/utils"
)

type Manifest struct {
AppName string
}

var manifest Manifest

func parseManifest() Manifest {
if manifest == (Manifest{}) {
root := utils.GetRootPath()
manifestData := fs.GetFileData(filepath.Join(root, constants.Manifest))
json.Unmarshal(manifestData, &manifest)
}
return manifest
}

func GetAppName() string {
return parseManifest().AppName
}

func GetUserDataPath() string {
return globalSettingFolder
}
6 changes: 6 additions & 0 deletions app/config.windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package app

import "os"

var systemSettingFolders = []string{os.Getenv("PROGRAMDATA")}
var globalSettingFolder = os.Getenv("APPDATA")
6 changes: 6 additions & 0 deletions app/config_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package app

import "os"

var systemSettingFolders = []string{"/Library/Application Support"}
var globalSettingFolder = os.Getenv("HOME") + "/Library/Application Support"
27 changes: 27 additions & 0 deletions app/config_xdg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// +build !windows,!darwin

package app

import (
"os"
"path/filepath"
"strings"
)

// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

var systemSettingFolders []string
var globalSettingFolder string

func init() {
if os.Getenv("XDG_CONFIG_HOME") != "" {
globalSettingFolder = os.Getenv("XDG_CONFIG_HOME")
} else {
globalSettingFolder = filepath.Join(os.Getenv("HOME"), ".config")
}
if os.Getenv("XDG_CONFIG_DIRS") != "" {
systemSettingFolders = strings.Split(os.Getenv("XDG_CONFIG_DIRS"), ":")
} else {
systemSettingFolders = []string{"/etc/xdg"}
}
}
12 changes: 7 additions & 5 deletions js-runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ function FileWalker(filePath, cb) {
this.filePath = filePath

this.cbId = _runtime.getCbId(cb)

this.fileStat = new FileStat(filePath, (fileStat) => {
this.fileStat = fileStat
// TODO: Add webpack instead of using _this so that arrow functions can be used in windows
_this = this
this.fileStat = new FileStat(filePath, function(fileStat, _this){
_this.fileStat = fileStat
})
this.walk()
}
Expand All @@ -128,7 +129,8 @@ function FileStat(filePath, cb) {
}

FileStat.prototype.getFileStat = function() {
sendAction({type: 'file_stat', filePath: this.filePath, callbackId: this.cbId}, (fileStat) => {
this.fileStat = fileStat
_this = this
sendAction({type: 'file_stat', filePath: this.filePath, callbackId: this.cbId}, function(fileStat){
_this.fileStat = fileStat
})
}
3 changes: 3 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"appName": "test-app"
}
42 changes: 42 additions & 0 deletions persistance/jsonPersistance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package persistance

import (
"os"
"path/filepath"
"sync"

"github.com/promignis/knack/app"
"github.com/promignis/knack/fs"
)

var lock sync.Mutex
var userdataFilePath = filepath.Join(app.GetUserDataPath(), "/", app.GetAppName())

func init() {

// In case the directory does not exist , create it before we start creating files inside it
if _, err := os.Stat(userdataFilePath); os.IsNotExist(err) {
err = os.MkdirAll(userdataFilePath, os.ModePerm)
if err != nil {
panic(err)
}
}
}

func Set(filename string, stringifiedJson string) {
// Putting this here so that read and write on the same file can not happen. Can be clenaed up in future.
lock.Lock()
defer lock.Unlock()
name := filename + ".json"
path := filepath.Join(userdataFilePath, name)
fs.WriteFileData(path, []byte(stringifiedJson))
}

func Get(filename string) string {
lock.Lock()
defer lock.Unlock()
name := filename + ".json"
path := filepath.Join(userdataFilePath, name)
stringifiedJson := fs.GetFileData(path)
return string(stringifiedJson)
}
2 changes: 2 additions & 0 deletions run-app-dev.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go build -o output-dev.exe
output-dev.exe
2 changes: 2 additions & 0 deletions run-app.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go build -ldflags="-H windowsgui" -o output.exe
output.exe

0 comments on commit 7bf4337

Please sign in to comment.