Adoptable Cookbooks List

Looking for a cookbook to adopt? You can now see a list of cookbooks available for adoption!
List of Adoptable Cookbooks

Supermarket Belongs to the Community

Supermarket belongs to the community. While Chef has the responsibility to keep it running and be stewards of its functionality, what it does and how it works is driven by the community. The chef/supermarket repository will continue to be where development of the Supermarket application takes place. Come be part of shaping the direction of Supermarket by opening issues and pull requests or by joining us on the Chef Mailing List.

Select Badges

Select Supported Platforms

RSS

line (39) Versions 2.1.1

Provides line editing resources for use by recipes

Berkshelf
Policyfile
Knife
cookbook 'line', '~> 2.1.1'
cookbook 'line', '~> 2.1.1', :supermarket
knife supermarket install line
knife supermarket download line
README
Dependencies
Changelog
Quality 100%

line cookbook

Build Status

Motivation

Quite often, the need arises to do line editing instead of managing an entire file with a template resource. This cookbook supplies various resources that will help you do this.

Limitations

  • The line resources processes the entire target file in memory. Trying to edit large files may fail.
  • The end of line processing was only tested using \n and \r\n. Using other line endings very well may not work.
  • The end of line string used needs to match the actual end of line used in the file \n and \r\n are used as the defaults but if they don't match the actual end of line used in the file the results will be weird.
  • Adding a line implies there is a separator on the previous line. Adding a line differs from appending characters.
  • Lines to be added should not contain EOL characters. The providers do not do multiline regex checks.
  • Missing file processing is the way it is by intention

    • add_to_list do nothing, list not found so there is nothing to add to.
    • append_if_no_line create file, add the line.
    • delete_from_list do nothing, the list was not found which implies there is nothing to delete
    • delete_lines do nothing, the line isn't there which implies there is nothing to delete
    • replace_or_add create file, add the line
  • Chef client version 13 or greater is expected.

Usage

Add "depends 'line'" to your cookbook's metadata.rb to gain access to the resources.

append_if_no_line "make sure a line is in some file" do
  path "/some/file"
  line "HI THERE I AM STRING"
end

replace_or_add "why hello" do
  path "/some/file"
  pattern "Why hello there.*"
  line "Why hello there, you beautiful person, you."
end

delete_lines "remove hash-comments from /some/file" do
  path "/some/file"
  pattern "^#.*"
end

delete_lines "remove hash-comments from /some/file with a regexp" do
  path "/some/file"
  pattern /^#.*/
end

replace_or_add "change the love, don't add more" do
  path "/some/file"
  pattern "Why hello there.*"
  line "Why hello there, you beautiful person, you."
  replace_only true
end

add_to_list "add entry to a list" do
  path "/some/file"
  pattern "People to call: "
  delim [","]
  entry "Bobby"
end

delete_from_list "delete entry from a list" do
  path "/some/file"
  pattern "People to call: "
  delim [","]
  entry "Bobby"
end

delete_lines 'remove from nonexisting' do
  path '/tmp/doesnotexist'
  pattern /^#/
  ignore_missing true
end


filter_lines 'Shift lines to at least 8 leading spaces' do
  path '/some/file'
  filter proc { |current| current.map(|line| line =~ /^ {8}/ ? line : "       #{line}") }
end

insert_lines = %w(line1 line2 line3)
match_pattern = /^COMMENT ME|^HELLO/
filter_lines 'Insert lines after match' do
  path '/some/file'
  filter after: [match_pattern, insert_lines]
end

filter_lines 'Built in example filters' do
  path '/tmp/multiple_filters'
  sensitive false
  filters(
    [
    # insert lines after the last match
      { after:  [match_pattern, insert_lines, :last] },
    ]
  )
end

Resource Notes

The resources implemented are:

append_if_no_line - Add a missing line
replace_or_add    - Replace a line that matches a pattern or add a missing line
delete_lines      - Delete an item from a list
add_to_list       - Add an item to a list
delete_from_list  - Delete lines that match a pattern
filter_lines      - Supply a proc or use a sample filter
  Sample filters:
    after         - Insert lines before a match

Resource: append_if_no_line

Actions

Action Description
edit Append a line if it is missing.

Properties

Properties Description Type Values and Default
path File to update String Required, no default
line Line contents String Required, no default
ignore_missing Don't fail if the file is missing true or false Default is true
eol Alternate line end characters String default \n on unix, \r\n on windows
backup Backup before changing Boolean default false

Notes

This resource is intended to match the whole line exactly. That means if the file contains this is my line (trailing whitespace) and you've specified line "this is my line", another line will be added. You may want to use replace_or_add instead, depending on your use case.

Resource: replace_or_add

Actions

Action Description
edit Replace lines that match the pattern. Append the line unless a source line matches the pattern.

Properties

Properties Description Type Values and Default
path File to update String Required, no default
pattern Regular expression to select lines Regular expression or String Required, no default
line Line contents String Required, no default
replace_only Don't append only replace matching lines true or false Required, no default
ignore_missing Don't fail if the file is missing true or false Default is true
eol Alternate line end characters String default \n on unix, \r\n on windows
backup Backup before changing Boolean default false

Resource: delete_lines

Actions

Action Description
edit Delete lines that match the pattern.

Properties

Properties Description Type Values and Default
path File to update String Required, no default
pattern Regular expression to select lines Regular expression or String Required, no default
ignore_missing Don't fail if the file is missing true or false Default is true
eol Alternate line end characters String default \n on unix, \r\n on windows
backup Backup before changing Boolean default false

Notes

Removes lines based on a string or regex.

Resource: add_to_list

Actions

Action Description
edit Add an item to a list

Properties

Properties Description Type Values and Default
path File to update String Required, no default
pattern Regular expression to select lines Regular expression or String Required, no default
delim Delimiter entries Array Array of 1, 2 or 3 multi-character elements
entry Value to add String Required, No default
ends_with List ending String No default
ignore_missing Don't fail if the file is missing true or false Default is true
eol Alternate line end characters String default \n on unix, \r\n on windows
backup Backup before changing Boolean default false

Notes

If one delimiter is given, it is assumed that either the delimiter or the given search pattern will proceed each entry and each entry will be followed by either the delimiter or a new line character: People to call: Joe, Bobby delim [","] entry 'Karen' People to call: Joe, Bobby, Karen

If two delimiters are given, the first is used as the list element delimiter and the second as entry delimiters: my @net1918 = ("10.0.0.0/8", "172.16.0.0/12"); delim [", ", "\""] entry "192.168.0.0/16" my @net1918 = ("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16");

if three delimiters are given, the first is used as the list element delimiter, the second as the leading entry delimiter and the third as the trailing delimiter: multi = ([310], [818]) delim [", ", "[", "]"] entry "425" multi = ([310], [818], [425])

ends_with is an optional property. If specified a list is expected to end with the given string.

Resource: delete_from_list

Actions

Action Description
edit Delete an item from a list

Properties

Properties Description Type Values and Default
path File to update String Required, no default
pattern Regular expression to select lines Regular expression or String Required, no default
delim Delimiter entries Array Array of 1, 2 or 3 multi-character elements
entry Value to delete String Required, No default
ends_with List ending String No default
ignore_missing Don't fail if the file is missing true or false Default is true
eol Alternate line end characters String default \n on unix, \r\n on windows
backup Backup before changing Boolean default false

Notes

Delimiters works exactly the same way as add_to_list, see above.

Resource: filter_lines

Actions

Action Description
edit Use a proc

Properties

Properties Description Type Values and Default
path String Path to file Required, no default
filters Array of filters, Proc, Method See the filter grammar Required, no default
ignore_missing Don't fail if the file is missing true or false Default is true
eol Alternate line end characters String default \n on unix, \r\n on windows
backup Backup before changing Boolean default false

Notes

The filter_lines resource passes the contents of the path file in an array of lines to a Proc or Method filter. The filter should return an array of lines. The output array will be written to the file or passed to the next filter. The built in filters are usable examples of what can be done with a filter, please write your own when you have specific needs. The built in filters all take an array of positional arguments.

Filter Grammar


filters ::= filter | [<filter>, ...]
filter  ::= <code> | { <code> => <args>  }
args    ::= <String> | <Array>
code    ::= <Symbol> | <Method> | <Proc>
Symbol  ::= :after | :before | :between | :comment | :replace | :stanza | :substitute
            Symbols are translated to methods in Line::Filter
Method  ::= A reference to a method that has a signature of method(current lines is Array, args is Array)
            and that  returns an array
Proc    ::= A reference to a proc that has a signature of proc(current lines is Array, args is Array)
            and returns an array

Filters

Built in Filter Description Arguments arg1 arg2 arg3
:after Insert lines after a matching line Pattern to match String or Array of lines to insert :each, :first, or :last to select the matching lines

Author

  • Contributor: Mark Gibbons
  • Contributor: Dan Webb
  • Contributor: Sean OMeara
  • Contributor: Antek S. Baranski

Dependent cookbooks

This cookbook has no specified dependencies.

Contingent cookbooks

al_agents Applicable Versions
autofs Applicable Versions
aws-kinesis-agent Applicable Versions
bash_history_timestamp Applicable Versions
chamber-kibana Applicable Versions
chefdk_bootstrap Applicable Versions
cis-el7-l1-hardening Applicable Versions
codenamephp_chef Applicable Versions
dynatrace Applicable Versions
dynatrace-appmon Applicable Versions
elk Applicable Versions
elkstack Applicable Versions
fipsify Applicable Versions
fqdn Applicable Versions
hdp-cloud Applicable Versions
jenkinsstack Applicable Versions
linux-tweak Applicable Versions
nfs Applicable Versions
nyan-cat Applicable Versions
ocserv Applicable Versions
openvpnas Applicable Versions
ps1 Applicable Versions
puncha-kibana Applicable Versions
r1337-linux-users Applicable Versions
rc Applicable Versions
zf2 Applicable Versions

line Cookbook CHANGELOG

v2.1.1 (2018-10-08)

  • Allow the backup option to be specified as true

v2.1.0 (2018-09-28)

  • Add the filter_lines resource
  • Add the after filter method to allow lines to be inserted after matching a line

v2.0.2 (2018-06-29)

  • Explicitly disallow embedded EOL characters in replacement and append lines

v2.0.1 (2018-06-01)

  • Tested on chef 12.13.37. Fix error caused by using the sensitive attribute. Sensitive true will always be used for chef 12.

v2.0.0 (2018-05-19)

  • Breaking change - Files are processed in memory instead of line by line. It's possible that large files that were previously updated by the line cookbook will not be able to be processed.
  • Breaking change - Drop Chef 12 support
  • Use template files instead of cookbook_file so that we get platform sensitive line endings written for testing.
  • Add windows support to add_to_list, append_if_no_line, delete_from_list, delete_lines, replace_or_add.
  • Make the processing of missing target files consistent. Add the ignore_missing property to the resources to allow a missing file to raise an error.
  • Clean up the order of some boiler plate code.
  • Create helper methods for some common resource functions.
  • Drop the OS helpers in favour os using platform_family?.

v1.2.0 (2018-04-18)

  • Add the ignore_missing option to the delete_lines and delete_from_list. Don't raise an error if the target file is missing.

v1.1.1 (2018-04-16)

  • Allow appending to an empty file.

v1.1.0 (2018-03-26)

  • Rework delete_lines to use file provider sub-resource.
  • Support matching with regexps in addition to strings with delete_lines.
  • Rework append_if_no_line to use file provider sub-resource.
  • Fix edge conditions around files-with-no-trailing-CR being fed to append_if_no_line.
  • Remove library helpers.
  • Remove the escape_regexp and escape_string methods in favour of native Regexp.escape

v1.0.6 (2018-03-23)

  • Add question mark to regular expression escape characters

v1.0.5 (2018-02-20)

  • Minor Testing updates
  • Remove custom matchers for ChefSpec. ChefDK 1 versions of ChefSpec will no longer work when unit testing against this cookbook.

v1.0.4 (2018-01-10)

  • Handle deleting items from a list using spaces as the delimeter

v1.0.3 (2017-08-22)

  • Add edge case tests for add_to_list
  • Handle the delete_lines, add_to_list, and delete_from_list resources when a missing file is specified.

v1.0.2 (2017-07-07)

  • Fix #58 Add resource locator matchers
  • Fix #59 Add resource matcher tests
  • Make cookstyle 2.0.0 fixes
  • Delete the unused minitest files
  • Clean up the file_ext inspec resource

v1.0.1 (2017-07-05)

  • Fix #53 append_if_no_line appends line always appends

v1.0.0 (2017-06-13)

  • Move cookbook to Sous-Chefs org
  • Move to using custom resources

v0.6.3 (2015-10-27)

  • Fixing Ruby and Chef deprecation warnings
  • Cleaning up tests a bit
  • Adding support for source_url and issues_url
  • delete_from_list resource

v0.6.2 (2015-07-15)

  • Catch lines missed by strict patterns
  • Add rspec tests for the replace_or_add provider. The existing chefspec tests don't step into the provider code and so don't check the provider functionality.
  • Change the Gemfile to reflect the need for berkshelf 3, chefspec v4.2, rspec 3 for the tests.
  • Update provider_replace_or_add to handle cases where the pattern does not match the replacement line.
  • Fix notification problem where updated_by_last_action was set when nothing changed.

v0.6.1 (2015-02-24)

  • Adding CHANGELOG
  • Adding ChefSpec matchers

Collaborator Number Metric
            

2.1.1 passed this metric

Contributing File Metric
            

2.1.1 passed this metric

Foodcritic Metric
            

2.1.1 passed this metric

No Binaries Metric
            

2.1.1 passed this metric

Publish Metric
            

2.1.1 passed this metric

Supported Platforms Metric
            

2.1.1 passed this metric

Testing File Metric
            

2.1.1 passed this metric

Version Tag Metric
            

2.1.1 passed this metric