Class: Inspec::Resources::Crontab

Inherits:
Object
  • Object
show all
Includes:
CommentParser
Defined in:
lib/resources/crontab.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommentParser

#parse_comment_line

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

#paramsObject (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_cmdObject



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_crontabObject



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_sObject



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