-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (87 loc) · 2.92 KB
/
main.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"github.com/alexflint/go-arg"
)
func submitUrl(url url.URL, server *url.URL, code *string, plain bool) {
var jsonData []byte
if code != nil {
jsonData = []byte(`{
"link": "` + url.String() + `",
"requestedCode": "` + *code + `"
}`)
} else {
jsonData = []byte(`{
"link": "` + url.String() + `"
}`)
}
resp, err := http.Post(server.String(), "application/json", bytes.NewBuffer(jsonData))
if err != nil {
panic("Error sending post request to server!")
}
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode == 200 {
if plain {
fmt.Println(strings.TrimRight(server.String(), "/") + "/" + string(body))
} else {
fmt.Println("Successfully created shortlink pointing to " + url.String() + " from " + strings.TrimRight(server.String(), "/") + "/" + string(body))
}
} else {
panic("Error " + resp.Status + " while shortening " + url.String() + ": \n" + string(body))
}
resp.Body.Close()
}
func main() {
configPath := os.Getenv("XDG_CONFIG_HOME")
if(len(configPath) == 0){
homeDir, _ := os.UserHomeDir()
configPath = homeDir + string(os.PathSeparator) + ".config"
}
configPath += string(os.PathSeparator) + "shortlinks_server"
serverURLdata, err := os.ReadFile(configPath)
var serverURL *url.URL
if err == nil {
serverURL, err = url.Parse(string(serverURLdata))
if err != nil {
panic("Error reading saved server url. Delete `shortlinks_service` from your config directory ("+configPath+") and try again.")
}
}
var options struct {
URLs []url.URL `arg:"positional"`
Plain bool `arg:"-p,--plain" help:"only outputs created shortlinks seperated by newlines"`
Code *string `arg:"-c,--request-code" placeholder:"CODE" help:"path to be requested from server"`
Server *url.URL `arg:"-s,--set-server" placeholder:"URL" help:"server url to use for this conversion. When ran with this option alone, it sets the default server."`
}
p := arg.MustParse(&options)
if options.Code == nil && options.Server == nil && len(options.URLs) == 0 {
p.WriteHelp(os.Stdout)
}
if options.Code != nil && len(options.URLs) != 1 {
p.Fail("Request-Code option only valid when shortening a single url.")
} else if options.Server != nil && len(options.URLs) == 0 {
os.Remove(configPath)
folders := strings.Split(configPath, string(os.PathSeparator))
os.MkdirAll(strings.Join(folders[:len(folders)-1], string(os.PathSeparator)), 0666)
os.WriteFile(configPath, []byte(options.Server.String()), 0666)
} else {
if options.Server != nil {
serverURL = options.Server
}
if serverURL == nil {
p.Fail("Shortlinks Server URL not provided. Run \"shorten -s [Server URL]\" to set your default server url.")
}
if options.Code != nil {
submitUrl(options.URLs[0], serverURL, options.Code, options.Plain)
} else {
for _, link := range options.URLs {
submitUrl(link, serverURL, nil, options.Plain)
}
}
}
}