Create the filepath for the config file
This commit is contained in:
parent
b33e366500
commit
3f1b8a4c2a
1 changed files with 18 additions and 8 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mdiluz/rove/pkg/game"
|
"github.com/mdiluz/rove/pkg/game"
|
||||||
|
@ -68,17 +69,26 @@ func InnerMain(command string) error {
|
||||||
var config = Config{
|
var config = Config{
|
||||||
Accounts: make(map[string]string),
|
Accounts: make(map[string]string),
|
||||||
}
|
}
|
||||||
_, err := os.Stat(*data)
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
if b, err := ioutil.ReadFile(*data); err != nil {
|
|
||||||
return fmt.Errorf("failed to read file %s error: %s", *data, err)
|
|
||||||
|
|
||||||
} else if len(b) == 0 {
|
// Create the path if needed
|
||||||
return fmt.Errorf("file %s was empty, assumin fresh data", *data)
|
path := filepath.Dir(*data)
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
os.MkdirAll(path, os.ModePerm)
|
||||||
|
} else {
|
||||||
|
// Read the file
|
||||||
|
_, err = os.Stat(*data)
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
if b, err := ioutil.ReadFile(*data); err != nil {
|
||||||
|
return fmt.Errorf("failed to read file %s error: %s", *data, err)
|
||||||
|
|
||||||
} else if err := json.Unmarshal(b, &config); err != nil {
|
} else if len(b) == 0 {
|
||||||
return fmt.Errorf("failed to unmarshal file %s error: %s", *data, err)
|
return fmt.Errorf("file %s was empty, assumin fresh data", *data)
|
||||||
|
|
||||||
|
} else if err := json.Unmarshal(b, &config); err != nil {
|
||||||
|
return fmt.Errorf("failed to unmarshal file %s error: %s", *data, err)
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue