Skip to content

Commit

Permalink
New funcs remove_from_url & value_from_url
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrode committed Nov 9, 2023
1 parent 8c109a9 commit f48f5c0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,14 @@ Example:
Write **msg** to log output. Args can be given. This uses logrus.Debugf to output.
## `remove_from_url` [key] [url]
Removes from **key** from **url**'s query, returns the **url** with the **key** removed. In case of an error, the **url** is returned as is. Unparsable urls are ignored and the **url** is returned.
## `value_from_url` [key]
Returns the **value** from the **url**'s query for **key**. In case of an error, an empty string is returned. Unparsable urls are ignored and an empty string is returned.
# HTTP Server
The apitest tool includes an HTTP Server. It can be used to serve files from the local disk temporarily. The HTTP Server can run in test mode. In this mode, the apitest tool does not run any tests, but starts the HTTP Server in the foreground, until CTRL-C in pressed.
Expand Down
21 changes: 21 additions & 0 deletions pkg/lib/template/template_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,27 @@ func (loader *Loader) Render(
logrus.Debugf(msg, args...)
return ""
},
// remove_from_url removes key from url's query part, returns
// the new url
"remove_from_url": func(qKey, urlStr string) (urlPatched string) {
u, err := url.Parse(urlStr)
if err != nil {
return urlStr
}
q := u.Query()
q.Del(qKey)
u.RawQuery = q.Encode()
return u.String()
},
// value_from_url returns the value from url's query part
"value_from_url": func(qKey, urlStr string) string {
u, err := url.Parse(urlStr)
if err != nil {
return ""
}
q := u.Query()
return q.Get(qKey)
},
}
tmpl, err := template.
New("tmpl").
Expand Down

0 comments on commit f48f5c0

Please sign in to comment.