cookbook 'xmledit', '~> 4.0.0'
xmledit
(20) Versions
4.0.0
-
Follow1
Provides a resource and provider for incremental editing of XML files
cookbook 'xmledit', '~> 4.0.0', :supermarket
knife supermarket install xmledit
knife supermarket download xmledit
xmledit-cookbook
This cookbook allows incremental editing of XML files. You must include the
default recipe to ensure nokogiri and its dependencies are installed using the
upstream community xml cookbook.
Here be dragons. This is definitely not idempotent, and is not the best way to manage a file in chef. clintoncwolfe has described why you should not use this. Please read his thoughts on the matter.
Specifically, this library cookbooks offers a resource and provider that can be
called by the name xml_edit
. This resource accepts XPath expressions and will
perform the expected action on the very first node found.
Important notes
This cookbook supports chef 12.x and greater. For working Chef 11.x support,
pin back to xmledit < 3.0.0.If no XML nodes match the XPath expression, no actions will be performed. If a
file cannot be found, this provider will raise an error by virtue of::File
raising an error.This cookbook attempts to sidestep resource cloning issues by forcing the
resource name to be something other than a path to a file to edit. This allows
you to perform multiple edits to the same file by giving the resources unique
names and providing the same path attribute value. You may also do a bulk edit,
if you know in advance all the edits that should be happening to a file.Including an XPath expression that is a match against the node's value that
you want to replace (e.g. matching against[text=()=\'false\']
if you want to
replace false with true) means that the underlying file resource will no longer
be defined, vs. having a file resource that was defined but idempotently 'up to
date'. Both styles are possible using this cookbook, depending on the XPath
expressions you provide.
Supported actions
name | description |
---|---|
replace | replaces a target node (and all children) with a new fragment |
append | appends a new XML fragment to a parent target node |
append_if_missing | if target is found, replace with fragment, otherwise append fragment |
remove | remove gets rid of a target node |
bulk | pass a hash of the above actions, do them all and write file out once after |
Supported attributes
These have no default values, and will cause a no-op if they are not specified or don't match a valid file or target.
name | description |
---|---|
path | path to the XML file to edit |
target | an xpath expression for the target node (see action replace and append_if_missing above) |
parent | an xpath expression for the parent of the target node (see actions append and append_if_missing above) |
fragment | a string that will be parsed into an XML fragment, for actions that add or update |
edits | an array of hashes, with each hash containing one or zero keys of: :action , :path , :targent , :parent , or :fragment
|
prepend |
true or false ; If the action is appending, prepend to the front (parent.prepend_chidl) vs. the end (parent.add_child) |
Checkout nokogiri's documentation on XPath and search to understand the syntax behind target
and parent
attributes.
Examples
Replace false
with true
in a file at /etc/application.xml
containing <config><foo>false</foo></config>
.
xml_edit 'set foo to true' do
path '/etc/application.xml'
target '/config/foo[text()=\'false\']'
fragment 'true '
action :replace
end
xml_edit '/tmp/xmledit_bulk_test.xml' do
edits [
{action: :replace, target: '/foo/bar', fragment: 'xyzzy '},
{action: :append, parent: '/foo', target: '/foo/baz', fragment: 'true '},
{action: :remove, target: '/foo/hideme'},
{action: :append_if_missing, parent: '/foo', target: '/foo/showme', fragment: ' '}
]
action :bulk
end
TODO
- Add an attribute for forcing an error when an xpath expr doesn't match
License
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Dependent cookbooks
This cookbook has no specified dependencies.
Contingent cookbooks
4.0.0
- Drop dependency on nokogiri (#21)
3.0.3
- Add ability to prepend when doing an append, vs. adding at the end
3.0.2
- Tweak name of matcher
3.0.1
- Add a bulk edit matcher
3.0.0
- Start using
provides
, breaks Chef 11 compat (#11) - Update CentOS testing from 6.6 to 6.7
- Update Berkshelf pin to ~> 4
- New rubocop complaints addressed
2.0.0
- Remove poise dependency, bump to v2.0.0 (#10)
1.2.4
- Close all file handles (#11)
1.2.3
- Ensure we pin to latest poise, so Berkshelf does not use xmledit with older poise
1.2.2
- Make compatible with poise 2.0 by adding
require 'poise'
to each resource and provider class.
1.2.1
- Remove comment that was preventing updates ot the file, fixes #9.
1.2.0
- New feature: bulk action with associated
edits
attribute. Pass an array of hashes that describe each edit, using the same parameters as the other actions. See README.md for an example.
1.1.0
- Load namespace in Nokogiri calls (#7)
1.0.7
- Normalize the output of modified XML. Nokogiri seems to be better at parsing files into normalized XML than just writing it out, so we added a very unfortunate temporary file workaround.
1.0.6
- Remove metadata that breaks chef 11 (source and issue url metadata, specifically)
- Add more unit tests and integration tests
- Update logic so that xml_edit resource truly reflects the state of the file resource being updated
1.0.5
- Do not parse a fragment or try to pass :remove action the fragment
1.0.1
- Add the ability to replace or append (action append_if_missing)
- Attempt to strip/format whitespace better when editing
1.0.0
- Rename actions to map better to nokogiri operations
- Implement remove and append actions
- Add missing xml cookbook dependency
0.1.0
- Initial release.
Collaborator Number Metric
4.0.0 failed this metric
Failure: Cookbook has 0 collaborators. A cookbook must have at least 2 collaborators to pass this metric.
Contributing File Metric
4.0.0 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must contain a CONTRIBUTING.md file
Foodcritic Metric
4.0.0 failed this metric
FC085: Resource using new_resource.updated_by_last_action to converge resource: xmledit/libraries/provider.rb:169
Run with Foodcritic Version 16.3.0 with tags metadata,correctness ~FC031 ~FC045 and failure tags any
No Binaries Metric
4.0.0 passed this metric
Testing File Metric
4.0.0 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must contain a TESTING.md file
Version Tag Metric
4.0.0 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must include a tag that matches this cookbook version number
4.0.0 failed this metric
4.0.0 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must contain a CONTRIBUTING.md file
Foodcritic Metric
4.0.0 failed this metric
FC085: Resource using new_resource.updated_by_last_action to converge resource: xmledit/libraries/provider.rb:169
Run with Foodcritic Version 16.3.0 with tags metadata,correctness ~FC031 ~FC045 and failure tags any
No Binaries Metric
4.0.0 passed this metric
Testing File Metric
4.0.0 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must contain a TESTING.md file
Version Tag Metric
4.0.0 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must include a tag that matches this cookbook version number
4.0.0 failed this metric
Run with Foodcritic Version 16.3.0 with tags metadata,correctness ~FC031 ~FC045 and failure tags any
4.0.0 passed this metric
Testing File Metric
4.0.0 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must contain a TESTING.md file
Version Tag Metric
4.0.0 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must include a tag that matches this cookbook version number
4.0.0 failed this metric
4.0.0 failed this metric
Failure: To pass this metric, your cookbook metadata must include a source url, the source url must be in the form of https://github.com/user/repo, and your repo must include a tag that matches this cookbook version number