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

Feat: support partial content for the resource "file" #313

Merged
merged 3 commits into from
Nov 2, 2021

Conversation

dduportal
Copy link
Contributor

@dduportal dduportal commented Oct 17, 2021

Fix #231 , depends on #321

This PR allows manipulating partial content of files.

  • Source of kind file can now:

    • Read the whole content from a file or an URL (same as before)
    • Read only the content of the line (by specifying its number) from a file or an URL (breaking: removes the old behavior that was transfomring lines)
    • Read the content of matched string(s) (new behavior) when provided a regex pattern
  • Conditions of kind file can now check that a file (or an URL) :

    • has its content matching the input source, or the specified content (same as before)
    • has the specified line matching the input source, or the specified content (new)
    • has the specified line not empty (new)
    • has its content, OR the specified line, matching the provided regex pattern (new)
  • Targets of kind file can now update a file:

    • totally, .e.g its whole content from either the source input value or specified content (same as before)
    • partially for a line, e.g. only the specified line is changed to either the source input value or specified content (new)
    • partially for a match + replace regex pattern e.g. a search and replace is applied (new)

How to Test

  • The file examples/updateCli.generic/file.yaml had been updated to be a "reference" of different cases
  • Unit tests had been added to the plugin file to ensure that a lot of cases can be run in less than 1 seconds (not mentioning the ability to debug easily)
cd ./pkg/plugins/file
go test
Click to show the code snippet
---
title: "Bump Maven version"
sources:
  getLatestMavenVersion:
    kind: githubRelease
    name: Get the latest Maven version
    spec:
      owner: "apache"
      repository: "maven"
      token: "{{ requiredEnv .github.token }}"
      username: "{{ .github.username }}"
      versionFilter:
        kind: latest
    transformers:
      - trimPrefix: "maven-"

conditions:
  checkIfReleaseIsAvailable:
    kind: shell
    disablesourceinput: true
    spec:
      command: curl --connect-timeout 5 --location --head --fail --silent --show-error https://archive.apache.org/dist/maven/maven-3/{{ source `getLatestMavenVersion` }}/binaries/apache-maven-{{ source `getLatestMavenVersion` }}-bin.tar.gz
  checkIfMavenVersionIsDefined:
    kind: file
    disablesourceinput: true
    spec:
      file: ".auto.pkrvars.hcl"
      matchPattern: 'maven_(.*)=.*'

targets:
  updateMavenVersion:
    name: Update the Maven version in the Packer default values
    sourceID: getLatestMavenVersion
    kind: file
    spec:
      file: ".auto.pkrvars.hcl"
      matchPattern: 'maven_(.*)=.*'
      replacePattern: 'maven_$1= "{{ source `getLatestMavenVersion` }}"'
    scm:
      github:
        user: "{{ .github.user }}"
        email: "{{ .github.email }}"
        owner: "{{ .github.owner }}"
        repository: "{{ .github.repository }}"
        token: "{{ requiredEnv .github.token }}"
        username: "{{ .github.username }}"
        branch: "{{ .github.branch }}"

Additional Details

  • Related to the added Unit Test, the packages shell, scm and text are now providing a reusable Mock for some of their internals. This code is always stored within a file named mocks.go

@dduportal dduportal force-pushed the gh-231 branch 10 times, most recently from 79ecb77 to a796151 Compare October 22, 2021 22:15
@dduportal dduportal force-pushed the gh-231 branch 10 times, most recently from 20142e5 to 4444dae Compare October 28, 2021 19:38
@dduportal dduportal requested a review from olblak October 28, 2021 19:54
@dduportal dduportal added condition Modify condition resource enhancement New feature or request resource-file Resource of kind File source target Related to updatecli target labels Oct 28, 2021
@dduportal dduportal marked this pull request as ready for review October 28, 2021 19:55
Copy link
Member

@olblak olblak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @dduportal for the effort in this pull request, excepted minor suggestion, it seems ready to me. I let you rebase and then merge the PR once you considered my comments.

pkg/core/config/main.go Outdated Show resolved Hide resolved
pkg/plugins/file/condition.go Outdated Show resolved Hide resolved
pkg/plugins/file/condition.go Outdated Show resolved Hide resolved

// New returns a reference to a newly initialized File object from a Filespec
// or an error if the provided Filespec triggers a validation error.
func New(spec FileSpec) (*File, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to run all validation, collect every error in a list of errors and then exit an error if there is at least one error. The purpose is to detect every potential configuration issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It had been applied as we discussed: The method has a list of error strings, and they are returned one time for all.

pkg/plugins/file/target.go Outdated Show resolved Hide resolved
Signed-off-by: Damien Duportal <damien.duportal@gmail.com>
…a line.

Signed-off-by: Damien Duportal <damien.duportal@gmail.com>
…sage

Signed-off-by: Damien Duportal <damien.duportal@gmail.com>
@dduportal
Copy link
Contributor Author

Doc PR (wip): updatecli/website#161

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
condition Modify condition resource enhancement New feature or request resource-file Resource of kind File source target Related to updatecli target
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: support partial content for the resource "file"
2 participants