~porcellis/t

181977b5d1c1592d04ac358fd7a8a5fa9c1600b5 — Pedro Lucas Porcellis 3 months ago 97afeb4
Load configuration from file instead of hardcoded defaults
5 files changed, 83 insertions(+), 11 deletions(-)

M Makefile
M commands/sync.go
M config/config.go
A config/t.conf
M go.mod
M Makefile => Makefile +3 -1
@@ 5,14 5,16 @@ PKGPATH = .go/src/$(PKGNAME)
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))

M commands/sync.go => commands/sync.go +1 -1
@@ 7,7 7,7 @@ import (
)

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

M config/config.go => config/config.go +55 -9
@@ 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,16 65,12 @@ func LoadBasicConfiguration() (*TConfig, error) {
		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
}


A config/t.conf => config/t.conf +22 -0
@@ 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"

M go.mod => go.mod +2 -0
@@ 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
)