You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add string matching helper functions to Go templates #6370
In a gh extension (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 with project: 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 support startsWith.
@iilyak Thanks for writing in. 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.
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.
{{- 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
@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-gh instead of cli/cli so extension authors might benefit from it as well.
@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 sprig to 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.
@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 sprig to 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 is fixed by cli/go-gh#184, but will be available when a new release for cli/go-gh is imported into cli/cli.
@BagToad: May need to update the help text for gh formatting accordingly for the supported sprig functions:
$ 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...
Activity
samcoe commentedon Sep 28, 2022
@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.iilyak commentedon Sep 28, 2022
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
However in my case all labels in the condition would be prefixed with
project:. So it would look like:[-]Template support for functions from https://github.com/Masterminds/sprig[/-][+]Add string matching helper functions to Go templates[/+]mislav commentedon 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)
LangLangBart commentedon Sep 29, 2022
hello,
if you know your prefix and its exact length, would the following fit your use case ?
💁
BagToad commentedon Feb 19, 2025
@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
BagToad commentedon Feb 20, 2025
@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: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().iamazeem commentedon Feb 20, 2025
@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.iamazeem commentedon Feb 20, 2025
@BagToad: PR cli/go-gh#184 created. Please review. Thanks!
BagToad commentedon Mar 7, 2025
This is fixed by cli/go-gh#184, but will be available when a new release for
cli/go-ghis imported intocli/cli.iamazeem commentedon Mar 7, 2025
@BagToad: May need to update the help text for
gh formattingaccordingly for the supportedsprigfunctions:BagToad commentedon Mar 7, 2025
Ah 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)BagToad commentedon Mar 10, 2025
Delaying 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