Repository navigation
Add string matching helper functions to Go templates #6370
Description
Activity
@iilyak Thanks for writing in. Is the extension that you are working on written in bash? This repo is for the
go-ghGo package which can be used byghextensions written in Go. If you want commands fromghto support these template functions this issue is more suited for the cli/cli repo where this would need to be addressed.Is the extension that you are working on written in bash? This repo is for the go-gh Go package which can be used by gh extensions written in Go. If you want commands from gh to support these template functions this issue is more suited for the cli/cli repo where this would need to be addressed.
The cli/cli calls out into go-gh to handle template rendering here (template module is imported here). For the context I am trying to do something similar to https://github.com/samcoe/gh-triage/blob/main/gh-triage
{{- range $index, $label := $issue.labels -}} {{- if not $found -}} {{- if eq $label.name "p1" "p2" "p3" "core" "help wanted" "tracking issue" "needs-design" "blocked" "needs-user-input" -}} {{- $found = true -}} {{- end -}} {{- end -}} {{- end -}}However in my case all labels in the condition would be prefixed with
project:. So it would look like:{{- range $index, $label := $issue.labels -}} {{- if not $found -}} {{- if hasPrefix $label.name "project:" -}} {{- $found = true -}} {{- end -}} {{- end -}} {{- end -}}- changed the title
[-]Template support for functions from https://github.com/Masterminds/sprig[/-][+]Add string matching helper functions to Go templates[/+]on Sep 29, 2022 @iilyak Thank you for the feature request! Agreed that adding some string helpers would be really helpful.
Related: #5976 (reply in thread)
Reacted by LangLangBart- addedenhancementa request to improve CLIa request to improve CLIand removedneeds-triageneeds to be reviewedneeds to be reviewed
on Sep 29, 2022 hello,
if you know your prefix and its exact length, would the following fit your use case ?# posted by iilyak {{- if hasPrefix $label.name "project:" -}} # printf {{- if eq (printf "%.8s" $label.name) "project:" -}}
💁
Reacted by iilyak- addedgh-helprelating to the gh help commandrelating to the gh help command
on Oct 2, 2023 @iamazeem Thanks for opening a PR for this and being patient while we review and discuss! ❤
I've discussed this with @jtmcg and we want to change the expected output for this. We believe this would be better implemented in
go-ghinstead ofcli/cliso extension authors might benefit from it as well.We think this might go here: https://github.com/cli/go-gh/blob/13104ed7b2db4b8c1a83de4248ddfaaab7682916/pkg/template/template.go#L36
Reacted by Azeem@iamazeem & anyone else reading: The team has been continuing to discuss this, and we also want this work to limit the functions we import from
sprigto the functions requested in this issue:- contains
- hasPrefix
- hasSuffix
- regexMatch
We think that importing all the functions is a bit more "unknown" surface area than what we are comfortable with.
This filtering would likely be done by calling
GenericFuncMap()to get all the functions from sprig then filter it before piping it in totemplate.FuncMap()from Go's built-in "text/template" package like sprig does in itsTxtFuncMap().Reacted by Azeem@iamazeem & anyone else reading: The team has been continuing to discuss this, and we also want this work to limit the functions we import from
sprigto the functions requested in this issue:- contains
- hasPrefix
- hasSuffix
- regexMatch
We think that importing all the functions is a bit more "unknown" surface area than what we are comfortable with.
This filtering would likely be done by calling
GenericFuncMap()to get all the functions from sprig then filter it before piping it in totemplate.FuncMap()from Go's built-in "text/template" package like sprig does in itsTxtFuncMap().@BagToad: Sounds good to me. 👍
Importing all the functions won't be as straightforward due to the conflict with the existing functions:go-ghsprigjoinjoinpluckplucktimeagoagotimefmtdatetruncatetruncNot all of these
sprigequivalents exactly map togo-ghso it won't be a drop-in replacement i.e. a breaking change w.r.t. UX.@BagToad: PR cli/go-gh#184 created. Please review. Thanks!
This is fixed by cli/go-gh#184, but will be available when a new release for
cli/go-ghis imported intocli/cli.Reacted by iilyak and AzeemThis is fixed by cli/go-gh#184, but will be available when a new release for
cli/go-ghis imported intocli/cli.@BagToad: May need to update the help text for
gh formattingaccordingly for the supportedsprigfunctions:$ gh formatting ... The `--template` flag requires a string argument in Go template syntax, and will only print those JSON values which match the query. In addition to the Go template functions in the standard library, the following functions can be used with this formatting directive: - `autocolor`: like `color`, but only emits color to terminals - `color <style> <input>`: colorize input using <https://github.com/mgutz/ansi> - `join <sep> <list>`: joins values in the list using a separator - `pluck <field> <list>`: collects values of a field from all items in the input - `tablerow <fields>...`: aligns fields in output vertically as a table - `tablerender`: renders fields added by tablerow in place - `timeago <time>`: renders a timestamp as relative to now - `timefmt <format> <time>`: formats a timestamp using Go's `Time.Format` function - `truncate <length> <input>`: ensures input fits within length - `hyperlink <url> <text>`: renders a terminal hyperlink ...
Reacted by Kynan WareAh yes! Good call @iamazeem. I'm going to re-open this issue with the expected output being:
gh formattinghelp text is updated to reflect the newsprigfunctions implemented in Integrate sprig library go-gh#184cli/cli'sgo-ghversion is updated (blocked on a newgo-ghrelease, I'll set a reminder to create thego-ghrelease on Monday next week)
Reacted by AzeemDelaying the next
go-ghrelease a bit as we have a few othergo-ghchanges in the pipeline & we want to reduce version churn forgo-ghconsumers.cc @andyfeller
In a
ghextension (written in bash) I needed to implement a fuzzy matching on the content of a label. Specifically I needed to exclude issues which has a label withproject:prefix. However, I couldn't find any functions in https://github.com/cli/go-gh/blob/trunk/pkg/template/template.go or standard go templates to supportstartsWith.There are quite a few string related function in https://github.com/Masterminds/sprig including the function which would help in my case:
If inclusion of sprig into go-gh is not an option, would it be possible to implement at least
containsin thepkg/template/template.go?