~porcellis/t

ref: 9ccc8779f3633e66a7352a68ecfd2b77fdca785e t/commands/share.go -rw-r--r-- 886 bytes
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
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
}