t

ref: master

commands/share.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
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
}