bin/pinpress in pinpress-1.2.3 vs bin/pinpress in pinpress-1.3.0

- old
+ new

@@ -31,10 +31,11 @@ # OTHER DEALINGS IN THE SOFTWARE. #-------------------------------------------------------------------- require 'chronic' require 'cliutils' require 'gli' +require 'htmlentities' require 'pinboard' require 'pinpress' include CLIUtils::Configuration include CLIUtils::Messaging @@ -47,19 +48,20 @@ version PinPress::VERSION # ====================================================== # Global Flags and Switches # ====================================================== +switch([:v], desc: 'Run pinpress in verbose mode') # ====================================================== -# Pre, Post, and Error +# Pre (runs before every command) # ====================================================== pre do |global, command, options, args| # Load PinPress configuration module. load_configuration(PinPress::CONFIG_FILEPATH) file_logger = Logger.new(PinPress::LOG_FILEPATH) - file_logger.level = LOG_LEVELS[configuration.pinpress[:log_level] || 'DEBUG'] + file_logger.level = LOG_LEVELS[configuration.pinpress.log_level || 'DEBUG'] messenger.attach(LOGFILE: file_logger) if File.exist?(PinPress::CONFIG_FILEPATH) # Set the current and last config versions in the Configurator. configuration.current_version = configuration.pinpress[:version] @@ -78,14 +80,20 @@ exit!(0) end true end +# ====================================================== +# Post (runs after every command) +# ====================================================== post do |global, command, options, args| end +# ====================================================== +# Error (runs when an exception is raised) +# ====================================================== on_error do |exception| messenger.error(exception.to_s) exit!(1) true end @@ -101,10 +109,12 @@ # ------------------------------------------------------ desc 'Install and initialize dependencies' command :init do |c| c.switch([:s], desc: 'Run init from scratch') c.action do |global_options, options, args| + PinPress.verbose = global_options[:v] + if options[:s] PinPress.init(true) else long_message = "You've already initialized PinPress. Do it again?" PinPress.init if messenger.prompt(long_message, 'N').downcase == 'y' @@ -122,26 +132,62 @@ c.flag([:e], desc: 'The end date to pull pins to') c.flag([:n], desc: 'The number of results to return') c.flag([:s], desc: 'The start date to pull pins from') c.flag([:t], desc: 'The tags to use (e.g., "ruby,pinboard")') + c.desc 'Gets pins from Pinboard' + c.action do |global_options, options, args| + PinPress.verbose = global_options[:v] + + # Figure out the template to use based on the passed argument (if any) + # and/or the presence of a default template. + template = PinPress.init_template(args[0], PinPress::Template::TYPE_PIN) + + # Assuming a valid template is found, transform CLI flags into options for + # the Pinboard gem. + opts = {} + opts.merge!(todt: Chronic.parse(options[:e])) if options[:e] + opts.merge!(fromdt: Chronic.parse(options[:s])) if options[:s] + opts.merge!(PinPress.merge_common_options(options)) + + # Request pin data from Pinboard and output the return data. + output = PinPress.pin_yield(template, opts) + puts output if output + + # Save the last-run date to the configuration file. + configuration.pinpress.last_pins_run = Time.now.utc.iso8601 + configuration.save + end + c.desc 'Gets all pins from the last run date + 1' c.command :last do |last| last.action do |global_options, options, args| - last_run_date = configuration.pinpress[:last_pins_run] + PinPress.verbose = global_options[:v] + + last_run_date = configuration.pinpress.last_pins_run if last_run_date - options[:s] = last_run_date + 1 - puts PinPress.get_data(PinPress::Template::TEMPLATE_TYPE_PIN, args, options) + # Figure out the template to use based on the passed argument (if any) + # and/or the presence of a default template. + template = PinPress.init_template(args[0], PinPress::Template::TYPE_PIN) + + # Set one option: the start date. Set it to the last-run date + 1. + opts = {} + opts.merge!(fromdt: last_run_date + 1) + opts.merge!(PinPress.merge_common_options(options)) + + # Request pin data from Pinboard and output the return data. + output = PinPress.pin_yield(template, opts) + puts output if output + + # Save the last-run date to the configuration file. + configuration.pinpress.last_pins_run = Time.now.utc.iso8601 + configuration.save else messenger.warn("`pinpress pins` hasn't been run before.") end end end - - c.action do |global_options, options, args| - puts PinPress.get_data(PinPress::Template::TEMPLATE_TYPE_PIN, args, options) - end end # ------------------------------------------------------ # tags command # @@ -154,35 +200,71 @@ c.flag([:s], desc: 'The start date to pull pins from') c.desc 'Gets all tags from the last run date + 1' c.command :last do |last| last.action do |global_options, options, args| - last_run_date = configuration.pinpress[:last_tags_run] + last_run_date = configuration.pinpress.last_tags_run if last_run_date - options[:s] = last_run_date + 1 - puts PinPress.get_data(PinPress::Template::TEMPLATE_TYPE_TAG, args, options) + PinPress.verbose = global_options[:v] + + # Figure out the template to use based on the passed argument (if any) + # and/or the presence of a default template. + template = PinPress.init_template(args[0], PinPress::Template::TYPE_TAG) + + # Set one option: the start date. Set it to the last-run date + 1. + opts = {} + opts.merge!(fromdt: last_run_date + 1) + opts.merge!(PinPress.merge_common_options(options)) + + # Request tag data from Pinboard and output the return data. + output = PinPress.tag_yield(template, opts) + puts output if output + + # Save the last-run date to the configuration file. + configuration.pinpress.last_tags_run = Time.now.utc.iso8601 + configuration.save else messenger.warn("`pinpress tags` hasn't been run before.") end end end - c.desc 'Gets the unique tags based on the passed criteria' + c.desc 'Gets unique tags based on the passed criteria' c.action do |global_options, options, args| - puts PinPress.get_data(PinPress::Template::TEMPLATE_TYPE_TAG, args, options) + PinPress.verbose = global_options[:v] + + # Figure out the template to use based on the passed argument (if any) + # and/or the presence of a default template. + template = PinPress.init_template(args[0], PinPress::Template::TYPE_TAG) + + # Assuming a valid template is found, transform CLI flags into options for + # the Pinboard gem. + opts = {} + opts.merge!(todt: Chronic.parse(options[:e])) if options[:e] + opts.merge!(fromdt: Chronic.parse(options[:s])) if options[:s] + opts.merge!(PinPress.merge_common_options(options)) + + # Request tag data from Pinboard and output the return data. + output = PinPress.tag_yield(template, opts) + puts output if output + + # Save the last-run date to the configuration file. + configuration.pinpress.last_tags_run = Time.now.utc.iso8601 + configuration.save end end # ------------------------------------------------------ # templates command # -# Manages pin templates. +# Manages pin and tag templates. # ------------------------------------------------------ desc 'Work with templates for pin output' command :templates do |c| c.desc 'List current templates' c.command :list do |list| list.action do |global_options, options, args| + PinPress.verbose = global_options[:v] PinPress.list_templates end end c.default_command :list end