Class: Winever::CommandLine

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CommandLine

Returns a new instance of CommandLine.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/winever/command_line.rb', line 38

def initialize options={}
  @options = options

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

  unless File.exists?(@options[:file])
    warn("[fail] Can't find file: #{@options[:file]}")
    exit(1)
  end

  if [@options[:update], @options[:clear]].compact.length > 1
    warn("[fail] Can only update 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
end

Class Method Details

.execute(options = {}) ⇒ Object



34
35
36
# File 'lib/winever/command_line.rb', line 34

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

.run_from_command_line_optionsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/winever/command_line.rb', line 4

def self.run_from_command_line_options
  require 'optparse'

  options = {}

  OptionParser.new do |opts|
    opts.banner = "Usage: whenever [options]"
    opts.on('-i', '--update [identifier]', 'Default: full path to schedule.rb file') do |identifier|
      options[:update] = true
      options[:identifier] = identifier if identifier
    end
    opts.on('-c', '--clear [identifier]') do |identifier|
      options[:clear] = true
      options[:identifier] = identifier if identifier
    end
    opts.on('-s', '--set [variables]', 'Example: --set \'environment=staging&path=/my/sweet/path\'') do |set|
      options[:set] = set if set
    end
    opts.on('-f', '--load-file [schedule file]', 'Default: config/schedule.rb') do |file|
      options[:file] = file if file
    end
    opts.on('-k', '--cut [lines]', 'Cut lines from the top of the cronfile') do |lines|
      options[:cut] = lines.to_i if lines
    end
    opts.on('-v', '--version') { puts "Winever v#{Winever::VERSION}"; exit(0) }
  end.parse!

  self.execute(options)
end

Instance Method Details

#default_identifierObject

protected



78
79
80
# File 'lib/winever/command_line.rb', line 78

def default_identifier
  File.expand_path(@options[:file])
end

#runObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/winever/command_line.rb', line 63

def run
  if @options[:update]
    Winever::TaskManager.update_tasks(@options)
  elsif @options[:clear]
    Winever::TaskManager.clear_tasks(@options)
  else
    puts Winever::WheneverInterface.cron(@options)
    puts "## [message] Above is your schedule file converted to cron-winever syntax; your crontab  file /scheduled tasks were not updated."
    puts "## [message] Run `winever --help' for more options."
    exit(0)
  end
end