diff --git a/README.mkd b/README.mkd index 588e4fa..6b17c5c 100644 --- a/README.mkd +++ b/README.mkd @@ -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: diff --git a/main.go b/main.go index 7516383..7e62ad2 100644 --- a/main.go +++ b/main.go @@ -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) @@ -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) @@ -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) + } }