From 63cf27c816ede9fbac51cf8f49fba1faf842d95b Mon Sep 17 00:00:00 2001 From: Geoff Williams Date: Wed, 15 Feb 2017 20:56:31 +0800 Subject: [PATCH] support for commenting out lines matching pattern --- .gitignore | 1 + README.md | 22 +++++++--- examples/{init.pp => comment.pp} | 6 ++- examples/uncomment.pp | 15 +++++++ manifests/init.pp | 42 +++++++++++++------ metadata.json | 3 +- spec/acceptance/{init.bats => comment.bats} | 6 +-- ...init__before.bats => comment__before.bats} | 0 .../{init__setup.sh => comment__setup.sh} | 0 spec/acceptance/uncomment.bats | 20 +++++++++ spec/acceptance/uncomment__before.bats | 8 ++++ spec/acceptance/uncomment__setup.sh | 8 ++++ 12 files changed, 106 insertions(+), 25 deletions(-) rename examples/{init.pp => comment.pp} (57%) create mode 100644 examples/uncomment.pp rename spec/acceptance/{init.bats => comment.bats} (77%) rename spec/acceptance/{init__before.bats => comment__before.bats} (100%) rename spec/acceptance/{init__setup.sh => comment__setup.sh} (100%) create mode 100644 spec/acceptance/uncomment.bats create mode 100644 spec/acceptance/uncomment__before.bats create mode 100644 spec/acceptance/uncomment__setup.sh diff --git a/.gitignore b/.gitignore index 1af5578..5da75d8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ Gemfile.lock Puppetfile.lock .librarian .tmp +/pkg diff --git a/README.md b/README.md index 2fc5907..cf5f9a4 100644 --- a/README.md +++ b/README.md @@ -11,17 +11,29 @@ ## Description -Comment out a line a file using Puppet (via `awk`). +Comment/uncomment a line a file using Puppet (via `awk`). ## Usage +### Commenting ```puppet -comment_line { "/foo/bar baz": - path => "/foo/bar", - match => "bar", +comment_line { "/foo/bar bar": + ensure => commented + path => "/foo/bar", + match => "bar", } ``` -Comment out all lines matching `/bar/` in the file `/foo/bar` +Comment all lines matching `/bar/` in the file `/foo/bar` + +### Uncommenting +```puppet +comment_line { "/foo/bar bam": + ensure => uncommented + path => "/foo/bar", + match => "bam", +} +``` +Uncomment all lines matching `/bam/` in the file `/foo/bar` ## Reference diff --git a/examples/init.pp b/examples/comment.pp similarity index 57% rename from examples/init.pp rename to examples/comment.pp index 648f22c..5fdfff8 100644 --- a/examples/init.pp +++ b/examples/comment.pp @@ -1,10 +1,12 @@ # @PDQTest comment_line { "/foo/bar baz": - path => "/foo/bar", - match => "bar", + ensure => commented, + path => "/foo/bar", + match => "bar", } comment_line { "/foo/bar bas": + ensure => commented, path => "/foo/bar", match => "bas", } diff --git a/examples/uncomment.pp b/examples/uncomment.pp new file mode 100644 index 0000000..16b8f96 --- /dev/null +++ b/examples/uncomment.pp @@ -0,0 +1,15 @@ +# @PDQTest + +# needs uncommenting +comment_line { "/foo/bar I went home": + ensure => uncommented, + path => "/foo/bar", + match => "I went home", +} + +# already uncommmented +comment_line { "/foo/bar I had a drink": + ensure => uncommented, + path => "/foo/bar", + match => "I had a drink", +} diff --git a/manifests/init.pp b/manifests/init.pp index a81863f..9510b35 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -8,25 +8,41 @@ # @param ensure Not used # @param comment_char Character to use for commenting out lines define comment_line( - $match, - $path, - $ensure = "present", - $comment_char = "#", + String $match, + String $path, + Enum["commented", "uncommented"] $ensure = "commented", + String $comment_char = "#", ) { $temp_file = "${path}.tmp" $replace_file = "> ${temp_file} && mv ${temp_file} ${path}" - $test_file = "bash -c \"[[ $(awk '/^[^#]+${match}/ {print}' ${path} | wc -l) -gt 0 ]]\"" + if $ensure == "commented" { + $test_file = "bash -c \"[[ $(awk '/^[^#]+${match}/ {print}' ${path} | wc -l) -gt 0 ]]\"" + + $change_file = + "awk '{ + if (match(\$0, /^[^${comment_char}].*${match}/)) { + print \"${comment_char}\" \$0 + } else { + print \$0 + } + }' ${path} ${replace_file}" + } else { + $test_file = "bash -c \"[[ $(awk '/^\\s*#.*${match}/ {print}' ${path} | wc -l) -gt 0 ]]\"" + + $change_file = + "awk '{ + if (match(\$0, /^\s*#.*${match}/)) { + gsub(/^\s*#\s*/,\"\", \$0) + print \$0 + } else { + print \$0 + } + }' ${path} ${replace_file}" + + } - $change_file = - "awk '{ - if (match(\$0, /^[^${comment_char}].*${match}/)) { - print \"${comment_char}\" \$0 - } else { - print \$0 - } - }' ${path} ${replace_file}" exec { "comment_line ${path} ${match}": command => $change_file, diff --git a/metadata.json b/metadata.json index 2f9134b..3d3051c 100644 --- a/metadata.json +++ b/metadata.json @@ -1,6 +1,6 @@ { "name": "geoffwilliams/comment_line", - "version": "0.1.0", + "version": "0.2.0", "author": "geoffwilliams", "summary": "Comment out a line a file using Puppet", "license": "Apache-2.0", @@ -12,4 +12,3 @@ ], "data_provider": null } - diff --git a/spec/acceptance/init.bats b/spec/acceptance/comment.bats similarity index 77% rename from spec/acceptance/init.bats rename to spec/acceptance/comment.bats index e417ac3..d90313a 100644 --- a/spec/acceptance/init.bats +++ b/spec/acceptance/comment.bats @@ -8,13 +8,13 @@ } @test "bas commented" { - grep '#I programmed in basic' /foo/bar + grep '^#I programmed in basic' /foo/bar } @test "line left alone 1" { - grep 'I had a drink' /foo/bar + grep '^I had a drink' /foo/bar } @test "line left alone 1" { - grep 'I went home' /foo/bar + grep '^I went home' /foo/bar } diff --git a/spec/acceptance/init__before.bats b/spec/acceptance/comment__before.bats similarity index 100% rename from spec/acceptance/init__before.bats rename to spec/acceptance/comment__before.bats diff --git a/spec/acceptance/init__setup.sh b/spec/acceptance/comment__setup.sh similarity index 100% rename from spec/acceptance/init__setup.sh rename to spec/acceptance/comment__setup.sh diff --git a/spec/acceptance/uncomment.bats b/spec/acceptance/uncomment.bats new file mode 100644 index 0000000..16995e6 --- /dev/null +++ b/spec/acceptance/uncomment.bats @@ -0,0 +1,20 @@ +# BATS test file to run after executing 'examples/init.pp' with puppet. +# +# For more info on BATS see https://github.com/sstephenson/bats + +# Tests are really easy! just the exit status of running a command... +@test "bar left alone" { + grep '^I went to the bar' /foo/bar +} + +@test "bas left alone" { + grep '^I programmed in basic' /foo/bar +} + +@test "drink left alone" { + grep '^I had a drink' /foo/bar +} + +@test "line uncommented" { + grep '^I went home' /foo/bar +} diff --git a/spec/acceptance/uncomment__before.bats b/spec/acceptance/uncomment__before.bats new file mode 100644 index 0000000..b429129 --- /dev/null +++ b/spec/acceptance/uncomment__before.bats @@ -0,0 +1,8 @@ +# BATS test file to run before executing 'examples/init.pp' with puppet. +# +# For more info on BATS see https://github.com/sstephenson/bats + +# Tests are really easy! just the exit status of running a command... +@test "testcase installed" { + grep '# I went home' /foo/bar +} diff --git a/spec/acceptance/uncomment__setup.sh b/spec/acceptance/uncomment__setup.sh new file mode 100644 index 0000000..f008154 --- /dev/null +++ b/spec/acceptance/uncomment__setup.sh @@ -0,0 +1,8 @@ +#!/bin/bash +mkdir /foo/ -p +cat > /foo/bar <