Move config stuff into the CLI app.
authorOllivier Robert <roberto@keltia.net>
Wed, 12 Apr 2017 09:38:20 +0200
changeset 69 3474d0f36259
parent 68 6252b7eea308
child 70 fbc089e7249d
Move config stuff into the CLI app.
cmd/gondole-cli/config.go
cmd/gondole-cli/config_test.go
config.go
config_test.go
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd/gondole-cli/config.go	Wed Apr 12 09:38:20 2017 +0200
@@ -0,0 +1,152 @@
+// config.go
+//
+// This file implements the configuration part for when you need the API
+// key to modify things in the Mastodon configuration and manage measurements.
+
+package gondole
+
+import (
+	"fmt"
+	"io/ioutil"
+	"log"
+	"os"
+	"path/filepath"
+
+	"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          string `json:"id"`
+	Name        string `json:"name"`
+	BearerToken string `json:"bearer_token"`
+	BaseURL     string `json:"base_url"`	// Allow for overriding APIEndpoint on registration
+}
+
+type Config struct {
+	Default string
+}
+
+func loadGlobal(file string) (c *Config, err error) {
+	log.Printf("file=%s", file)
+	// Check if there is any config file
+	_, err = os.Stat(file)
+	if err != nil {
+		return
+	}
+
+	log.Printf("file=%s, found it", file)
+	// Read it
+	buf, err := ioutil.ReadFile(file)
+	if err != nil {
+		return c, fmt.Errorf("Can not read %s", file)
+	}
+
+	cnf := Config{}
+	err = toml.Unmarshal(buf, &cnf)
+	if err != nil {
+		return c, fmt.Errorf("Error parsing toml %s: %v", file, err)
+	}
+	c = &cnf
+	return
+}
+
+func loadInstance(name string) (s *Server, err error) {
+	// Load instance-specific file
+	file := filepath.Join(baseDir, name+".token")
+
+	log.Printf("instance is %s", file)
+
+	// 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)
+		}
+
+		sc := Server{}
+		err = toml.Unmarshal(buf, &sc)
+		if err != nil {
+			return s, fmt.Errorf("Error parsing toml %s: %v", file, err)
+		}
+		s = &sc
+	}
+	return
+}
+
+func GetInstanceList() (list []string) {
+	list, err := filepath.Glob(filepath.Join(baseDir, "*.token"))
+	log.Printf("basedir=%s", filepath.Join(baseDir, "*.token"))
+	if err != nil {
+		log.Printf("warning, no *.token files in %s", baseDir)
+		list = nil
+	}
+	log.Printf("list=%v", list)
+	return
+}
+
+// LoadConfig reads a file as a TOML document and return the structure
+func LoadConfig(name string) (s *Server, err error) {
+	// Load global file
+	gFile := filepath.Join(baseDir, DefaultName)
+
+	log.Printf("global is %s", gFile)
+	c, err := loadGlobal(gFile)
+	if err != nil {
+		return
+	}
+	if name == "" {
+		s, err = loadInstance(c.Default)
+	} else {
+		s, err = loadInstance(name)
+	}
+
+	return s, err
+}
+
+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)
+	}
+
+	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
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd/gondole-cli/config_test.go	Wed Apr 12 09:38:20 2017 +0200
@@ -0,0 +1,105 @@
+package gondole
+
+import (
+	"github.com/stretchr/testify/assert"
+	"os"
+	"path/filepath"
+	"testing"
+)
+
+func TestLoadGlobal(t *testing.T) {
+	baseDir = "."
+
+	_, err := loadGlobal(filepath.Join("test", "non.toml"))
+	assert.Error(t, err, "does not exist")
+
+	_, err = loadGlobal(filepath.Join("test", "garbage.token"))
+	assert.Error(t, err, "just garbage")
+
+	// git does now allow you to checkin 000 files :(
+	err = os.Chmod(filepath.Join("test", "perms.toml"), 0000)
+	assert.NoError(t, err, "should be fine")
+	_, err = loadGlobal(filepath.Join("test", "perms.toml"))
+	assert.Error(t, err, "unreadable")
+	err = os.Chmod(filepath.Join("test", "perms.toml"), 0600)
+	assert.NoError(t, err, "should be fine")
+
+	c, err := loadGlobal(filepath.Join("test", "config.toml"))
+	assert.NoError(t, err, "should read it fine")
+	assert.EqualValues(t, "foo", c.Default, "equal")
+}
+
+func TestLoadInstance(t *testing.T) {
+	baseDir = "."
+
+	_, err := loadInstance("nonexistent")
+	assert.Error(t, err, "does not exist")
+
+	file := filepath.Join("test", "garbage")
+	_, err = loadInstance(file)
+	assert.Error(t, err, "just garbage")
+
+	file = filepath.Join("test", "foo.token")
+	err = os.Chmod(file, 0000)
+	assert.NoError(t, err, "should be fine")
+
+	file = filepath.Join("test", "foo")
+	_, err = loadInstance(file)
+	assert.Error(t, err, "unreadable")
+
+	file = filepath.Join("test", "foo.token")
+	err = os.Chmod(file, 0644)
+	assert.NoError(t, err, "should be fine")
+
+	real := &Server{
+		ID:          "666",
+		Name:        "foo",
+		BearerToken: "d3b07384d113edec49eaa6238ad5ff00",
+		BaseURL:     "https://mastodon.social",
+	}
+	file = filepath.Join("test", "foo")
+	s, err := loadInstance(file)
+	assert.NoError(t, err, "all fine")
+	assert.Equal(t, real, s, "equal")
+}
+
+func TestGetInstanceList(t *testing.T) {
+	baseDir = "test"
+
+	real := []string{
+		filepath.Join("test", "foo.token"),
+		filepath.Join("test", "garbage.token"),
+	}
+	list := GetInstanceList()
+	assert.Equal(t, real, list, "equal")
+
+	baseDir = "/tmp"
+	real = nil
+	list = GetInstanceList()
+	assert.Equal(t, real, list, "equal")
+
+	baseDir = "/nonexistent"
+	real = nil
+	list = GetInstanceList()
+	assert.Equal(t, real, list, "equal")
+}
+
+func TestLoadConfig(t *testing.T) {
+	baseDir = "test"
+
+	_, err := LoadConfig("foo")
+	assert.NoError(t, err, "should be fine")
+
+	_, err = LoadConfig("")
+	assert.NoError(t, err, "should be fine")
+
+	err = os.Chmod(filepath.Join("test", "config.toml"), 0000)
+	assert.NoError(t, err, "should be fine")
+
+	_, err = LoadConfig("")
+	assert.Error(t, err, "should be unreadable")
+
+	err = os.Chmod(filepath.Join("test", "config.toml"), 0600)
+	assert.NoError(t, err, "should be fine")
+
+}
\ No newline at end of file
--- a/config.go	Tue Apr 11 17:15:12 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,152 +0,0 @@
-// config.go
-//
-// This file implements the configuration part for when you need the API
-// key to modify things in the Mastodon configuration and manage measurements.
-
-package gondole
-
-import (
-	"fmt"
-	"io/ioutil"
-	"log"
-	"os"
-	"path/filepath"
-
-	"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          string `json:"id"`
-	Name        string `json:"name"`
-	BearerToken string `json:"bearer_token"`
-	BaseURL     string `json:"base_url"`	// Allow for overriding APIEndpoint on registration
-}
-
-type Config struct {
-	Default string
-}
-
-func loadGlobal(file string) (c *Config, err error) {
-	log.Printf("file=%s", file)
-	// Check if there is any config file
-	_, err = os.Stat(file)
-	if err != nil {
-		return
-	}
-
-	log.Printf("file=%s, found it", file)
-	// Read it
-	buf, err := ioutil.ReadFile(file)
-	if err != nil {
-		return c, fmt.Errorf("Can not read %s", file)
-	}
-
-	cnf := Config{}
-	err = toml.Unmarshal(buf, &cnf)
-	if err != nil {
-		return c, fmt.Errorf("Error parsing toml %s: %v", file, err)
-	}
-	c = &cnf
-	return
-}
-
-func loadInstance(name string) (s *Server, err error) {
-	// Load instance-specific file
-	file := filepath.Join(baseDir, name+".token")
-
-	log.Printf("instance is %s", file)
-
-	// 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)
-		}
-
-		sc := Server{}
-		err = toml.Unmarshal(buf, &sc)
-		if err != nil {
-			return s, fmt.Errorf("Error parsing toml %s: %v", file, err)
-		}
-		s = &sc
-	}
-	return
-}
-
-func GetInstanceList() (list []string) {
-	list, err := filepath.Glob(filepath.Join(baseDir, "*.token"))
-	log.Printf("basedir=%s", filepath.Join(baseDir, "*.token"))
-	if err != nil {
-		log.Printf("warning, no *.token files in %s", baseDir)
-		list = nil
-	}
-	log.Printf("list=%v", list)
-	return
-}
-
-// LoadConfig reads a file as a TOML document and return the structure
-func LoadConfig(name string) (s *Server, err error) {
-	// Load global file
-	gFile := filepath.Join(baseDir, DefaultName)
-
-	log.Printf("global is %s", gFile)
-	c, err := loadGlobal(gFile)
-	if err != nil {
-		return
-	}
-	if name == "" {
-		s, err = loadInstance(c.Default)
-	} else {
-		s, err = loadInstance(name)
-	}
-
-	return s, err
-}
-
-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)
-	}
-
-	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
-}
--- a/config_test.go	Tue Apr 11 17:15:12 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,105 +0,0 @@
-package gondole
-
-import (
-	"github.com/stretchr/testify/assert"
-	"os"
-	"path/filepath"
-	"testing"
-)
-
-func TestLoadGlobal(t *testing.T) {
-	baseDir = "."
-
-	_, err := loadGlobal(filepath.Join("test", "non.toml"))
-	assert.Error(t, err, "does not exist")
-
-	_, err = loadGlobal(filepath.Join("test", "garbage.token"))
-	assert.Error(t, err, "just garbage")
-
-	// git does now allow you to checkin 000 files :(
-	err = os.Chmod(filepath.Join("test", "perms.toml"), 0000)
-	assert.NoError(t, err, "should be fine")
-	_, err = loadGlobal(filepath.Join("test", "perms.toml"))
-	assert.Error(t, err, "unreadable")
-	err = os.Chmod(filepath.Join("test", "perms.toml"), 0600)
-	assert.NoError(t, err, "should be fine")
-
-	c, err := loadGlobal(filepath.Join("test", "config.toml"))
-	assert.NoError(t, err, "should read it fine")
-	assert.EqualValues(t, "foo", c.Default, "equal")
-}
-
-func TestLoadInstance(t *testing.T) {
-	baseDir = "."
-
-	_, err := loadInstance("nonexistent")
-	assert.Error(t, err, "does not exist")
-
-	file := filepath.Join("test", "garbage")
-	_, err = loadInstance(file)
-	assert.Error(t, err, "just garbage")
-
-	file = filepath.Join("test", "foo.token")
-	err = os.Chmod(file, 0000)
-	assert.NoError(t, err, "should be fine")
-
-	file = filepath.Join("test", "foo")
-	_, err = loadInstance(file)
-	assert.Error(t, err, "unreadable")
-
-	file = filepath.Join("test", "foo.token")
-	err = os.Chmod(file, 0644)
-	assert.NoError(t, err, "should be fine")
-
-	real := &Server{
-		ID:          "666",
-		Name:        "foo",
-		BearerToken: "d3b07384d113edec49eaa6238ad5ff00",
-		BaseURL:     "https://mastodon.social",
-	}
-	file = filepath.Join("test", "foo")
-	s, err := loadInstance(file)
-	assert.NoError(t, err, "all fine")
-	assert.Equal(t, real, s, "equal")
-}
-
-func TestGetInstanceList(t *testing.T) {
-	baseDir = "test"
-
-	real := []string{
-		filepath.Join("test", "foo.token"),
-		filepath.Join("test", "garbage.token"),
-	}
-	list := GetInstanceList()
-	assert.Equal(t, real, list, "equal")
-
-	baseDir = "/tmp"
-	real = nil
-	list = GetInstanceList()
-	assert.Equal(t, real, list, "equal")
-
-	baseDir = "/nonexistent"
-	real = nil
-	list = GetInstanceList()
-	assert.Equal(t, real, list, "equal")
-}
-
-func TestLoadConfig(t *testing.T) {
-	baseDir = "test"
-
-	_, err := LoadConfig("foo")
-	assert.NoError(t, err, "should be fine")
-
-	_, err = LoadConfig("")
-	assert.NoError(t, err, "should be fine")
-
-	err = os.Chmod(filepath.Join("test", "config.toml"), 0000)
-	assert.NoError(t, err, "should be fine")
-
-	_, err = LoadConfig("")
-	assert.Error(t, err, "should be unreadable")
-
-	err = os.Chmod(filepath.Join("test", "config.toml"), 0600)
-	assert.NoError(t, err, "should be fine")
-
-}
\ No newline at end of file