Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string matching helper functions to Go templates #6370

Open
iilyak opened this issue Sep 27, 2022 · 8 comments · May be fixed by cli/go-gh#184
Open

Add string matching helper functions to Go templates #6370

iilyak opened this issue Sep 27, 2022 · 8 comments · May be fixed by cli/go-gh#184
Assignees
Labels
enhancement a request to improve CLI gh-help relating to the gh help command help wanted Contributions welcome

Comments

@iilyak
Copy link

iilyak commented Sep 27, 2022

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?

@samcoe
Copy link
Contributor

samcoe commented 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-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.

@iilyak
Copy link
Author

iilyak commented Sep 28, 2022

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

@samcoe samcoe transferred this issue from cli/go-gh Sep 29, 2022
@cliAutomation cliAutomation added the needs-triage needs to be reviewed label Sep 29, 2022
@mislav mislav changed the title Template support for functions from https://github.com/Masterminds/sprig Add string matching helper functions to Go templates Sep 29, 2022
@mislav
Copy link
Contributor

mislav commented 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)

@mislav mislav added enhancement a request to improve CLI and removed needs-triage needs to be reviewed labels Sep 29, 2022
@LangLangBart
Copy link

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

💁

@BagToad
Copy link
Member

BagToad commented 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-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

@BagToad
Copy link
Member

BagToad commented 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 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().

@iamazeem
Copy link
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.

@iamazeem iamazeem linked a pull request Feb 20, 2025 that will close this issue
@iamazeem
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement a request to improve CLI gh-help relating to the gh help command help wanted Contributions welcome
Projects
None yet
9 participants