Class: Whenever::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/whenever/command_line.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CommandLine

Returns a new instance of CommandLine.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/whenever/command_line.rb', line 9

def initialize(options={})
  @options = options

  @options[:crontab_command] ||= 'crontab'
  @options[:file]            ||= 'config/schedule.rb'
  @options[:cut]             ||= 0
  @options[:identifier]      ||= default_identifier

  if !File.exist?(@options[:file]) && @options[:clear].nil?
    warn("[fail] Can't find file: #{@options[:file]}")
    exit(1)
  end

  if [@options[:update], @options[:write], @options[:clear]].compact.length > 1
    warn("[fail] Can only update, write or clear. Choose one.")
    exit(1)
  end

  unless @options[:cut].to_s =~ /[0-9]*/
    warn("[fail] Can't cut negative lines from the crontab #{options[:cut]}")
    exit(1)
  end
  @options[:cut] = @options[:cut].to_i

  @timestamp = Time.now.to_s
end

Class Method Details

.execute(options = {}) ⇒ Object



5
6
7
# File 'lib/whenever/command_line.rb', line 5

def self.execute(options={})
  new(options).run
end

Instance Method Details

#runObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/whenever/command_line.rb', line 36

def run
  if @options[:update] || @options[:clear]
    write_crontab(updated_crontab)
  elsif @options[:write]
    write_crontab(whenever_cron)
  else
    puts Whenever.cron(@options)
    puts "## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated."
    puts "## [message] Run `whenever --help' for more options."
    exit(0)
  end
end