#!/usr/bin/env ruby # Encoding: utf-8 #-------------------------------------------------------------------- # PinPress # # A Pinboard application that allows for the creation of # "pin templates" in almost any conceivable format. # # Copyright (c) 2014 # Aaron Bach # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation # files (the "Software"), to deal in the Software without # restriction, including without limitation the rights to use, # copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following # conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # 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 'chronic' require 'cliutils' require 'gli' require 'pinboard' require 'pinpress' include CLIUtils::Configuration include CLIUtils::Messaging include GLI::App # ====================================================== # App Info # ====================================================== program_desc PinPress::DESCRIPTION version PinPress::VERSION # ====================================================== # Global Flags and Switches # ====================================================== # ====================================================== # Pre, Post, and Error # ====================================================== 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'] 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] configuration.last_version = PinPress::NEWEST_CONFIG_VERSION # Compare the two versions and, if needed, update. configuration.compare_version do |c, l| messenger.debug { "Upgrading from #{ c } to #{ l }" } PinPress.update_config_file exit!(0) end else # Force the user to init if they try to run any command # other than `init` first. PinPress.init(true) exit!(0) end true end post do |global, command, options, args| end on_error do |exception| # messenger.error(exception.to_s) # exit!(1) # true end # ====================================================== # Commands # ====================================================== # ------------------------------------------------------ # init command # # Initializes the app by asking the user for information # needed to run. # ------------------------------------------------------ desc 'Install and initialize dependencies' command :init do |c| c.switch([:s], desc: 'Run init from scratch') c.action do |global_options, options, args| 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' end end end # ------------------------------------------------------ # pins command # # Gets pins from Pinboard. # ------------------------------------------------------ desc 'Get pins from Pinboard' command :pins do |c| 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 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] if last_run_date options[:s] = last_run_date + 1 puts PinPress.get_data(PinPress::Template::TEMPLATE_TYPE_PIN, args, options) 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 # # Gets pins from Pinboard. # ------------------------------------------------------ desc 'Get tags from Pinboard' command :tags do |c| c.flag([:e], desc: 'The end date to pull pins to') c.flag([:t], desc: 'The tags to use (e.g., "ruby,pinboard")') 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] if last_run_date options[:s] = last_run_date + 1 puts PinPress.get_data(PinPress::Template::TEMPLATE_TYPE_TAG, args, options) 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.action do |global_options, options, args| puts PinPress.get_data(PinPress::Template::TEMPLATE_TYPE_TAG, args, options) end end # ------------------------------------------------------ # templates command # # Manages pin 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.list_templates end end c.default_command :list end # ====================================================== # Run! # ====================================================== exit run(ARGV)