t

ref: structure

./main.go


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main

import (
	"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"

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

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

	c, err := config.Initialize()

	if err != nil {
		panic(err)
	}

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

	if err != nil {
		usage()

		return
	}

	for _, opt := range opts {
		switch opt.Option {
		case 'e':
			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':
			println(opt.Value)

			var note models.Note
			if opt.Value != "" {
				note, err = commands.CreateWithName(*c, opt.Value)
			} else {
				note, err = commands.Create(*c)
			}

			if err != nil {
				panic("We we're not able to create your note")
			}

			err = commands.Write(note)

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

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

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

			err = commands.Sync(*c)

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

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