-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/url" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
var d bool | ||
var kv string | ||
flag.BoolVar(&d, "d", false, "decode query string(s)") | ||
flag.StringVar(&kv, "kv", "", "given key, replace value(s)") | ||
flag.Parse() | ||
|
||
if !d && kv == "" { | ||
log.Fatal("must select d or v") | ||
} | ||
|
||
exit := make(chan struct{}) | ||
if d { | ||
go decode(exit) | ||
} else { | ||
go replaceValue(exit, kv) | ||
} | ||
|
||
<-exit | ||
} | ||
|
||
func decode(exit chan struct{}) { | ||
defer close(exit) | ||
s := bufio.NewScanner(os.Stdin) | ||
for s.Scan() { | ||
d, err := url.QueryUnescape(s.Text()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(d) | ||
} | ||
if err := s.Err(); err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func replaceValue(exit chan struct{}, kv string) { | ||
defer close(exit) | ||
|
||
p := strings.Split(kv, "=") | ||
if len(p) != 2 { | ||
log.Fatal("must supply input as key=value") | ||
} | ||
key := p[0] | ||
value := p[1] | ||
|
||
s := bufio.NewScanner(os.Stdin) | ||
for s.Scan() { | ||
u, err := url.ParseRequestURI(s.Text()) | ||
if err != nil { | ||
log.Println(err) | ||
continue | ||
} | ||
params := u.Query() | ||
|
||
params.Set(key, value) | ||
|
||
u.RawQuery = params.Encode() | ||
fmt.Println(u.String()) | ||
} | ||
if err := s.Err(); err != nil { | ||
fmt.Println(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# replace params | ||
Replace a key=value parameter in a given URL(s) with your own key=value. Bonus! Pipe in an encoded mess and decode it. | ||
|
||
## Flags | ||
``` | ||
-q: query parameter as key=value | ||
-d: decode mode | ||
``` | ||
|
||
## Test URLs | ||
``` | ||
https://example.com/foo/bar?first=1&second=2 | ||
https://example.com/bar?foo=me&baz=barz | ||
https://www.google.com/search?name=golang&language=en&mascot=gopher&foo=bar | ||
google.com | ||
``` | ||
|
||
## Usage | ||
(replace a value) | ||
``` | ||
cat urls.txt | rp -v foo=bar | ||
https://example.com/foo/bar?first=1&foo=bar&second=2 // adds &foo=bar | ||
https://example.com/bar?baz=barz&foo=bar // overwrites value for preexisting key | ||
https://www.example.com/rp?foo=bar&language=en&mascot=gopher&name=golang // kv already exists | ||
2022/12/05 05:52:44 parse "google.com": invalid URI for request // no thanks | ||
``` | ||
|
||
(decode query string(s)) | ||
``` | ||
echo "cart=%5B%5B0%2C+%7B%22logo%22%3A+%22kitten.jpg%22%2C+%22price%22%3A+0%2C+%22name%22%3A+%22Kitten%22%2C+%22desc%22%3A+%228%5C%22x10%5C%22+color+glossy+photograph+of+a+kitten.%22%7D%5D%2C+%5B0%2C+%7B%22logo%22%3A+%22kitten.jpg%22%2C+%22price%22%3A+0%2C+%22name%22%3A+%22Kitten%22%2C+%22desc%22%3A+%228%5C%22x10%5C%22+color+glossy+photograph+of+a+kitten.%22%7D%5D%2C+%5B1%2C+%7B%22logo%22%3A+%22puppy.jpg%22%2C+%22price%22%3A+0%2C+%22name%22%3A+%22Puppy%22%2C+%22desc%22%3A+%228%5C%22x10%5C%22+color+glossy+photograph+of+a+puppy.%22%7D%5D%5D" | rp -d | ||
cart=[[0, {"logo": "kitten.jpg", "price": 0, "name": "Kitten", "desc": "8\"x10\" color glossy photograph of a kitten."}], [0, {"logo": "kitten.jpg", "price": 0, "name": "Kitten", "desc": "8\"x10\" color glossy photograph of a kitten."}], [1, {"logo": "puppy.jpg", "price": 0, "name": "Puppy", "desc": "8\"x10\" color glossy photograph of a puppy."}]] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
https://example.com/foo/bar?first=1&second=2 | ||
https://example.com/bar?foo=me&baz=barz | ||
https://www.example.com/rp?name=golang&language=en&mascot=gopher&foo=bar | ||
google.com |