Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added ability to set variabled on-the-fly from the command line

  • Loading branch information...
commit c1bc0ab1b9530f52b19927da278fe1a8617a3f94 1 parent 623c895
@javan javan authored
View
4 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]
View
2  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
View
3  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)
View
4 lib/command_line.rb
@@ -28,7 +28,7 @@ def run
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 @@ def default_identifier
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
View
24 lib/job_list.rb
@@ -5,20 +5,24 @@ def initialize(options)
@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 @@ def generate_cron_output
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?
View
4 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
View
17 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
View
68 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
View
6 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"]
Please sign in to comment.
Something went wrong with that request. Please try again.