~porcellis/t

ba29c240213ee972c0919163e34f3cec1e09528b — Pedro Lucas Porcellis 2 months ago 3ee1e98
Introduce commands instead of wrongly use POSIX args
1 files changed, 63 insertions(+), 63 deletions(-)

M main.go
M main.go => main.go +63 -63
@@ 1,112 1,112 @@
package main

import (
	"fmt"
	"git.sr.ht/~porcellis/t/commands"
	"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"
var Version = "0.1.0"

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

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

	c, err := config.Initialize()
	configuration, err := config.Initialize()

	if err != nil {
		panic(err)
	}

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

	if err != nil {
	if len(os.Args) < 2 {
		usage()

		return
	}

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

			var note models.Note
			notes, _ := commands.BuildList(*c)
	switch os.Args[1] {
	case "create", "c":
		var note models.Note
		if len(os.Args) == 2 {
			note, err = commands.Create(*configuration)
		} else {
			note, err = commands.CreateWithName(*configuration, os.Args[2])
		}

			if opt.Value == "" {
				note = notes[0]
			} else {
				index, err := strconv.Atoi(opt.Value)
		if err != nil {
			panic("We we're not able to create your note")
		}

				if err == nil {
					note = notes[index]
				}
			}
		err = commands.Write(note)

			err = commands.Write(note)
		if err != nil {
			panic("We could not invoke your $EDITOR")
		}

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

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

			err = commands.Sync(*c)
		err = commands.Sync(*configuration)

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

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

			var note models.Note
			if opt.Value != "" {
				note, err = commands.CreateWithName(*c, opt.Value)
			} else {
				note, err = commands.Create(*c)
			}
		fmt.Println(fmt.Sprintf("Note created %s", note.Name))

			if err != nil {
				panic("We we're not able to create your note")
			}
	case "list", "l":
		commands.List(*configuration)
	case "edit", "e":
		var note models.Note
		notes, _ := commands.BuildList(*configuration)

			err = commands.Write(note)
		if len(os.Args) == 2 {
			note = notes[0]
		} else {
			index, err := strconv.Atoi(os.Args[2])

			if err != nil {
				panic("We could not invoke your $EDITOR")
			if err == nil {
				note = notes[index]
			}
		}

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

			if err != nil {
				panic("We could not commit your note")
			}
		err = commands.Commit(*configuration, note)

			err = commands.Sync(*c)
		if err != nil {
			panic("Could not commit your edited note")
		}

			if err != nil {
				panic("We could not sync your notes")
			}
		err = commands.Sync(*configuration)

			println("Note created", note.Name)
		case 'v':
			println("t", Version)
		if err != nil {
			panic("Could not sync your note")
		}

		println("Finished editing ", note.Title())

	case "version", "v":
		println("t ", Version)

	case "help", "h":
		usage()

	default:
		usage()
		os.Exit(1)
	}
}