forked from tomnomnom/qsreplace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
62 lines (50 loc) · 1.25 KB
/
main.go
File metadata and controls
62 lines (50 loc) · 1.25 KB
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
package main
import (
"bufio"
"flag"
"fmt"
"net/url"
"os"
"sort"
"strings"
)
func main() {
var appendMode bool
flag.BoolVar(&appendMode, "a", false, "Append the value instead of replacing it")
flag.Parse()
seen := make(map[string]bool)
// read URLs on stdin, then replace the values in the query string
// with some user-provided value
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
u, err := url.Parse(sc.Text())
if err != nil {
fmt.Fprintf(os.Stderr, "failed to parse url %s [%s]\n", sc.Text(), err)
continue
}
// Go's maps aren't ordered, but we want to use all the param names
// as part of the key to output only unique requests. To do that, put
// them into a slice and then sort it.
pp := make([]string, 0)
for p, _ := range u.Query() {
pp = append(pp, p)
}
sort.Strings(pp)
key := fmt.Sprintf("%s%s?%s", u.Hostname(), u.EscapedPath(), strings.Join(pp, "&"))
// Only output each host + path + params combination once
if _, exists := seen[key]; exists {
continue
}
seen[key] = true
qs := url.Values{}
for param, vv := range u.Query() {
if appendMode {
qs.Set(param, vv[0]+flag.Arg(0))
} else {
qs.Set(param, flag.Arg(0))
}
}
u.RawQuery = qs.Encode()
fmt.Printf("%s\n", u)
}
}