diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 5fa001b..34290c0 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,7 @@ +== 0.3.0 / June 2nd, 2009 + +* Added ability to set variables on the fly from the command line (ex: whenever --set environment=staging). [Javan Makhmali] + == 0.2.2 / April 30th, 2009 * Days of week jobs can now accept an :at directive (ex: every :monday, :at => '5pm'). [David Eisinger] diff --git a/Manifest b/Manifest index f256704..954fe50 100644 --- a/Manifest +++ b/Manifest @@ -10,6 +10,7 @@ lib/job_types/runner.rb lib/outputs/cron.rb lib/version.rb lib/whenever.rb +Manifest Rakefile README.rdoc test/command_line_test.rb @@ -20,4 +21,3 @@ test/output_rake_test.rb test/output_runner_test.rb test/test_helper.rb whenever.gemspec -Manifest diff --git a/bin/whenever b/bin/whenever index 6c761cc..39993f4 100644 --- a/bin/whenever +++ b/bin/whenever @@ -24,6 +24,9 @@ OptionParser.new do |opts| opts.on('-u', '--user [user]', 'Default: current user') do |user| options[:user] = user if user end + opts.on('-s', '--set [variables]', 'Example: --set environment=staging&path=/my/sweet/path') do |set| + options[:set] = set if set + end end.parse! Whenever::CommandLine.execute(options) \ No newline at end of file diff --git a/lib/command_line.rb b/lib/command_line.rb index d7920a8..b1bad31 100644 --- a/lib/command_line.rb +++ b/lib/command_line.rb @@ -28,7 +28,7 @@ module Whenever elsif @options[:write] write_crontab(whenever_cron) else - puts Whenever.cron(:file => @options[:file]) + puts Whenever.cron(@options) exit end end @@ -40,7 +40,7 @@ module Whenever end def whenever_cron - @whenever_cron ||= [comment_open, Whenever.cron(:file => @options[:file]), comment_close].join("\n") + @whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].join("\n") end def read_crontab diff --git a/lib/job_list.rb b/lib/job_list.rb index 21a799f..245d7b8 100644 --- a/lib/job_list.rb +++ b/lib/job_list.rb @@ -5,20 +5,24 @@ module Whenever @jobs = Hash.new @env = Hash.new - config = case options - when String then options + case options + when String + config = options when Hash - if options[:string] + config = if options[:string] options[:string] elsif options[:file] File.read(options[:file]) end + pre_set(options[:set]) end eval(config) end def set(variable, value) + return if instance_variable_defined?("@#{variable}".to_sym) + instance_variable_set("@#{variable}".to_sym, value) self.class.send(:attr_reader, variable.to_sym) end @@ -57,6 +61,20 @@ module Whenever end private + + # Takes a string like: "variable1=something&variable2=somethingelse" + # and breaks it into variable/value pairs. Used for setting variables at runtime from the command line. + # Only works for setting values as strings. + def pre_set(variable_string = nil) + return if variable_string.blank? + + pairs = variable_string.split('&') + pairs.each do |pair| + next unless pair.index('=') + variable, value = *pair.split('=') + set(variable.strip, value.strip) unless variable.blank? || value.blank? + end + end def environment_variables return if @env.empty? diff --git a/lib/version.rb b/lib/version.rb index 4a9c456..a020f59 100644 --- a/lib/version.rb +++ b/lib/version.rb @@ -1,8 +1,8 @@ module Whenever module VERSION #:nodoc: MAJOR = 0 - MINOR = 2 - TINY = 2 + MINOR = 3 + TINY = 0 STRING = [MAJOR, MINOR, TINY].join('.') end diff --git a/test/output_command_test.rb b/test/output_command_test.rb index 413d5be..ff9005c 100644 --- a/test/output_command_test.rb +++ b/test/output_command_test.rb @@ -67,4 +67,21 @@ class OutputCommandTest < Test::Unit::TestCase end end + context "A command when the cron_log is set and is overridden by the :set option" do + setup do + @output = Whenever.cron :set => 'cron_log=otherlog.log', :string => \ + <<-file + set :cron_log, 'logfile.log' + every 2.hours do + command "blahblah" + end + file + end + + should "output the otherlog.log as the log file" do + assert_no_match /.+ .+ .+ .+ blahblah >> logfile.log 2>&1/, @output + assert_match /^.+ .+ .+ .+ blahblah >> otherlog.log 2>&1/, @output + end + end + end \ No newline at end of file diff --git a/test/output_runner_test.rb b/test/output_runner_test.rb index 70dfbd6..0142852 100644 --- a/test/output_runner_test.rb +++ b/test/output_runner_test.rb @@ -122,4 +122,72 @@ class OutputRunnerTest < Test::Unit::TestCase end end + context "A runner where the environment is overridden using the :set option" do + setup do + @output = Whenever.cron :set => 'environment=serious', :string => \ + <<-file + set :environment, :silly + set :path, '/my/path' + every 2.hours do + runner "blahblah" + end + file + end + + should "output the runner using the override environment" do + assert_match two_hours + ' /my/path/script/runner -e serious "blahblah"', @output + end + end + + context "A runner where the environment and path are overridden using the :set option" do + setup do + @output = Whenever.cron :set => 'environment=serious&path=/serious/path', :string => \ + <<-file + set :environment, :silly + set :path, '/silly/path' + every 2.hours do + runner "blahblah" + end + file + end + + should "output the runner using the overridden path and environment" do + assert_match two_hours + ' /serious/path/script/runner -e serious "blahblah"', @output + end + end + + context "A runner where the environment and path are overridden using the :set option with spaces in the string" do + setup do + @output = Whenever.cron :set => ' environment = serious& path =/serious/path', :string => \ + <<-file + set :environment, :silly + set :path, '/silly/path' + every 2.hours do + runner "blahblah" + end + file + end + + should "output the runner using the overridden path and environment" do + assert_match two_hours + ' /serious/path/script/runner -e serious "blahblah"', @output + end + end + + context "A runner where the environment is overridden using the :set option but no value is given" do + setup do + @output = Whenever.cron :set => ' environment=', :string => \ + <<-file + set :environment, :silly + set :path, '/silly/path' + every 2.hours do + runner "blahblah" + end + file + end + + should "output the runner using the original environmnet" do + assert_match two_hours + ' /silly/path/script/runner -e silly "blahblah"', @output + end + end + end \ No newline at end of file diff --git a/whenever.gemspec b/whenever.gemspec index 66d8828..71ce5e0 100644 --- a/whenever.gemspec +++ b/whenever.gemspec @@ -2,16 +2,16 @@ Gem::Specification.new do |s| s.name = %q{whenever} - s.version = "0.2.2" + s.version = "0.3.0" s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version= s.authors = ["Javan Makhmali"] - s.date = %q{2009-04-30} + s.date = %q{2009-06-02} s.description = %q{Provides clean ruby syntax for defining messy cron jobs and running them Whenever.} s.email = %q{javan@javan.us} s.executables = ["whenever", "wheneverize"] s.extra_rdoc_files = ["bin/whenever", "bin/wheneverize", "CHANGELOG.rdoc", "lib/base.rb", "lib/command_line.rb", "lib/job_list.rb", "lib/job_types/default.rb", "lib/job_types/rake_task.rb", "lib/job_types/runner.rb", "lib/outputs/cron.rb", "lib/version.rb", "lib/whenever.rb", "README.rdoc"] - s.files = ["bin/whenever", "bin/wheneverize", "CHANGELOG.rdoc", "lib/base.rb", "lib/command_line.rb", "lib/job_list.rb", "lib/job_types/default.rb", "lib/job_types/rake_task.rb", "lib/job_types/runner.rb", "lib/outputs/cron.rb", "lib/version.rb", "lib/whenever.rb", "Rakefile", "README.rdoc", "test/command_line_test.rb", "test/cron_test.rb", "test/output_command_test.rb", "test/output_env_test.rb", "test/output_rake_test.rb", "test/output_runner_test.rb", "test/test_helper.rb", "whenever.gemspec", "Manifest"] + s.files = ["bin/whenever", "bin/wheneverize", "CHANGELOG.rdoc", "lib/base.rb", "lib/command_line.rb", "lib/job_list.rb", "lib/job_types/default.rb", "lib/job_types/rake_task.rb", "lib/job_types/runner.rb", "lib/outputs/cron.rb", "lib/version.rb", "lib/whenever.rb", "Manifest", "Rakefile", "README.rdoc", "test/command_line_test.rb", "test/cron_test.rb", "test/output_command_test.rb", "test/output_env_test.rb", "test/output_rake_test.rb", "test/output_runner_test.rb", "test/test_helper.rb", "whenever.gemspec"] s.has_rdoc = true s.homepage = %q{http://github.com/javan/whenever} s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Whenever", "--main", "README.rdoc"]