Skip to content

Commit

Permalink
Merge pull request #6 from DiSiqueira/location
Browse files Browse the repository at this point in the history
Location
  • Loading branch information
disiqueira authored Dec 27, 2016
2 parents 65ca85a + e3f457e commit 0deb616
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
36 changes: 30 additions & 6 deletions defaultdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,45 @@ package main
import (
"fmt"
"gopkg.in/ini.v1"
"os"
"os/user"
"path/filepath"
)

func testDb(file string) bool {
cfgTest, err := ini.LoadSources(ini.LoadOptions{AllowBooleanKeys: true, Loose: false}, file)

if err != nil {
return false
}

cfg = cfgTest

return true
}

func initDb() {

cfg, _ = ini.LoadSources(ini.LoadOptions{AllowBooleanKeys: true, Loose: true}, configFile)
//test if exist a configFile in the directory
cfgFile = configFile
if testDb(cfgFile) {
return
}

if _, err := os.Stat(configFile); os.IsNotExist(err) {
cfg = ini.Empty()
defaultDb()
//test if exist a configFile in the user's home
currentUser, _ := user.Current()
cfgFile = filepath.Join(currentUser.HomeDir, configFile)
if testDb(cfgFile) {
return
}

//Create a default database in user's home
cfg = ini.Empty()
defaultDb()

}

func closeDb() {
err := cfg.SaveTo(configFile)
err := cfg.SaveTo(cfgFile)

if err != nil {
panic(err)
Expand Down
3 changes: 2 additions & 1 deletion gorganizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
)

const (
configFile = "config.ini"
configFile = ".gorganizer.ini"
)

var cfg *ini.File
var cfgFile string

func main() {

Expand Down

0 comments on commit 0deb616

Please sign in to comment.