bin/srd in sifttter-redux-0.4.4 vs bin/srd in sifttter-redux-0.4.6
- old
+ new
@@ -30,234 +30,143 @@
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#--------------------------------------------------------------------
-
-require 'fileutils'
require 'gli'
require 'methadone'
require 'sifttter_redux'
+require 'securerandom'
include GLI::App
include Methadone::CLILogging
include SifttterRedux
+# ======================================================
+# App Info
+# ======================================================
program_desc 'Sifttter Redux
A customized IFTTT-to-Day One service that allows for
smart installation and automated running on a standalone
*NIX device (such as a Raspberry Pi).'
-
+
version VERSION
# ======================================================
# Global Flags and Switches
# ======================================================
+switch([:verbose], desc: 'Turns on verbose output')
-switch(
- [:verbose],
- desc: 'Turns on verbose output'
-)
-
# ======================================================
# Pre, Post, and Error
# ======================================================
-
pre do |global, command, options, args|
-
# Load SifttterRedux configuration module.
Configuration::load(SRD_CONFIG_FILEPATH)
# Load Methadone CLILogging module.
- Methadone::CLILogging::change_logger(Methadone::CLILogger.new(SRD_LOG_FILEPATH, SRD_LOG_FILEPATH))
+ Methadone::CLILogging::change_logger(Methadone::CLILogger.new(SRD_LOG_FILEPATH, SRD_LOG_FILEPATH))
if File.exists?(SRD_CONFIG_FILEPATH)
+ current_version = Gem::Version.new(Configuration['sifttter_redux']['version'])
+ last_config_change_version = Gem::Version.new(NEWEST_CONFIG_VERSION)
+
# If the config file needs updating, force the user to do that first.
- if Configuration['sifttter_redux']['version'].nil? || Gem::Version.new(Configuration['sifttter_redux']['version']) < Gem::Version.new(NEWEST_CONFIG_VERSION)
+ if Configuration['sifttter_redux']['version'].nil? || current_version < last_config_change_version
CLIMessage::info('This version needs to make some config changes.')
- CLIMessage::info("Don't worry; when prompted, original values to existing config options will be presented.")
- CLIMessage::prompt("Press enter to continue")
-
+ CLIMessage::info("Don't worry; when prompted, your current values for")
+ CLIMessage::info("existing config options will be presented (so it'll")
+ CLIMessage::info('be easier to fly through the upgrade).')
+ CLIMessage::prompt('Press enter to continue')
SifttterRedux::init(true)
exit!
end
+
+ # Load Dropbox Uploader module.
+ DBU.load(Configuration['db_uploader']['exe_filepath'])
else
- # Force the user to initialize if they try to execute first.
- CLIMessage::info('You need to initialize Sifttter Redux first...') unless command.name_for_help[0] == 'init'
-
+ # Force the user to init if they try to run any command other than `init` first.
+ CLIMessage::info('Initializing Sifttter Redux first...') unless command.name_for_help[0] == 'init'
SifttterRedux::init
exit!
end
-
- # Load Dropbox Uploader module.
- DBU.load(Configuration['db_uploader']['exe_filepath']) if File.exists?(SRD_CONFIG_FILEPATH)
-
+
true
end
# ======================================================
# Commands
# ======================================================
-
# ------------------------------------------------------
# exec command
#
# Executes the script.
# ------------------------------------------------------
-
desc 'Execute the script'
command :exec do |c|
+ c.flag([:f], desc: 'Run catch-up mode with this start date')
+ c.flag([:n], desc: 'Run catch-up mode for the last N days')
+ c.flag([:t], desc: 'Run catch-up mode with this end date (must also have -f)')
+ c.flag([:w], desc: 'Run catch-up mode for the last N weeks')
- c.flag(
- [:f],
- desc: 'Run catch-up mode with this start date'
- )
+ c.switch([:c], desc: 'Run catch-up mode from the beginning of the week to yesterday')
+ c.switch([:i], desc: "Include today's date in catch-up")
+ c.switch([:verbose], desc: 'Turns on verbose output')
+ c.switch([:y], desc: 'Run catch-up mode for yesterday')
- c.flag(
- [:n],
- desc: 'Run catch-up mode for the last N days'
- )
-
- c.flag(
- [:t],
- desc: 'Run catch-up mode with this end date (must also have -f)'
- )
-
- c.flag(
- [:w],
- desc: 'Run catch-up mode for the last N weeks'
- )
-
- c.switch(
- [:c],
- desc: 'Run catch-up mode from the beginning of the week to yesterday'
- )
-
- c.switch(
- [:i],
- desc: "Include today's date in catch-up"
- )
-
- c.switch(
- [:verbose],
- desc: 'Turns on verbose output'
- )
-
- c.switch(
- [:y],
- desc: 'Run catch-up mode for yesterday'
- )
-
c.action do |global_options, options, args|
-
SifttterRedux.verbose = global_options[:verbose] || options[:verbose]
- CLIMessage::section_block('EXECUTING...') do
- if options[:c] || options[:n] || options[:w] || options[:y] || options[:f] || options[:t]
+ dates = SifttterRedux::get_dates_from_options(options)
+ unless dates.nil?
+ first_date = dates.first
+ second_date = dates.reverse_each.first
- command_complete = false
+ date_string = first_date.strftime('%B %d, %Y')
+ date_string << " to #{ second_date.strftime('%B %d, %Y') }" if first_date != second_date
+ CLIMessage::info("Creating #{ first_date == second_date ? 'entry' : 'entries' }: #{ date_string }")
- # Current Week
- if !command_complete && options[:c]
- dates = DateRangeMaker.last_n_weeks(0, options[:i])
- command_complete = true
- end
+ # Download Sifttter files from Dropbox.
+ DBU.local_target = Configuration['sifttter_redux']['sifttter_local_filepath']
+ DBU.remote_target = Configuration['sifttter_redux']['sifttter_remote_filepath']
+ DBU.message = 'Downloading Sifttter files...'
+ DBU.download
- # Last N Days
- if !command_complete && options[:n]
- dates = DateRangeMaker.last_n_days(options[:n].to_i, options[:i])
- command_complete = true
- end
-
- # Yesterday
- if !command_complete && options[:y]
- dates = DateRangeMaker.yesterday
- command_complete = true
- end
-
- # Last N Weeks
- if !command_complete && options[:w]
- dates = DateRangeMaker.last_n_weeks(options[:w].to_i, options[:i])
- command_complete = true
- end
-
- # Specific Range
- if !command_complete && (options[:f] || options[:t])
- begin
- dates = DateRangeMaker.range(options[:f], options[:t], options[:i])
-
- if dates.last > Date.today
- CLIMessage::warning("Ignoring overextended end date and using today's date (#{ Date.today })")
- dates = (dates.first..Date.today)
- end
- rescue ArgumentError => e
- CLIMessage::error(e)
- end
- end
- else
- dates = DateRangeMaker.today
+ # Process a new Sifttter entry for each date.
+ dates.each do |date|
+ Sifttter::run(date)
end
- unless dates.nil?
- first_date = dates.first
- second_date = dates.reverse_each.first
-
- if first_date == second_date
- date_string = first_date.strftime('%B %d, %Y')
- CLIMessage::info("Creating entry for #{ date_string }...")
- else
- date_string = "#{ first_date.strftime('%B %d, %Y') } to #{ second_date.strftime('%B %d, %Y') }"
- CLIMessage::info("Creating entries for dates from #{ date_string }...")
- end
-
- DBU.local_target = Configuration['sifttter_redux']['sifttter_local_filepath']
- DBU.remote_target = Configuration['sifttter_redux']['sifttter_remote_filepath']
- DBU.message = 'Downloading Sifttter files...'
- DBU.download
-
- dates.each do |date|
- Sifttter.run(date)
- end
-
- # Upload any Day One entries to Dropbox (if there are any).
- unless Dir[Configuration['sifttter_redux']['dayone_local_filepath'] + '/*'].empty?
- DBU.local_target = "#{ Configuration['sifttter_redux']['dayone_local_filepath'] }/*"
- DBU.remote_target = Configuration['sifttter_redux']['dayone_remote_filepath']
- DBU.message = 'Uploading Day One entries to Dropbox...'
- DBU.upload
- end
-
- # Remove any downloaded local files that we no longer need.
- dirs = [
- Configuration['sifttter_redux']['dayone_local_filepath'],
- Configuration['sifttter_redux']['sifttter_local_filepath']
- ]
-
- CLIMessage::info_block('Removing temporary local files...') { dirs.each { |d| FileUtils.rm_rf(d) } }
+ # Upload any Day One entries to Dropbox (if there are any).
+ unless Dir[Configuration['sifttter_redux']['dayone_local_filepath'] + '/*'].empty?
+ DBU.local_target = "#{ Configuration['sifttter_redux']['dayone_local_filepath'] }/*"
+ DBU.remote_target = Configuration['sifttter_redux']['dayone_remote_filepath']
+ DBU.message = 'Uploading Day One entries to Dropbox...'
+ DBU.upload
end
+
+ # Remove any downloaded local files that we no longer need.
+ SifttterRedux::cleanup_temp_files
end
end
end
# ------------------------------------------------------
# init command
#
# Initializes the script.
# ------------------------------------------------------
-desc 'Install and initialize dependencies'
+desc 'Install and SifttterRedux::initialize dependencies'
command :init do |c|
-
- c.switch(
- [:s],
- desc: 'Run init from scratch (i.e., clear out all values from configuration)'
- )
-
+ c.switch([:s], desc: 'Run init from scratch (i.e., clear out all values from configuration)')
+
c.action do |global_options, options, args|
CLIMessage::section_block('INITIALIZING...') do
if File.exists?(Configuration::config_path) && !options[:s]
- SifttterRedux::init(true) if CLIMessage::prompt("It looks like you've already initialized Sifttter Redux. Do you want to re-initialize?", 'N').downcase == 'y'
+ long_message = "You've already initialized Sifttter Redux. Do it again?"
+ SifttterRedux::init(true) if CLIMessage::prompt(long_message, 'N').downcase == 'y'
else
SifttterRedux::init
end
end
end