From 028e7f977ec7f9687f43e5a617e925d9735198ad Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Tue, 6 Oct 2015 13:56:29 +0200 Subject: [PATCH] filter comments in /etc/group --- lib/resources/etc_group.rb | 29 ++++++++++++++++++++++------- test/unit/mock/files/etcgroup | 1 + 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/resources/etc_group.rb b/lib/resources/etc_group.rb index 2769f6bac..3673aec6e 100644 --- a/lib/resources/etc_group.rb +++ b/lib/resources/etc_group.rb @@ -6,9 +6,9 @@ # The file format consists of # - group name -# - password -# - gid -# - group list, comma seperated list +# - password - group's encrypted password +# - gid - group's decimal ID +# - member list - group members, comma seperated list # # Usage: # describe etc_group do @@ -21,13 +21,17 @@ # its('users') { should include 'my_user' } # end +require 'utils/parser' + class EtcGroup < Vulcano.resource(1) + include ContentParser + name 'etc_group' attr_accessor :gid, :entries def initialize(path = nil) @path = path || '/etc/group' - @entries = parse(@path) + @entries = parse_group(@path) end def to_s @@ -77,12 +81,23 @@ class EtcGroup < Vulcano.resource(1) private - def parse(path) + def parse_group(path) @content = vulcano.file(path).content - @content.split("\n").map do |line| - line.split(':') + # iterate over each line and filter comments + @content.split("\n").each_with_object([]) do |line, lines| + grp_info = parse_group_line(line) + lines.push(grp_info) if !grp_info.nil? && grp_info.size > 0 end end + + def parse_group_line(line) + opts = { + comment_char: '#', + standalone_comments: false, + } + line, _idx_nl = parse_comment_line(line, opts) + line.split(':') + end end # object that hold a specifc view on etc group diff --git a/test/unit/mock/files/etcgroup b/test/unit/mock/files/etcgroup index e7388fd4d..431b3bdfc 100644 --- a/test/unit/mock/files/etcgroup +++ b/test/unit/mock/files/etcgroup @@ -1,2 +1,3 @@ +# comment root:x:0: www-data:x:33:www-data,root