Class: Inspec::Resources::Crontab
- Inherits:
-
Object
- Object
- Inspec::Resources::Crontab
- Includes:
- CommentParser
- Defined in:
- lib/resources/crontab.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #crontab_cmd ⇒ Object
-
#initialize(opts = nil) ⇒ Crontab
constructor
A new instance of Crontab.
- #parse_crontab_line(l) ⇒ Object
- #read_crontab ⇒ Object
- #to_s ⇒ Object
Methods included from CommentParser
Constructor Details
#initialize(opts = nil) ⇒ Crontab
Returns a new instance of Crontab.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/resources/crontab.rb', line 38 def initialize(opts = nil) if opts.respond_to?(:fetch) Hash[opts.map { |k, v| [k.to_sym, v] }] @user = opts.fetch(:user, nil) @path = opts.fetch(:path, nil) raise Inspec::Exceptions::ResourceFailed, 'A user or path must be supplied.' if @user.nil? && @path.nil? else @user = opts @path = nil end @params = read_crontab end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
34 35 36 |
# File 'lib/resources/crontab.rb', line 34 def params @params end |
Instance Method Details
#crontab_cmd ⇒ Object
63 64 65 |
# File 'lib/resources/crontab.rb', line 63 def crontab_cmd @user.nil? ? 'crontab -l' : "crontab -l -u #{@user}" end |
#parse_crontab_line(l) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/resources/crontab.rb', line 56 def parse_crontab_line(l) data, = parse_comment_line(l, comment_char: '#', standalone_comments: false) return nil if data.nil? || data.empty? is_system_crontab? ? parse_system_crontab(data) : parse_user_crontab(data) end |
#read_crontab ⇒ Object
51 52 53 54 |
# File 'lib/resources/crontab.rb', line 51 def read_crontab ct = is_system_crontab? ? inspec.file(@path).content : inspec.command(crontab_cmd).stdout ct.lines.map { |l| parse_crontab_line(l) }.compact end |
#to_s ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/resources/crontab.rb', line 85 def to_s if is_system_crontab? "crontab for path #{@path}" elsif is_user_crontab? "crontab for user #{@user}" else 'crontab for current user' end end |