~porcellis/t

ref: 9ccc8779f3633e66a7352a68ecfd2b77fdca785e t/main.go -rw-r--r-- 3.0 KiB
9ccc8779 — Pedro Lucas Porcellis Add command to share notes at a given provider 2 months ago
                                                                                
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main

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

var Version = "0.1.0"

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

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

	configuration, err := config.Initialize()

	if err != nil {
		panic(err)
	}

	if len(os.Args) < 2 {
		usage()

		return
	}

	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 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(*configuration, note)

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

		err = commands.Sync(*configuration)

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

		fmt.Println(fmt.Sprintf("Note created %s", note.Name))

	case "list", "l":
		content, err := commands.List(*configuration)

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

		pager := os.Getenv("PAGER")

		if pager == "" {
			// This options display control chars as raw
			pager = "less -r"
		}

		pagerCommand := strings.Split(pager, " ")

		cmd := exec.Command(pagerCommand[0], pagerCommand[1:]...)
		cmd.Stdin = strings.NewReader(content)
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr

		cmd.Run()

	case "edit", "e":
		var note models.Note
		notes, _ := commands.BuildList(*configuration)

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

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

		err = commands.Write(note)

		err = commands.Commit(*configuration, note)

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

		err = commands.Sync(*configuration)

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

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

	case "show", "s":
		var note models.Note
		notes, _ := commands.BuildList(*configuration)

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

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

		err = commands.Show(note)

		if err != nil {
			panic("There was some error when trying to display the note")
		}

	case "share", "sh":
		var note models.Note
		notes, _ := commands.BuildList(*configuration)

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

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

		err = commands.Share(*configuration, note)

		if err != nil {
			panic(fmt.Sprintf("There was some error when trying to share the note, %s", err))
		}

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

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

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