~porcellis/t

2440779b26de7c3dd939177bfa88d0a21c625727 — Pedro Lucas Porcellis 1 year, 7 months ago ba93924
Add a usage and version to the main program
3 files changed, 111 insertions(+), 14 deletions(-)

A commands/commit.go
A commands/sync.go
M main.go
A commands/commit.go => commands/commit.go +39 -0
@@ 0,0 1,39 @@
package commands

import (
	"fmt"
	"git.sr.ht/~porcellis/t/config"
	"git.sr.ht/~porcellis/t/models"
	"os"
	"os/exec"
)

func AddToStagedList(basePath string, notePath string) error {
	cmd := exec.Command("git", "-C", basePath, "add", notePath)
	cmd.Stdin = nil
	cmd.Stdout = nil
	cmd.Stderr = os.Stderr

	return cmd.Run()
}

func Commit(config config.TConfig, note models.Note) error {
	var (
		err error
	)

	err = AddToStagedList(config.BasePath, note.Path)

	if err != nil {
		return err
	}

	commitMessage := fmt.Sprintf("Modified '%s'", note.Title())

	cmd := exec.Command("git", "-C", config.BasePath, "commit", "-am", commitMessage)
	cmd.Stdin = nil
	cmd.Stdout = nil
	cmd.Stderr = os.Stderr

	return cmd.Run()
}

A commands/sync.go => commands/sync.go +16 -0
@@ 0,0 1,16 @@
package commands

import (
	"git.sr.ht/~porcellis/t/config"
	"os"
	"os/exec"
)

func Sync(config config.TConfig) error {
	cmd := exec.Command("git", "-C", config.BasePath, "push", config.GitRemote, "-u", "--quiet")
	cmd.Stdin = nil
	cmd.Stdout = os.NewFile(0, os.DevNull)
	cmd.Stderr = os.Stderr

	return cmd.Run()
}

M main.go => main.go +56 -14
@@ 5,9 5,18 @@ import (
	"git.sr.ht/~porcellis/t/config"
	"git.sr.ht/~porcellis/t/models"
	"git.sr.ht/~sircmpwn/getopt"
	"log"
	"os"

	"strconv"
)

var Version = "0.0.1"

func usage() {
	log.Fatal("Usage: t -[l,c,e]")
}

func main() {
	var (
		c *config.TConfig


@@ 19,16 28,47 @@ func main() {
		panic(err)
	}

	opts, optind, err := getopt.Getopts(os.Args, "le:c:d:")
	opts, _, err := getopt.Getopts(os.Args, "vle:c:d:")

	if err != nil {
		panic(err)
		usage()

		return
	}

	for _, opt := range opts {
		switch opt.Option {
		case 'e':
			println("Edit specified")
			println("Editing")

			var note models.Note
			notes, _ := commands.BuildList(*c)

			if opt.Value == "" {
				note = notes[0]
			} else {
				index, err := strconv.Atoi(opt.Value)

				if err == nil {
					note = notes[index]
				}
			}

			err = commands.Write(note)

			err = commands.Commit(*c, note)

			if err != nil {
				panic("Could not commit your edited note")
			}

			err = commands.Sync(*c)

			if err != nil {
				panic("Could not sync your note")
			}

			println("Finished editing ", note.Title())
		case 'l':
			commands.List(*c)
		case 'c':


@@ 52,19 92,21 @@ func main() {
			}

			// We should call commit
			// commands.Commit()
			err = commands.Commit(*c, note)

			// commands.Sync()
			println("Note created", note.Name)
		case 'd':
			println("Delete specified")
		}
	}
			if err != nil {
				panic("We could not commit your note")
			}

	for _, arg := range os.Args[optind:] {
		if string(arg) == "l" {
			commands.List(*c)
			err = commands.Sync(*c)

			if err != nil {
				panic("We could not sync your notes")
			}

			println("Note created", note.Name)
		case 'v':
			println("t", Version)
		}
	}

}