bin/twog in twog-0.2.2 vs bin/twog in twog-0.3.0

- old
+ new

@@ -1,42 +1,86 @@ #!/usr/bin/env ruby $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) +require 'tempfile' require 'twog' require 'optparse' +require 'whenever' +require 'fileutils' +twog_dir = "#{ENV['HOME']}/.twog" +FileUtils.mkdir_p twog_dir unless File.exists?(twog_dir) +twog_cron_schedule_file = "#{twog_dir}/schedule.rb" +twog_cron_schedule_log = "#{twog_dir}/cron.log" +twog_cron_identifier = "twog" +legacy_conf_file_name = "#{ENV['HOME']}/.twog.yaml" +twog_conf_file = "#{twog_dir}/conf.yaml" + options = {} opts = OptionParser.new do |opts| opts.banner = "Twog is a tool to tweet blog posts" - opts.on("--version", "Display current version") do + opts.on_tail("-h","-?","--help", "Display help") do + puts opts + exit 0 + end + + opts.on("-v", "--version", "Display current version") do puts "Twog " + Twog.version exit 0 end + + opts.on("--cronadd N", "Add crontab job to run twog every N minutes") do |n| + twog_cron_schedule_content = <<-EOS + set :output, "#{twog_cron_schedule_log}" + every #{n}.minutes do + command "twog" + end + EOS + + answer = "" + if File.exists?(twog_cron_schedule_file) + puts "Cron schedule file (#{twog_cron_schedule_file}) already exists. Overwrite? [Y/n]" + answer = gets + end + + File.open(twog_cron_schedule_file, 'w') {|f| f.write(twog_cron_schedule_content) } if ["Y","y",""].include?(answer.chomp) + + cron_options = {:file => twog_cron_schedule_file, :identifier => twog_cron_identifier, :update => true} + Whenever::CommandLine.execute(cron_options) + exit 0 + end + opts.on("--cronrm", "Remove twog crontab job") do + File.open(twog_cron_schedule_file, 'w') {|f| f.write("") } + cron_options = {:file => twog_cron_schedule_file, :identifier => twog_cron_identifier, :update => true} + Whenever::CommandLine.execute(cron_options) + puts "Twog cron job removed successfully" + exit 0 + end + opts.on("--conf", "Create default conf file") do - AUTH_FILE_NAME = "#{ENV['HOME']}/.twog.yaml" - - AUTH_CONFIG_FILE = <<-EOS + conf_file_contents = <<-EOS rss_feed: 'http://url.com/feed.rss' consumer_key: 'consumer key' consumer_secret: 'consumer secret' access_token: 'access token' access_secret: 'access secret' bitly_username: 'username' bitly_api_key: 'api key' last_blog_post_tweeted: EOS - if File.exists?(AUTH_FILE_NAME) - puts "#{ENV['HOME']}/.twog.yaml already exists" + FileUtils.mv(legacy_conf_file_name, twog_conf_file) if File.exists?(legacy_conf_file_name) + if File.exists?(twog_conf_file) + puts "#{twog_conf_file} already exists" exit 0 end - File.open(AUTH_FILE_NAME, 'w') {|f| f.write(AUTH_CONFIG_FILE) } - puts "Default configuration file created at ~/.twog.yaml" + File.open(twog_conf_file, 'w') {|f| f.write(conf_file_contents) } + puts "Default configuration file created at #{twog_conf_file}" exit 0 end end # Read command line options into `options` hash @@ -46,9 +90,9 @@ if ARGV.size > 1 puts "Invalid options. Run `twog --help` for assistance." exit(1) end -raise "please run 'twog --conf' to create the ~/.twog.yaml file" unless File.exists?("#{ENV['HOME']}/.twog.yaml") -conf = YAML.load_file("#{ENV['HOME']}/.twog.yaml") +raise "please run 'twog --conf' to create the #{twog_conf_file} file" unless File.exists?(twog_conf_file) +conf = YAML.load_file(twog_conf_file) Twog.run(conf)