Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ https://example.com/pathtwo?one=1newval&two=2newval
https://example.net/a/path?one=1newval&two=2newval
```

### Modify Query String Values one at a time

```
▶ cat urls.txt | qsreplace -u newval
https://example.com/path?one=newval&two=2
https://example.com/path?one=1&two=newval
https://example.com/pathtwo?one=1&two=newval
https://example.com/pathtwo?one=newval&two=2
https://example.net/a/path?one=1&two=newval
https://example.net/a/path?one=newval&two=2
```

### Remove Duplicate URL and Parameter Combinations

You can omit the argument to `-a` to only output each combination of URL and query string parameters once:
Expand Down
43 changes: 33 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (

func main() {
var appendMode bool
var uniqueMode bool
flag.BoolVar(&appendMode, "a", false, "Append the value instead of replacing it")
flag.BoolVar(&uniqueMode, "u", false, "Uniquely modify one parameter at a time")
flag.Parse()

seen := make(map[string]bool)
Expand All @@ -31,7 +33,7 @@ func main() {
// 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() {
for p := range u.Query() {
pp = append(pp, p)
}
sort.Strings(pp)
Expand All @@ -44,18 +46,39 @@ func main() {
}
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))
if uniqueMode {
old_qs := u.Query()
for param := range u.Query() {
qs := url.Values{}
if appendMode {
qs.Set(param, old_qs.Get(param)+flag.Arg(0))
} else {
qs.Set(param, flag.Arg(0))
}
for p := range u.Query() {
if p != param {
qs.Set(p, old_qs.Get(p))
}
}

u.RawQuery = qs.Encode()

fmt.Printf("%s\n", u)
}
} else {
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()
u.RawQuery = qs.Encode()

fmt.Printf("%s\n", u)
fmt.Printf("%s\n", u)
}

}

Expand Down