Skip to content

Commit

Permalink
add rp
Browse files Browse the repository at this point in the history
  • Loading branch information
davemolk committed Dec 5, 2022
1 parent dbeaab6 commit bc65345
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ parse urls in style. read urls from a line-separated file or from stdin. Get dom
## pw
need a kinda sorta safe(ish?) password and too lazy to log into your pw manager or some rando genderating rando site? type something in and get a mostly gibberish password that contains at least one lower-case letter, one upper-case, one special character, and one number. there are, of course, much easier ways to do this -- I was mainly interested in practicing pipelines :)

## rp
replace a key=value parameter in a given URL(s) with your own key=value. Bonus! Pipe in an encoded mess and decode it.

## tas
throw against site (pull down archived links from Wayback Machine, run against the site, see what status codes currently are)

Expand Down
76 changes: 76 additions & 0 deletions rp/main.go
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)
}
}
33 changes: 33 additions & 0 deletions rp/readme.md
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."}]]
```
4 changes: 4 additions & 0 deletions rp/urls.txt
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

0 comments on commit bc65345

Please sign in to comment.