t

commit 181977b5d1c1592d04ac358fd7a8a5fa9c1600b5

Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>

Load configuration from file instead of hardcoded defaults

 Makefile | 4 ++
 commands/sync.go | 2 
 config/config.go | 64 ++++++++++++++++++++++++++++++++++++++++++-------
 config/t.conf | 22 +++++++++++++++++
 go.mod | 2 +


diff --git a/Makefile b/Makefile
index 72549a83f0a05e9665ed72df31020f3d1900cac5..98ca987c31f5154eb1e8140e8e0b94e7a4d3c190 100644
--- a/Makefile
+++ b/Makefile
@@ -5,14 +5,16 @@
 PREFIX?=/usr/local
 _INSTDIR=$(DESTDIR)$(PREFIX)
 BINDIR?=$(_INSTDIR)/bin
+SHAREDIR?=$(_INSTDIR)/share/t
 MANDIR?=$(_INSTDIR)/share/man
 
 all: t doc
 
 install: all
-	mkdir -m755 -p $(BINDIR) $(MANDIR)/man1
+	mkdir -m755 -p $(BINDIR) $(MANDIR)/man1 $(SHAREDIR)
 	install -m755 t $(BINDIR)/t
 	install -m644 docs/t.1 $(MANDIR)/man1/t.1
+	install -m644 config/t.conf $(SHAREDIR)
 
 .go:
 		mkdir -p $(dir $(PKGPATH))




diff --git a/commands/sync.go b/commands/sync.go
index b95b946bc5773b59db7ca6f55a0edc2692b2c796..1e615871292346c80e4fd6eaae641c5192ab5e74 100644
--- a/commands/sync.go
+++ b/commands/sync.go
@@ -7,7 +7,7 @@ 	"os/exec"
 )
 
 func Sync(config config.TConfig) error {
-	cmd := exec.Command("git", "-C", config.BasePath, "push", config.GitRemote, "-u", "--quiet")
+	cmd := exec.Command("git", "-C", config.BasePath, "push", config.Sync.Remote, "-u", "--quiet")
 	cmd.Stdin = nil
 	cmd.Stdout = os.NewFile(0, os.DevNull)
 	cmd.Stderr = os.Stderr




diff --git a/config/config.go b/config/config.go
index d9157a9a2676b2e19419ccdb05963afe68fa6e5b..aac78becc1a71f4c39c7ff29c49434da869c3e15 100644
--- a/config/config.go
+++ b/config/config.go
@@ -1,13 +1,63 @@
 package config
 
 import (
-	"os/user"
+	"log"
 	"path"
+
+	"github.com/go-ini/ini"
+	"github.com/kyoh86/xdg"
 )
 
+type ShareConfig struct {
+	Protocol string `ini:"protocol"`
+	Base     string `ini:"base"`
+	Path     string `ini:"path"`
+}
+
+type SyncConfig struct {
+	Remote string `ini:"remote"`
+}
+
 type TConfig struct {
-	BasePath  string
-	GitRemote string
+	BasePath string      `ini:"base"`
+	Share    ShareConfig `ini:"-"`
+	Sync     SyncConfig  `ini:"-"`
+}
+
+func LoadConfigFromFile(root *string, sharedir string) (*TConfig, error) {
+	// TODO: Handle loading from the default config
+	filename := path.Join(xdg.ConfigHome(), "t", "t.conf")
+
+	file, err := ini.LoadSources(ini.LoadOptions{
+		KeyValueDelimiters: "=",
+	}, filename)
+
+	if err != nil {
+		// TODO: Install if was not installed within the make command
+		log.Fatalln("Can't find config")
+		return nil, err
+	}
+
+	// TODO: Handle converting ~/ into the user default home directory
+	config := &TConfig{
+		Share: ShareConfig{
+			Protocol: "https",
+			Base:     "rascunho.eletrotupi.com",
+			Path:     "/api/v1",
+		},
+
+		Sync: SyncConfig{
+			Remote: "origin",
+		},
+	}
+
+	err = file.MapTo(&config)
+
+	if err != nil {
+		return nil, err
+	}
+
+	return config, nil
 }
 
 func LoadBasicConfiguration() (*TConfig, error) {
@@ -15,15 +65,11 @@ 	var (
 		err error
 	)
 
-	currentUser, err := user.Current()
+	config, err := LoadConfigFromFile(nil, "")
 
 	if err != nil {
-		panic("Could not get the current user")
+		return nil, err
 	}
-
-	base := path.Join(currentUser.HomeDir, "notes")
-
-	config := &TConfig{BasePath: base, GitRemote: "origin"}
 
 	return config, err
 }




diff --git a/config/t.conf b/config/t.conf
new file mode 100644
index 0000000000000000000000000000000000000000..e076e758e1bf590b79ea69c8ac3524ad8661c4b8
--- /dev/null
+++ b/config/t.conf
@@ -0,0 +1,22 @@
+# Where are your notes stored on your disk
+base = "~/notes"
+
+# These are configurations related to git
+[sync]
+remote = "origin"
+
+# These are configurations related to how the share command will build it's url
+# and other additional info.
+#
+# Ships with the rascunho as the default share option, courtesy of
+# eletrotupi.com
+#
+# rascunho is a no bullshit, no tracking, no ads, no javascript markdown
+# previewer.
+#
+# See more at https://sr.ht/~porcellis/rascunho
+
+[share]
+protocol = "https"
+base = "rascunho.eletrotupi.com"
+path = "/api/v1"




diff --git a/go.mod b/go.mod
index 8110271f9fc5d0f776682b1d817b87a13935c3e1..64dc38a0f929d4c98a1a092129202110c59ac4df 100644
--- a/go.mod
+++ b/go.mod
@@ -4,5 +4,7 @@ go 1.13
 
 require (
 	git.sr.ht/~sircmpwn/getopt v0.0.0-20190808004552-daaf1274538b
+	github.com/go-ini/ini v1.62.0
+	github.com/kyoh86/xdg v1.2.0
 	github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23
 )