providers: Add provider for URL functions

This commit is contained in:
Sasha Koshka 2024-12-06 00:12:50 -05:00
parent 541f2f0620
commit c6b5fd7eeb
2 changed files with 32 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package providers
import "git.tebibyte.media/sashakoshka/step"
import fpos "git.tebibyte.media/sashakoshka/step/providers/os"
import fpurl "git.tebibyte.media/sashakoshka/step/providers/url"
import fpsprig "git.tebibyte.media/sashakoshka/step/providers/sprig"
import fpmarkdown "git.tebibyte.media/sashakoshka/step/providers/markdown"
@ -9,6 +10,7 @@ import fpmarkdown "git.tebibyte.media/sashakoshka/step/providers/markdown"
func All () []step.FuncProvider {
return []step.FuncProvider {
new(fpos.Provider),
new(fpurl.Provider),
new(fpsprig.Provider),
fpmarkdown.Default(),
}

30
providers/url/url.go Normal file
View File

@ -0,0 +1,30 @@
package url
import "net/url"
import "html/template"
import "git.tebibyte.media/sashakoshka/step"
var _ step.FuncProvider = new(Provider)
// Provider provides URL functions.
type Provider struct {
}
// FuncMap fulfills the step.FuncProvider interface.
func (this *Provider) FuncMap () template.FuncMap {
return template.FuncMap {
"parseQuery": funcParseQuery,
}
}
func funcParseQuery (query string) url.Values {
// wrapped here because the query might contain all sorts of nonsense,
// and returning an error in a template function causes everything to
// stop rather ungracefully
values, err := url.ParseQuery(query)
if err != nil {
values = make(url.Values)
}
return values
}