Skip to content

Commit

Permalink
Add wildcard passthrough (#31)
Browse files Browse the repository at this point in the history
- Add wildcard passthrough
- Ignore reserved keywords when present in URL paths
  • Loading branch information
C0urante authored and issmirnov committed Aug 24, 2019
1 parent 7aaf69d commit f65c27c
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 3 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ go test ./... -v

- [Ivan Smirnov](http://ivansmirnov.name)
- [Sergey Smirnov](https://smirnov.nyc/)
- [Chris Egerton](https://github.com/C0urante)
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ You'll notice the difference is that we have to run as `root` in order to bind t

The config file is located at `/usr/local/etc/zap/c.yml` on OSX. For ubuntu, you will have to create `/etc/zap/c.yml` by hand.

Open up `c.yml` and update the mappings you would like. You can nest arbitrarily deep. Expansions work on strings and ints. Notice that we have three keywords available: `expand`, `query`, and `port`.
Open up `c.yml` and update the mappings you would like. You can nest arbitrarily deep. Expansions work on strings and ints. Notice that we have three reserved keywords available: `expand`, `query`, and `port`.

- `expand` - takes a short token and expands it to the specified string. Turns `z` into `zap/`,
- `query` - acts almost like the `expand` option, but drops the separating slash between query expansion and search term (`example.com?q=foo` instead of `example.com?q=/foo`).
- `port` - only valid as the first child under a host. Takes an int and appends it as `:$INT` to the host defined. See the usage in the [sample config](c.yml)

Additionally, you can use `"*"` to capture a path element that should be retained as-is while also allowing for expansion of later elements to take place.

Important gotcha: yaml has [reserved types](http://yaml.org/type/bool.html) and thus `n`, `y`, `no` and the like need to be quoted. See the sample config.

When you add a new shortcut, you need to indicate to your web browser that it's not a search term. You can do this by typing it in once with just a slash. For example, if you add a shortcut `g/z` -> `github.com/issmirnov/zap`, if you try `g/z` right away you will get taken to the search page. Instead, try `g/` once, and then `g/z`. This initial step only needs to be taken once per new shortcut.
Expand Down Expand Up @@ -159,6 +161,15 @@ l:
port: 8080
"n":
port: 9001
ak:
expand: kafka.apache.org
hi:
expand: contact
"*":
j:
expand: javadoc/index.html?overview-summary.html
d:
expand: documentation.html
```
With this config, you can use the following queries:
Expand All @@ -167,6 +178,8 @@ With this config, you can use the following queries:
- `f/zuck` -> facebook.com/zuck
- `f/php` -> facebook.com/groups/2204685680/
- `r/catsstandingup` -> reddit.com/r/catsstandingup
- `ak/hi` -> kafka.apache.org/contact
- `ak/23/j` -> kafka.apache.org/23/javadoc/index.html?overview-summary.html

### Troubleshooting

Expand Down Expand Up @@ -242,3 +255,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md)
## Contributors

- [Ivan Smirnov](http://ivansmirnov.name)
- [Chris Egerton](https://github.com/C0urante)
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
expandKey = "expand"
queryKey = "query"
portKey = "port"
passKey = "*"
sslKey = "ssl_off"
httpsPrefix = "https:/" // second slash appended in expandPath() call
httpPrefix = "http:/" // second slash appended in expandPath() call
Expand Down
16 changes: 16 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ l:
port: 8080
s:
expand: service
ak:
expand: kafka.apache.org
hi:
expand: contact
"*":
d:
expand: documentation.html
j:
expand: javadoc/index.html?overview-summary.html
wc:
expand: wildcard.com
"*":
"*":
"*":
four:
expand: "4"
`

func loadTestYaml() (*gabs.Container, error) {
Expand Down
24 changes: 22 additions & 2 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func getPrefix(c *gabs.Container) (string, int, error) {
}
return "", 0, fmt.Errorf("unexpected type of expansion value, got %T instead of int or string", d)
}

q := c.Path(queryKey).Data()
if q != nil {
if s, ok := q.(string); ok {
Expand Down Expand Up @@ -70,8 +71,8 @@ func expandPath(c *gabs.Container, token *list.Element, res *bytes.Buffer) {
return
}
children := c.ChildrenMap()
child, ok := children[token.Value.(string)]
if ok {
tokVal := token.Value.(string)
if child, ok := children[tokVal]; !isReserved(tokVal) && ok {
p, action, err := getPrefix(child)
if err != nil {
fmt.Println(err.Error())
Expand Down Expand Up @@ -99,6 +100,11 @@ func expandPath(c *gabs.Container, token *list.Element, res *bytes.Buffer) {
}
expandPath(child, token.Next(), res)
return
} else if child, ok := children[passKey]; ok {
res.WriteString("/")
res.WriteString(token.Value.(string))
expandPath(child, token.Next(), res)
return
}

// if tokens left over, append the rest
Expand All @@ -107,3 +113,17 @@ func expandPath(c *gabs.Container, token *list.Element, res *bytes.Buffer) {
res.WriteString(e.Value.(string))
}
}

func isReserved(pathElem string) bool {
switch pathElem {
case
expandKey,
queryKey,
portKey,
passKey,
sslKey:
return true
default:
return false
}
}
84 changes: 84 additions & 0 deletions text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,88 @@ func TestExpander(t *testing.T) {
So(res.String(), ShouldEqual, "https://github.com/search?q=foo/bar/baz/")
})
})
Convey("Given 'g/query/homebrew'", t, func() {
c, _ := loadTestYaml()
l := tokenize("g/query/homebrew")
var res bytes.Buffer
res.WriteString(httpsPrefix)

expandPath(c, l.Front(), &res)

Convey("result should equal 'https://github.com/query/homebrew'", func() {
So(res.String(), ShouldEqual, "https://github.com/query/homebrew")
})
})
Convey("Given 'wc/1/*/3/four'", t, func() {
c, _ := loadTestYaml()
l := tokenize("wc/1/*/3/four")
var res bytes.Buffer
res.WriteString(httpsPrefix)

expandPath(c, l.Front(), &res)

Convey("result should equal 'https://wildcard.com/1/*/3/4'", func() {
So(res.String(), ShouldEqual, "https://wildcard.com/1/*/3/4")
})
})
Convey("Given 'wc/1/2/3/four'", t, func() {
c, _ := loadTestYaml()
l := tokenize("wc/1/2/3/four")
var res bytes.Buffer
res.WriteString(httpsPrefix)

expandPath(c, l.Front(), &res)

Convey("result should equal 'https://wildcard.com/1/2/3/4'", func() {
So(res.String(), ShouldEqual, "https://wildcard.com/1/2/3/4")
})
})
Convey("Given 'ak/hi'", t, func() {
c, _ := loadTestYaml()
l := tokenize("ak/hi")
var res bytes.Buffer
res.WriteString(httpsPrefix)

expandPath(c, l.Front(), &res)

Convey("result should equal 'https://kafka.apache.org/contact", func() {
So(res.String(), ShouldEqual, "https://kafka.apache.org/contact")
})
})
Convey("Given 'ak/23'", t, func() {
c, _ := loadTestYaml()
l := tokenize("ak/23")
var res bytes.Buffer
res.WriteString(httpsPrefix)

expandPath(c, l.Front(), &res)

Convey("result should equal 'https://kafka.apache.org/23", func() {
So(res.String(), ShouldEqual, "https://kafka.apache.org/23")
})
})
Convey("Given 'ak/23/j", t, func() {
c, _ := loadTestYaml()
l := tokenize("ak/23/j")
var res bytes.Buffer
res.WriteString(httpsPrefix)

expandPath(c, l.Front(), &res)

Convey("result should equal 'https://kafka.apache.org/23/javadoc/index.html?overview-summary.html", func() {
So(res.String(), ShouldEqual, "https://kafka.apache.org/23/javadoc/index.html?overview-summary.html")
})
})
Convey("Given 'ak/expand/j'", t, func() {
c, _ := loadTestYaml()
l := tokenize("ak/expand/j")
var res bytes.Buffer
res.WriteString(httpsPrefix)

expandPath(c, l.Front(), &res)

Convey("result should equal 'https://kafka.apache.org/expand/javadoc/index.html?overview-summary.html", func() {
So(res.String(), ShouldEqual, "https://kafka.apache.org/expand/javadoc/index.html?overview-summary.html")
})
})
}

0 comments on commit f65c27c

Please sign in to comment.