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

Support replacements capture groups #702

Closed
Magic-Mayo opened this issue Sep 6, 2024 · 3 comments · Fixed by #707
Closed

Support replacements capture groups #702

Magic-Mayo opened this issue Sep 6, 2024 · 3 comments · Fixed by #707
Assignees
Labels
enhancement New feature or request

Comments

@Magic-Mayo
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Currently, Replacement only allows you to replace with a hardcoded string or with ReplaceData when processing replacements. I'd like to use the shortest possible regex since I'm covering multiple lines of text to find what I'm looking for.

Specifically, I'm trying to update versions of dependencies in a helm chart so I can't just do something like version: .* as there will be multiple lines in the Chart.yaml that have that. I need to get the specific list item and update the version so my regex would look something like this when updating a single dependency:

(- name: chartName.+?version): \d+\.\d+\.\d+

I'd like to use the capture group to put whatever string I found back in place. This helps with not having to format each of those files very specifically and ensures no mistakes are made. It also keeps us from having to do a bunch tedious formatting as well as making sure we don't have to follow a specific format in the future.

Describe the solution you'd like
Supporting regex capture groups

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
I've tested out some changes and can post a PR if you think the feature is worthwhile

@Magic-Mayo Magic-Mayo added the enhancement New feature or request label Sep 6, 2024
@miniscruff
Copy link
Owner

Hmm this is largely left to a package I use I believe. If you could give me an example of the helm chart and regex I can see what I can do.

@Magic-Mayo
Copy link
Contributor Author

Magic-Mayo commented Sep 6, 2024

Sure....

# Chart.yaml

version: 1.0.0
name: common
description: Common chart to be shared

dependencies:
  - name: other
    version: 2.0.0
    repository: https://fake-registry.com/other
  - name: dependency
    repository: https://fake-registry.com/dependency
    condition: dependency.enabled
    version: 1.0.0

Regex to find what I'm looking for and replace a version number:

(- name: other.+?version): \d+\.\d+\.\d+

This would find the dependency chart other in the Chart.yaml for common and replace it's version with the one that was just released. By using the capture group it can just put back the piece it found in the replacement when you do something like this in changie.yaml

# changie.yaml

projects:
  - key: other
    label: Other
    replacements:
      - path: other/Chart.yaml
        find: "^version: .*"
        replace: "version: {{ .VersionNoPrefix }}"
      - path: common/Chart.yaml
        find: "(- name: other.+?version): \\d+\\.\\d+\\.\\d+"
        replace: "{{ index .CaptureGroups 0 }}: {{ .VersionNoPrefix }}"
        flags: s

The thing I tested out was adding a CaptureGroups field to ReplaceData and moving some of the code around to read the file and run a regexp.FindAllStringSubmatch on the current replacement then add that to the new CaptureGroups field but if there's another/better way to do it I'm all for it.

@miniscruff
Copy link
Owner

Hey there @Magic-Mayo I did some testing and found I don't actually need a package for this as the regexp has one already. Go uses $N for index based replacements, or $KEY for key value based replacements. Both will be supported with the next update.

# changie.yaml

projects:
  - key: other
    label: Other
    replacements:
      - path: other/Chart.yaml
        find: "^version: .*"
        replace: "version: {{ .VersionNoPrefix }}"
      - path: common/Chart.yaml
        find: "(- name: other.+?version): \\d+\\.\\d+\\.\\d+"
        replace: "$1: {{ .VersionNoPrefix }}"
        flags: s

^ is what you will be able to do, should have this up and pushed this weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants