Skip to content

Commit

Permalink
support for commenting out lines matching pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoff Williams committed Feb 15, 2017
1 parent ac2a065 commit 63cf27c
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ Gemfile.lock
Puppetfile.lock
.librarian
.tmp
/pkg
22 changes: 17 additions & 5 deletions README.md
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions examples/init.pp → 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",
}
15 changes: 15 additions & 0 deletions 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",
}
42 changes: 29 additions & 13 deletions manifests/init.pp
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions 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",
Expand All @@ -12,4 +12,3 @@
],
"data_provider": null
}

6 changes: 3 additions & 3 deletions spec/acceptance/init.bats → spec/acceptance/comment.bats
Expand Up @@ -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
}
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions 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
}
8 changes: 8 additions & 0 deletions 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
}
8 changes: 8 additions & 0 deletions spec/acceptance/uncomment__setup.sh
@@ -0,0 +1,8 @@
#!/bin/bash
mkdir /foo/ -p
cat > /foo/bar <<END
I went to the bar
I had a drink
# I went home
I programmed in basic
END

0 comments on commit 63cf27c

Please sign in to comment.