Added --cut option to the command line to allow pruning of the crontab
This commit is contained in:
parent
f17c8d8cd2
commit
3e20f936eb
@ -10,6 +10,9 @@ OptionParser.new do |opts|
|
|||||||
opts.banner = "Usage: whenever [options]"
|
opts.banner = "Usage: whenever [options]"
|
||||||
opts.on('-v', '--version') { puts "Whenever v#{Whenever::VERSION}"; exit(0) }
|
opts.on('-v', '--version') { puts "Whenever v#{Whenever::VERSION}"; exit(0) }
|
||||||
opts.on('-w', '--write-crontab') { options[:write] = true }
|
opts.on('-w', '--write-crontab') { options[:write] = true }
|
||||||
|
opts.on('-c', '--cut [lines]', 'Cut lines from the top of the cronfile') do |lines|
|
||||||
|
options[:cut] = lines.to_i if lines
|
||||||
|
end
|
||||||
opts.on('-i', '--update-crontab [identifier]', 'Default: full path to schedule.rb file') do |identifier|
|
opts.on('-i', '--update-crontab [identifier]', 'Default: full path to schedule.rb file') do |identifier|
|
||||||
options[:update] = true
|
options[:update] = true
|
||||||
options[:identifier] = identifier if identifier
|
options[:identifier] = identifier if identifier
|
||||||
|
@ -12,6 +12,7 @@ module Whenever
|
|||||||
@options = options
|
@options = options
|
||||||
|
|
||||||
@options[:file] ||= 'config/schedule.rb'
|
@options[:file] ||= 'config/schedule.rb'
|
||||||
|
@options[:cut] ||= 0
|
||||||
@options[:identifier] ||= default_identifier
|
@options[:identifier] ||= default_identifier
|
||||||
|
|
||||||
unless File.exists?(@options[:file])
|
unless File.exists?(@options[:file])
|
||||||
@ -23,6 +24,13 @@ module Whenever
|
|||||||
warn("[fail] Can only update, write or clear. Choose one.")
|
warn("[fail] Can only update, write or clear. Choose one.")
|
||||||
exit(1)
|
exit(1)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
@ -53,7 +61,7 @@ module Whenever
|
|||||||
command << "-u #{@options[:user]}" if @options[:user]
|
command << "-u #{@options[:user]}" if @options[:user]
|
||||||
|
|
||||||
command_results = %x[#{command.join(' ')} 2> /dev/null]
|
command_results = %x[#{command.join(' ')} 2> /dev/null]
|
||||||
@current_crontab = $?.exitstatus.zero? ? command_results : ''
|
@current_crontab = $?.exitstatus.zero? ? prepare(command_results) : ''
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_crontab(contents)
|
def write_crontab(contents)
|
||||||
@ -95,6 +103,11 @@ module Whenever
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
def prepare(contents)
|
||||||
|
contents.split("\n")[@options[:cut]..-1].join("\n")
|
||||||
|
end
|
||||||
|
|
||||||
def comment_base
|
def comment_base
|
||||||
"Whenever generated tasks for: #{@options[:identifier]}"
|
"Whenever generated tasks for: #{@options[:identifier]}"
|
||||||
end
|
end
|
||||||
|
@ -243,4 +243,46 @@ NEW_CRON
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "prepare-ing the output" do
|
||||||
|
setup do
|
||||||
|
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not trim off the top lines of the file" do
|
||||||
|
@command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => 0)
|
||||||
|
existing = <<-EXISTING_CRON
|
||||||
|
# Useless Comments
|
||||||
|
# at the top of the file
|
||||||
|
|
||||||
|
# Begin Whenever generated tasks for: My identifier
|
||||||
|
My whenever job that was already here
|
||||||
|
# End Whenever generated tasks for: My identifier
|
||||||
|
EXISTING_CRON
|
||||||
|
|
||||||
|
# here-doc adds an extra newline we need removed
|
||||||
|
assert_equal existing.strip, @command.send(:prepare, existing)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "trim off the top lines of the file" do
|
||||||
|
@command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => '3')
|
||||||
|
existing = <<-EXISTING_CRON
|
||||||
|
# Useless Comments
|
||||||
|
# at the top of the file
|
||||||
|
|
||||||
|
# Begin Whenever generated tasks for: My identifier
|
||||||
|
My whenever job that was already here
|
||||||
|
# End Whenever generated tasks for: My identifier
|
||||||
|
EXISTING_CRON
|
||||||
|
|
||||||
|
new_cron = <<-NEW_CRON
|
||||||
|
# Begin Whenever generated tasks for: My identifier
|
||||||
|
My whenever job that was already here
|
||||||
|
# End Whenever generated tasks for: My identifier
|
||||||
|
NEW_CRON
|
||||||
|
|
||||||
|
# here-doc adds an extra newline we need removed
|
||||||
|
assert_equal new_cron.strip, @command.send(:prepare, existing)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user