From 3e20f936eb596536e13c93fc48cf6dcb7b42dff2 Mon Sep 17 00:00:00 2001 From: Peer Allan Date: Thu, 9 Sep 2010 00:58:19 +0800 Subject: [PATCH] Added --cut option to the command line to allow pruning of the crontab --- bin/whenever | 3 ++ lib/whenever/command_line.rb | 15 +++++++++- test/functional/command_line_test.rb | 44 +++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/bin/whenever b/bin/whenever index d109ed4..dad702e 100644 --- a/bin/whenever +++ b/bin/whenever @@ -10,6 +10,9 @@ OptionParser.new do |opts| opts.banner = "Usage: whenever [options]" opts.on('-v', '--version') { puts "Whenever v#{Whenever::VERSION}"; exit(0) } 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| options[:update] = true options[:identifier] = identifier if identifier diff --git a/lib/whenever/command_line.rb b/lib/whenever/command_line.rb index b8882f9..77409a5 100644 --- a/lib/whenever/command_line.rb +++ b/lib/whenever/command_line.rb @@ -12,6 +12,7 @@ module Whenever @options = options @options[:file] ||= 'config/schedule.rb' + @options[:cut] ||= 0 @options[:identifier] ||= default_identifier unless File.exists?(@options[:file]) @@ -23,6 +24,13 @@ module Whenever 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 + end def run @@ -53,7 +61,7 @@ module Whenever command << "-u #{@options[:user]}" if @options[:user] command_results = %x[#{command.join(' ')} 2> /dev/null] - @current_crontab = $?.exitstatus.zero? ? command_results : '' + @current_crontab = $?.exitstatus.zero? ? prepare(command_results) : '' end def write_crontab(contents) @@ -95,6 +103,11 @@ module Whenever end end + # + def prepare(contents) + contents.split("\n")[@options[:cut]..-1].join("\n") + end + def comment_base "Whenever generated tasks for: #{@options[:identifier]}" end diff --git a/test/functional/command_line_test.rb b/test/functional/command_line_test.rb index 0267036..8611d01 100644 --- a/test/functional/command_line_test.rb +++ b/test/functional/command_line_test.rb @@ -242,5 +242,47 @@ NEW_CRON assert_match two_hours + %( cd /silly/path && script/runner -e silly 'blahblah'), @output 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