Merge pull request 'Create data directory and file if they do not exist, fixes #1' (#5) from gcrkrause/issue-1 into master
Reviewed-on: https://git.soundship.de/gcrkrause/gott/pulls/5main
commit
8b718dd31c
14
main.go
14
main.go
|
@ -27,6 +27,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Record struct {
|
type Record struct {
|
||||||
|
@ -92,13 +93,24 @@ func main() {
|
||||||
datafile := os.Getenv("HOME") + "/.gott/data.json"
|
datafile := os.Getenv("HOME") + "/.gott/data.json"
|
||||||
args := os.Args[1:]
|
args := os.Args[1:]
|
||||||
|
|
||||||
|
dir := filepath.Dir(datafile)
|
||||||
|
if _, errDir := os.Stat(dir); os.IsNotExist(errDir) {
|
||||||
|
errCreateDir := os.Mkdir(dir, 0777)
|
||||||
|
if errCreateDir == nil {
|
||||||
|
fmt.Println("Created data directory")
|
||||||
|
} else {
|
||||||
|
fmt.Println("Cannot create data directory. Please create it on your own")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// representation of the data
|
// representation of the data
|
||||||
data := Records {
|
data := Records {
|
||||||
Entries: []Record {},
|
Entries: []Record {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
jsonFile, err := os.Open(datafile)
|
jsonFile, err := os.Create(datafile)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
Loading…
Reference in New Issue