Skip to content

Commit

Permalink
Merge pull request #515 from sibext/master
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld authored Mar 20, 2024
2 parents 5f85e77 + 96a8fad commit 0687268
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
7 changes: 7 additions & 0 deletions lib/whenever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ def self.script_rails?
def self.bundler?
File.exist?(File.join(path, 'Gemfile'))
end

def self.update_cron options
o = { 'update' => true, 'console' => false}
o.merge! options if options
Whenever::CommandLine.execute o
end

end
30 changes: 22 additions & 8 deletions lib/whenever/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ def initialize(options={})
@options[:file] ||= 'config/schedule.rb'
@options[:cut] ||= 0
@options[:identifier] ||= default_identifier
@options[:console] = true if @options[:console].nil?

if !File.exist?(@options[:file]) && @options[:clear].nil?
warn("[fail] Can't find file: #{@options[:file]}")
exit(1)
return_or_exit(false)
end

if [@options[:update], @options[:write], @options[:clear]].compact.length > 1
warn("[fail] Can only update, write or clear. Choose one.")
exit(1)
return_or_exit(false)
end

unless @options[:cut].to_s =~ /[0-9]*/
warn("[fail] Can't cut negative lines from the crontab #{options[:cut]}")
exit(1)
return_or_exit(false)
end
@options[:cut] = @options[:cut].to_i

Expand All @@ -42,7 +43,7 @@ def run
puts Whenever.cron(@options)
puts "## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated."
puts "## [message] Run `whenever --help' for more options."
exit(0)
return_or_exit(true)
end
end

Expand Down Expand Up @@ -85,21 +86,21 @@ def write_crontab(contents)
action = 'written' if @options[:write]
action = 'updated' if @options[:update]
puts "[write] crontab file #{action}"
exit(0)
return_or_exit(true)
else
warn "[fail] Couldn't write crontab; try running `whenever' with no options to ensure your schedule file is valid."
exit(1)
return_or_exit(false)
end
end

def updated_crontab
# Check for unopened or unclosed identifier blocks
if read_crontab =~ Regexp.new("^#{comment_open_regex}\s*$") && (read_crontab =~ Regexp.new("^#{comment_close_regex}\s*$")).nil?
warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open(false)}', but no '#{comment_close(false)}'"
exit(1)
return_or_exit(false)
elsif (read_crontab =~ Regexp.new("^#{comment_open_regex}\s*$")).nil? && read_crontab =~ Regexp.new("^#{comment_close_regex}\s*$")
warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close(false)}', but no '#{comment_open(false)}'"
exit(1)
return_or_exit(false)
end

# If an existing identifier block is found, replace it with the new cron entries
Expand Down Expand Up @@ -151,5 +152,18 @@ def comment_close_regex
def timestamp_regex
" at: \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} ([+-]\\d{4}|UTC)"
end

private

def return_or_exit success
result = 1
result = 0 if success
if @options[:console]
exit(result)
else
result
end
end

end
end

0 comments on commit 0687268

Please sign in to comment.