Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Modify a portion of a file if it fits a certain format, fail if not

I have to update an application file i dont own, meaning it could get changed in an upgrade.

What I would like to do is run a check that it first fits the format I expect. If it doesnt, that likely means the file underwent major changes and I need to review what happened and not update the file. If it does match the pattern, then make sure my value is as expected.

Here is the snippet Im working with (ERB file)

<os>
<type arch='<%= arch %>'><%= os_type %></type>
<% boot_order.each do |dev| -%>
<boot dev='<%= dev %>'/>
<% end -%>
</os>

Basically, if everything in the OS tag looks good, I then want to make sure my value is there, making the final form look like this

<os>
<type arch='<%= arch %>'><%= os_type %></type>
<% boot_order.each do |dev| -%>
<boot dev='<%= dev %>'/>
<% end -%>
<bios useserial='yes' rebootTimeout='5000'/>
</os>

I have a regex that will match to verify that the OS block is there as expected (with or without my edit)

^\s*<os>\n?\s*<type arch='<%= arch %>'><%= os_type %></type>\n?\s*<% boot_order.each do \|dev\| -%>\n?\s*<boot dev='<%= dev %>'/>\n?\s*<% end -%>\n?(.+\n)?\s*</os>

From reading it sounds like maybe I can use the line/match attributes to get this done. If the match is true (valid format, value may or may not be there) and then have it inject my value (replacing the whole block, every time its run.) I suspect this will work ok, but its probably horrible in the sense that it would update the file on every run.

The other thought I had was to use the -> (still pretty new so might be way off base)

file { is it there? } -> file_line { valid format? } -> file_line {valid value?}