Create the filepath for the config file

This commit is contained in:
Marc Di Luzio 2020-06-22 11:14:08 +01:00
parent b33e366500
commit 3f1b8a4c2a

View file

@ -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,7 +69,15 @@ 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)
// Create the path if needed
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 !os.IsNotExist(err) {
if b, err := ioutil.ReadFile(*data); err != nil { if b, err := ioutil.ReadFile(*data); err != nil {
return fmt.Errorf("failed to read file %s error: %s", *data, err) return fmt.Errorf("failed to read file %s error: %s", *data, err)
@ -81,6 +90,7 @@ func InnerMain(command string) error {
} }
} }
}
// If there's a host set on the command line, override the one in the config // If there's a host set on the command line, override the one in the config
if len(*host) != 0 { if len(*host) != 0 {