apps/jotts/cmd_upload.go 1004 B raw
1
package main
2
3
import (
4
	"fmt"
5
	"os"
6
	"path/filepath"
7
	"strings"
8
9
	"github.com/atotto/clipboard"
10
	"github.com/stevedylandev/andromeda/apps/jotts/tui"
11
)
12
13
func runUpload(args []string) {
14
	path := args[0]
15
	data, err := os.ReadFile(path)
16
	if err != nil {
17
		fmt.Fprintln(os.Stderr, "read file:", err)
18
		os.Exit(1)
19
	}
20
	title := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
21
	if title == "" {
22
		title = "Untitled"
23
	}
24
25
	backend, err := tui.ResolveBackend(tui.ParseArgs(args[1:]))
26
	if err != nil {
27
		fmt.Fprintln(os.Stderr, "backend:", err)
28
		os.Exit(1)
29
	}
30
	defer backend.Close()
31
32
	note, err := backend.Create(title, string(data))
33
	if err != nil {
34
		fmt.Fprintln(os.Stderr, "create note:", err)
35
		os.Exit(1)
36
	}
37
38
	if remote := backend.RemoteURL(); remote != "" {
39
		link := strings.TrimRight(remote, "/") + "/notes/" + note.ShortID
40
		fmt.Println(link)
41
		if err := clipboard.WriteAll(link); err == nil {
42
			fmt.Println("(copied to clipboard)")
43
		}
44
	} else {
45
		fmt.Println("created:", note.ShortID)
46
	}
47
}