Skip to content

Add string matching helper functions to Go templates #6370

Description

@iilyak

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.

There are quite a few string related function in https://github.com/Masterminds/sprig including the function which would help in my case:

  • contains
  • hasPrefix
  • hasSuffix
  • regexMatch

If inclusion of sprig into go-gh is not an option, would it be possible to implement at least contains in the pkg/template/template.go?

Activity

  1. samcoe commented on Sep 28, 2022

    @samcoe
    Contributor

    @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.

  2. iilyak commented on Sep 28, 2022

    @iilyak
    Author

    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 -}}
    
  3. transferred this issue fromcli/go-ghon Sep 29, 2022
  4. 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
  5. mislav commented on Sep 29, 2022

    @mislav
    Contributor

    @iilyak Thank you for the feature request! Agreed that adding some string helpers would be really helpful.

    Related: #5976 (reply in thread)

  6. added
    enhancementa request to improve CLI
    and removed on Sep 29, 2022
  7. LangLangBart commented on Sep 29, 2022

    @LangLangBart

    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:" -}}

    💁

  8. self-assigned this
    on Feb 18, 2025
  9. BagToad commented on Feb 19, 2025

    @BagToad
    Member

    @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.

    We think this might go here: https://github.com/cli/go-gh/blob/13104ed7b2db4b8c1a83de4248ddfaaab7682916/pkg/template/template.go#L36

  10. BagToad commented on Feb 20, 2025

    @BagToad
    Member

    @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 filtering would likely be done by calling GenericFuncMap() to get all the functions from sprig then filter it before piping it in to template.FuncMap() from Go's built-in "text/template" package like sprig does in its TxtFuncMap().

  11. iamazeem commented on Feb 20, 2025

    @iamazeem
    Contributor

    @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 filtering would likely be done by calling GenericFuncMap() to get all the functions from sprig then filter it before piping it in to template.FuncMap() from Go's built-in "text/template" package like sprig does in its TxtFuncMap().

    @BagToad: Sounds good to me. 👍
    Importing all the functions won't be as straightforward due to the conflict with the existing functions:

    go-gh sprig
    join join
    pluck pluck
    timeago ago
    timefmt date
    truncate trunc

    Not all of these sprig equivalents exactly map to go-gh so it won't be a drop-in replacement i.e. a breaking change w.r.t. UX.

  12. iamazeem commented on Feb 20, 2025

    @iamazeem
    Contributor

    @BagToad: PR cli/go-gh#184 created. Please review. Thanks!

  13. BagToad commented on Mar 7, 2025

    @BagToad
    Member

    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.

  14. iamazeem commented on Mar 7, 2025

    @iamazeem
    Contributor

    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
    ...
  15. BagToad commented on Mar 7, 2025

    @BagToad
    Member

    Ah yes! Good call @iamazeem. I'm going to re-open this issue with the expected output being:

    • gh formatting help text is updated to reflect the new sprig functions implemented in Integrate sprig library go-gh#184
    • cli/cli's go-gh version is updated (blocked on a new go-gh release, I'll set a reminder to create the go-gh release on Monday next week)
  16. reopened this on Mar 7, 2025
  17. BagToad commented on Mar 10, 2025

    @BagToad
    Member

    Delaying the next go-gh release a bit as we have a few other go-gh changes in the pipeline & we want to reduce version churn for go-gh consumers.

    cc @andyfeller

  18. self-assigned this
    on Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementa request to improve CLIgh-helprelating to the gh help commandhelp wantedContributions welcome

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions