-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (50 loc) · 934 Bytes
/
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
package main
import (
"flag"
"fmt"
"log"
"net/url"
"os"
"strings"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
if len(os.Args) == 2 && os.Args[1] == "version" {
fmt.Printf("%v, commit %v, built at %v", version, commit, date)
return
}
var encodeURL bool
flag.BoolVar(&encodeURL, "e", false, "Encodes the given URL/String")
flag.Parse()
paramStart := 1
if encodeURL {
paramStart = 2
}
if len(os.Args) <= paramStart {
log.Fatal("Expected at least one parameter")
flag.Usage()
return
}
params := strings.Join(os.Args[paramStart:], " ")
if encodeURL {
fmt.Println(encode(params))
return
}
ret, err := decode(params)
if err != nil {
log.Fatal(err)
flag.Usage()
return
}
fmt.Println(ret)
}
func decode(fullurl string) (string, error) {
return url.PathUnescape(fullurl)
}
func encode(fullurl string) string {
return url.QueryEscape(fullurl)
}