v / cmd / tools / vshare.v
49 lines · 39 sloc · 806 bytes · 13c1e4e5790bc5e5671695ce4d50aeecd7effa31
Raw
1module main
2
3import json
4import net.http
5import os
6import vshare
7
8struct Response {
9 hash string
10 error string
11}
12
13fn main() {
14 if os.args.len < 3 {
15 eprintln('Please provide a file')
16 exit(1)
17 }
18
19 if os.file_ext(os.args[2]) != '.v' {
20 eprintln('Must be a V source file.')
21 exit(1)
22 }
23
24 if !os.is_file(os.args[2]) {
25 eprintln('File not found.')
26 exit(1)
27 }
28
29 to_send := os.args[2]
30
31 content := os.read_file(to_send) or {
32 eprintln(err)
33 exit(1)
34 }
35
36 share := http.post_form('https://play.vlang.io/share', {
37 'code': content
38 })!
39
40 response := json.decode(Response, share.body)!
41 url := 'https://play.vlang.io/p/${response.hash}'
42
43 println(url)
44 if vshare.copy_to_clipboard(url) {
45 println('Copied URL to clipboard.')
46 } else {
47 println('Clipboard copy unavailable. Copy the URL manually.')
48 }
49}
50