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

File modifications #47

Open
ewen-lbh opened this issue Jul 22, 2021 · 0 comments
Open

File modifications #47

ewen-lbh opened this issue Jul 22, 2021 · 0 comments
Assignees
Labels
manifest Involves modification of the Manifest struct
Milestone

Comments

@ewen-lbh
Copy link
Owner

ewen-lbh commented Jul 22, 2021

An array of directive objects called modifications, which can be variants/components-overriden.

Available "directives":

  • in (path, mandatory) which file to modify. Modifications are run on files that have been copied
  • after (regex | string) run the modification only on the part of the file after the specified line
  • before (regex | string) run the modification only on the part of the file before the specified line
  • append (string) append content to the file.
    • if before is specified, put the line just above the one matched by before
    • if after is specified, put the line just below the one matched by after
    • else, put the line at the end of the file
  • prepend (string) like append, but the roles of before/after are reversed.
  • replace (string | regex) search a string to be replaced with something
  • with (string, mandatory if replace used) Replace content matched by replace with this. If replace is a regex, $n will refer to the n-th capture group

Specifying a regex instead of a string is done via the custom YAML tag !regex.

This requires switching to gopkg.in/yaml.v3 and use a special struct:

type stringOrRegexp struct {
    yaml.Node
}

func (sor stringOrRegexp) isRegexp() bool {
    return sor.Tag == "!regex"
}

func (sor stringOrRegexp) isString() bool {
    return sor.Tag == "!!str"
}

func (sor stringOrRegexp) regexp() (*regexp.Regexp, error) {
    return regexp.Compile(sor.Value)
}

func (sor stringOrRegexp) string() string {
    return fmt.Sprint(sor.Value)
}

tested in Go playground, yaml won't unmarshal to stringOrRegexp even if it embeds yaml.Node, instead:

type stringOrRegexp = yaml.Node

func isRegexp(sor stringOrRegexp) bool {
    return sor.Tag == "!regex"
}

func isString(sor stringOrRegexp) bool {
    return sor.Tag == "!!str"
}

And in the manifest:

type Modification struct {
    In string
    Before stringOrRegexp
    After stringOrRegexp
    Replace stringOrRegexp
    With string
    Append string
    Preprend string
}

type Manifest struct {
    ...
    Modifications []Modification
    ...
}

func (m Modification) searchAndReplace() {
    var searchPattern *regexp.Regexp
    if isRegexp(m.Replace) {
        searchPattern, err := regexp.Compile(m.Replace.Value)
    } else if isString(m.Replace) {
        searchPattern, err := regexp.Compile(regexp.Escape(m.Replace.Value)) # ^1
    } else {
        return // no replacement to do, `replace` field not mentionned
    }
    if err != nil {
        ...
    }
    ...
}
@ewen-lbh ewen-lbh self-assigned this Jul 22, 2021
@ewen-lbh ewen-lbh added this to the 1.1.0 milestone Jul 22, 2021
@ewen-lbh ewen-lbh added the manifest Involves modification of the Manifest struct label Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
manifest Involves modification of the Manifest struct
Projects
None yet
Development

No branches or pull requests

1 participant