t

commit 9ccc8779f3633e66a7352a68ecfd2b77fdca785e

Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>

Add command to share notes at a given provider

Currently, we're shipping a `rascunho` instance as a default pastebin
provider, as `rascunho` is also a markdown previewer as a bonus.

You can configure to a private instance if you want, tweaking your
personal `t.conf` file on the share section.

See more about rascunho at:

https://sr.ht/~porcellis/rascunho

 commands/share.go | 45 +++++++++++++++++++++++++++++++++++++++++++++
 main.go | 20 ++++++++++++++++++++


diff --git a/commands/share.go b/commands/share.go
new file mode 100644
index 0000000000000000000000000000000000000000..6f70ac81c63220223a4d76bbeb3e94f97ebdf251
--- /dev/null
+++ b/commands/share.go
@@ -0,0 +1,45 @@
+package commands
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+	"git.sr.ht/~porcellis/t/config"
+	"git.sr.ht/~porcellis/t/models"
+	"io/ioutil"
+	"log"
+	"net/http"
+)
+
+func Share(config config.TConfig, note models.Note) error {
+	url := fmt.Sprintf("%s://%s", config.Share.Protocol, config.Share.Base)
+	api := fmt.Sprintf("%s%s", url, config.Share.Path)
+
+	contents, err := ioutil.ReadFile(note.Path)
+
+	if err != nil {
+		return err
+		log.Fatalln(err)
+	}
+
+	requestBody, err := json.Marshal(map[string]string{
+		"text": string(contents),
+	})
+
+	// TODO: Make this configurable
+	response, err := http.Post(api, "application/json", bytes.NewBuffer(requestBody))
+	if err != nil {
+		return err
+		log.Fatalln(err)
+	}
+
+	defer response.Body.Close()
+
+	var result map[string]interface{}
+
+	json.NewDecoder(response.Body).Decode(&result)
+
+	log.Println(fmt.Sprintf("%s/%s", url, result["sha"]))
+
+	return nil
+}




diff --git a/main.go b/main.go
index 4379b5c3e5fc38379c447d2322de58e8b459939e..ac374bc52d456b29a3c330440d0dd30f36719370 100644
--- a/main.go
+++ b/main.go
@@ -142,6 +142,26 @@ 		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)