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

This commit is contained in:
Javan Makhmali 2009-06-02 12:26:15 +02:00
parent 623c895d5e
commit c1bc0ab1b9
9 changed files with 121 additions and 11 deletions

View File

@ -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 == 0.2.2 / April 30th, 2009
* Days of week jobs can now accept an :at directive (ex: every :monday, :at => '5pm'). [David Eisinger] * Days of week jobs can now accept an :at directive (ex: every :monday, :at => '5pm'). [David Eisinger]

View File

@ -10,6 +10,7 @@ lib/job_types/runner.rb
lib/outputs/cron.rb lib/outputs/cron.rb
lib/version.rb lib/version.rb
lib/whenever.rb lib/whenever.rb
Manifest
Rakefile Rakefile
README.rdoc README.rdoc
test/command_line_test.rb test/command_line_test.rb
@ -20,4 +21,3 @@ test/output_rake_test.rb
test/output_runner_test.rb test/output_runner_test.rb
test/test_helper.rb test/test_helper.rb
whenever.gemspec whenever.gemspec
Manifest

View File

@ -24,6 +24,9 @@ OptionParser.new do |opts|
opts.on('-u', '--user [user]', 'Default: current user') do |user| opts.on('-u', '--user [user]', 'Default: current user') do |user|
options[:user] = user if user options[:user] = user if user
end end
opts.on('-s', '--set [variables]', 'Example: --set environment=staging&path=/my/sweet/path') do |set|
options[:set] = set if set
end
end.parse! end.parse!
Whenever::CommandLine.execute(options) Whenever::CommandLine.execute(options)

View File

@ -28,7 +28,7 @@ module Whenever
elsif @options[:write] elsif @options[:write]
write_crontab(whenever_cron) write_crontab(whenever_cron)
else else
puts Whenever.cron(:file => @options[:file]) puts Whenever.cron(@options)
exit exit
end end
end end
@ -40,7 +40,7 @@ module Whenever
end end
def whenever_cron 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 end
def read_crontab def read_crontab

View File

@ -5,20 +5,24 @@ module Whenever
@jobs = Hash.new @jobs = Hash.new
@env = Hash.new @env = Hash.new
config = case options case options
when String then options when String
config = options
when Hash when Hash
if options[:string] config = if options[:string]
options[:string] options[:string]
elsif options[:file] elsif options[:file]
File.read(options[:file]) File.read(options[:file])
end end
pre_set(options[:set])
end end
eval(config) eval(config)
end end
def set(variable, value) def set(variable, value)
return if instance_variable_defined?("@#{variable}".to_sym)
instance_variable_set("@#{variable}".to_sym, value) instance_variable_set("@#{variable}".to_sym, value)
self.class.send(:attr_reader, variable.to_sym) self.class.send(:attr_reader, variable.to_sym)
end end
@ -57,6 +61,20 @@ module Whenever
end end
private 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 def environment_variables
return if @env.empty? return if @env.empty?

View File

@ -1,8 +1,8 @@
module Whenever module Whenever
module VERSION #:nodoc: module VERSION #:nodoc:
MAJOR = 0 MAJOR = 0
MINOR = 2 MINOR = 3
TINY = 2 TINY = 0
STRING = [MAJOR, MINOR, TINY].join('.') STRING = [MAJOR, MINOR, TINY].join('.')
end end

View File

@ -67,4 +67,21 @@ class OutputCommandTest < Test::Unit::TestCase
end end
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 end

View File

@ -122,4 +122,72 @@ class OutputRunnerTest < Test::Unit::TestCase
end end
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 end

View File

@ -2,16 +2,16 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{whenever} 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.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Javan Makhmali"] 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.description = %q{Provides clean ruby syntax for defining messy cron jobs and running them Whenever.}
s.email = %q{javan@javan.us} s.email = %q{javan@javan.us}
s.executables = ["whenever", "wheneverize"] 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.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.has_rdoc = true
s.homepage = %q{http://github.com/javan/whenever} s.homepage = %q{http://github.com/javan/whenever}
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Whenever", "--main", "README.rdoc"] s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Whenever", "--main", "README.rdoc"]