From f65c27c8b82499ffd1ce2dda1f343d4b7ab53472 Mon Sep 17 00:00:00 2001 From: Chris Egerton Date: Sat, 24 Aug 2019 14:27:06 -0700 Subject: [PATCH] Add wildcard passthrough (#31) - Add wildcard passthrough - Ignore reserved keywords when present in URL paths --- CONTRIBUTING.md | 1 + README.md | 16 +++++++++- config.go | 1 + main_test.go | 16 ++++++++++ text.go | 24 ++++++++++++-- text_test.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 139 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f3f6887..e1dec68 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -32,3 +32,4 @@ go test ./... -v - [Ivan Smirnov](http://ivansmirnov.name) - [Sergey Smirnov](https://smirnov.nyc/) +- [Chris Egerton](https://github.com/C0urante) \ No newline at end of file diff --git a/README.md b/README.md index ddddaf4..a3db018 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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: @@ -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 @@ -242,3 +255,4 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) ## Contributors - [Ivan Smirnov](http://ivansmirnov.name) +- [Chris Egerton](https://github.com/C0urante) \ No newline at end of file diff --git a/config.go b/config.go index ddbb0f2..6a013fc 100644 --- a/config.go +++ b/config.go @@ -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 diff --git a/main_test.go b/main_test.go index 4664e1c..f1d4db6 100644 --- a/main_test.go +++ b/main_test.go @@ -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) { diff --git a/text.go b/text.go index 56e7d70..57af5ef 100644 --- a/text.go +++ b/text.go @@ -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 { @@ -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()) @@ -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 @@ -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 + } +} diff --git a/text_test.go b/text_test.go index 19ddc6c..16dd7d8 100644 --- a/text_test.go +++ b/text_test.go @@ -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") + }) + }) }