| 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/sipp/tui" |
| 11 | ) |
| 12 | |
| 13 | func runUpload(args []string) { |
| 14 | var file string |
| 15 | for _, a := range args { |
| 16 | if !strings.HasPrefix(a, "-") { |
| 17 | file = a |
| 18 | break |
| 19 | } |
| 20 | } |
| 21 | if file == "" { |
| 22 | fmt.Fprintln(os.Stderr, "no file specified") |
| 23 | fmt.Fprint(os.Stderr, usage) |
| 24 | os.Exit(2) |
| 25 | } |
| 26 | |
| 27 | data, err := os.ReadFile(file) |
| 28 | if err != nil { |
| 29 | fmt.Fprintln(os.Stderr, "read file:", err) |
| 30 | os.Exit(1) |
| 31 | } |
| 32 | name := filepath.Base(file) |
| 33 | |
| 34 | opts := tui.ParseArgs(args) |
| 35 | if opts.RemoteURL == "" { |
| 36 | opts.RemoteURL = os.Getenv("SIPP_REMOTE_URL") |
| 37 | } |
| 38 | if opts.RemoteURL == "" { |
| 39 | cfg, _ := tui.LoadConfig() |
| 40 | opts.RemoteURL = cfg.RemoteURL |
| 41 | } |
| 42 | if opts.RemoteURL == "" { |
| 43 | fmt.Fprintln(os.Stderr, "remote URL not set (use -r, SIPP_REMOTE_URL, or `sipp auth`)") |
| 44 | os.Exit(2) |
| 45 | } |
| 46 | |
| 47 | backend, err := tui.ResolveBackend(opts) |
| 48 | if err != nil { |
| 49 | fmt.Fprintln(os.Stderr, "backend:", err) |
| 50 | os.Exit(1) |
| 51 | } |
| 52 | defer backend.Close() |
| 53 | |
| 54 | snippet, err := backend.Create(name, string(data)) |
| 55 | if err != nil { |
| 56 | fmt.Fprintln(os.Stderr, "create snippet:", err) |
| 57 | os.Exit(1) |
| 58 | } |
| 59 | |
| 60 | if remote := backend.RemoteURL(); remote != "" { |
| 61 | link := strings.TrimRight(remote, "/") + "/s/" + snippet.ShortID |
| 62 | fmt.Println(link) |
| 63 | if err := clipboard.WriteAll(link); err == nil { |
| 64 | fmt.Println("(copied to clipboard)") |
| 65 | } |
| 66 | } else { |
| 67 | fmt.Println("created:", snippet.ShortID) |
| 68 | } |
| 69 | } |