Properly load/write configuration files.
authorOllivier Robert <roberto@keltia.net>
Mon, 10 Apr 2017 16:20:02 +0200
changeset 36 b3c341207b30
parent 35 27c58d359940
child 37 a0d3e7265cd2
Properly load/write configuration files.
config.go
--- a/config.go	Mon Apr 10 15:54:10 2017 +0200
+++ b/config.go	Mon Apr 10 16:20:02 2017 +0200
@@ -8,92 +8,130 @@
 import (
 	"fmt"
 	"io/ioutil"
+	"log"
 	"os"
 	"path/filepath"
-	"strings"
 
 	"github.com/naoina/toml"
 )
 
 /*
 Assume the application is registered if $HOME/.config/<gondole>/config.toml already exist
- */
+We will store the per-instance token into $HOME/.config/<gondole>/<site>.token
+*/
 
 const (
 	DefaultName = "config.toml"
 )
 
+var (
+	baseDir = filepath.Join(os.Getenv("HOME"),
+		".config",
+		"gondole",
+	)
+)
+
 // Config holds our parameters
+type Server struct {
+	ID          int64
+	Name        string
+	BearerToken string
+}
+
 type Config struct {
-	ID           int64
-	Name         string
-	BearerToken  string
+	Default string
 }
 
-// Check the parameter for either tag or filename
-func checkName(file string) (str string) {
-	// Full path, MUST have .toml
-	if bfile := []byte(file); bfile[0] == '/' {
-		if !strings.HasSuffix(file, ".toml") {
-			str = ""
-		} else {
-			str = file
+func loadGlobal(file string) (c *Config, err error) {
+	// Check if there is any config file
+	if _, err := os.Stat(file); err == nil {
+		// Read it
+		buf, err := ioutil.ReadFile(file)
+		if err != nil {
+			return c, fmt.Errorf("Can not read %s", file)
 		}
-		return
+
+		err = toml.Unmarshal(buf, c)
+		if err != nil {
+			return c, fmt.Errorf("Error parsing toml %s: %v", file, err)
+		}
 	}
+	return
+}
+
+func loadInstance(name string) (s *Server, err error) {
+	// Load instance-specific file
+	file := filepath.Join(baseDir, name+".token")
 
-	// If ending with .toml, take it literally
-	if strings.HasSuffix(file, ".toml") {
-		str = file
-		return
-	}
+	// Check if there is any config file
+	if _, err := os.Stat(file); err == nil {
+		// Read it
+		buf, err := ioutil.ReadFile(file)
+		if err != nil {
+			return s, fmt.Errorf("Can not read %s", file)
+		}
 
-	// Check for tag
-	if !strings.HasSuffix(file, ".toml") {
-		// file must be a tag so add a "."
-		str = filepath.Join(os.Getenv("HOME"),
-			fmt.Sprintf(".%s", file),
-			"config.toml")
+		err = toml.Unmarshal(buf, s)
+		if err != nil {
+			return s, fmt.Errorf("Error parsing toml %s: %v", file, err)
+		}
+	}
+	return
+}
+
+func GetInstanceList() (list []string) {
+	list, err := filepath.Glob(filepath.Join(baseDir + "*.token"))
+	if err != nil {
+		log.Printf("warning, no *.token files")
+		list = nil
 	}
 	return
 }
 
 // LoadConfig reads a file as a TOML document and return the structure
-func LoadConfig(name, redirectURL string) (c *Config, err error) {
-	c = new(Config)
-
-	sFile := filepath.Join(os.Getenv("HOME"),
-		".config",
-		"gondole",
-		DefaultName,
-	)
-
-	// Check if there is any config file
-	if _, err := os.Stat(sFile); err != nil {
-		// No config file is no error
-
+func LoadConfig(name string) (s *Server, err error) {
+	// Load global file
+	gFile := filepath.Join(baseDir, DefaultName)
 
+	c, err := loadGlobal(gFile)
+	if err != nil {
+		return
+	}
+	if name == "" {
+		s, err = loadInstance(c.Default)
 	} else {
-		// Read it
-		buf, err := ioutil.ReadFile(sFile)
-		if err != nil {
-			return c, fmt.Errorf("Can not read %s", sFile)
-		}
+		s, err = loadInstance(name)
+	}
 
-		err = toml.Unmarshal(buf, &c)
-		if err != nil {
-			return c, fmt.Errorf("Error parsing toml %s: %v",
-				sFile, err)
-		}
-	}
-	return c, nil
+	return s, err
 }
 
-func WriteConfig(c * Config) (err error) {
-	sc, err := toml.Marshal(c)
-	if err != nil {
+func (c *Config) Write() (err error) {
+	if err = os.MkdirAll(baseDir, 0700); err != nil {
+		log.Fatalf("error creating configuration directory: %s", baseDir)
+	}
+
+	var sc []byte
+
+	if sc, err = toml.Marshal(c); err != nil {
+		log.Fatalf("error saving configuration")
+	}
+	err = ioutil.WriteFile(filepath.Join(baseDir, DefaultName), sc, 0600)
+	return
+}
 
+func (s *Server) WriteToken(instance string) (err error) {
+	if err = os.MkdirAll(baseDir, 0700); err != nil {
+		log.Fatalf("error creating configuration directory: %s", baseDir)
 	}
-	err = ioutil.WriteFile(filepath.Join(os.Getenv("HOME"), ".config", "gondole", DefaultName), sc, 0600)
+
+	var sc []byte
+
+	if sc, err = toml.Marshal(s); err != nil {
+		log.Fatalf("error saving configuration")
+	}
+
+	full := instance + ".token"
+	err = ioutil.WriteFile(filepath.Join(baseDir, full), sc, 0600)
 	return
-}
\ No newline at end of file
+}