Skip to content

Commit

Permalink
Default configuration, save to file, load from file
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed Dec 20, 2018
1 parent a9e1a3f commit 211245f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
86 changes: 80 additions & 6 deletions internal/config/configuration.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
package config

import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strconv"

"github.com/OpenDroneMap/CloudODM/internal/fs"
"github.com/OpenDroneMap/CloudODM/internal/logger"

homedir "github.com/mitchellh/go-homedir"
)

var cfgPath string

func NewConfiguration() Configuration {
conf := Configuration{}
conf.Nodes = map[string]Node{}
return conf
}

type Configuration struct {
nodes map[string]Node
Nodes map[string]Node `json:"nodes"`
}

type Node struct {
url string
token string
Url string `json:"url"`
Token string `json:"token"`
}

// Initialize the configuration
Expand All @@ -26,7 +41,66 @@ func Initialize() {
os.Exit(1)
}

cfgPath := filepath.Join(home, ".odm.json")
GetPublicNodes()
fmt.Println(cfgPath)
cfgPath = filepath.Join(home, ".odm.json")
if exists, _ := fs.FileExists(cfgPath); exists {
// Read existing config
loadFromFile()
} else {
// Download public nodes, choose a default
nodes := GetPublicNodes()
defaultConfig := NewConfiguration()

logger.Info("Found " + strconv.Itoa(len(nodes)) + " public nodes")

if len(nodes) > 0 {
logger.Info("Picking a random one...")

randomNode := nodes[rand.Intn(len(nodes))]

logger.Info("Setting default node to " + randomNode.String())
defaultConfig.Nodes["default"] = Node{Url: randomNode.Url, Token: ""}
}

saveToFile(defaultConfig)
}
}

func saveToFile(conf Configuration) {
jsonData, err := json.MarshalIndent(conf, "", " ")
if err != nil {
logger.Error(err)
}

jsonFile, err := os.Create(cfgPath)
if err != nil {
logger.Error(err)
}
defer jsonFile.Close()

jsonFile.Write(jsonData)

logger.Info("Wrote default configuration in " + cfgPath)
}

func loadFromFile() Configuration {
jsonFile, err := os.Open(cfgPath)
if err != nil {
logger.Error(err)
}
logger.Debug("Loaded configuration from " + cfgPath)

defer jsonFile.Close()

jsonData, err := ioutil.ReadAll(jsonFile)
if err != nil {
logger.Error("Cannot read configuration file: " + cfgPath)
}

conf := Configuration{}
err = json.Unmarshal([]byte(jsonData), &conf)
if err != nil {
logger.Error("Cannot parse configuration file: " + err.Error())
}

return conf
}
15 changes: 9 additions & 6 deletions internal/config/publicnodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package config

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"

"github.com/OpenDroneMap/CloudODM/internal/logger"
)
Expand All @@ -16,28 +16,31 @@ type PublicNode struct {
Website string `json:"website"`
}

func (n PublicNode) String() string {
return fmt.Sprintf("%s", n.Url)
}

func GetPublicNodes() []PublicNode {
logger.Debug("Retrieving public nodes...")
nodes := []PublicNode{}

resp, err := http.Get("https://github.com/raw/OpenDroneMap/CloudODM/master/public_nodes.json")
if err != nil {
logger.Info(err)
return nil
return nodes
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Info(err)
return nil
return nodes
}

nodes := []PublicNode{}
if err := json.Unmarshal(body, &nodes); err != nil {
logger.Info("Invalid JSON content: " + string(body))
return nil
return nodes
}

logger.Info("Loaded " + strconv.Itoa(len(nodes)) + " public nodes")
return nodes
}
17 changes: 17 additions & 0 deletions internal/fs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fs

import "os"

// FileExists checks if a file path exists
func FileExists(filePath string) (bool, error) {
exists := true
if _, err := os.Stat(filePath); err != nil {
if os.IsNotExist(err) {
exists = false
} else {
return false, err
}
}

return exists, nil
}

0 comments on commit 211245f

Please sign in to comment.