bin/klipbook in klipbook-0.3.0 vs bin/klipbook in klipbook-1.0.0

- old
+ new

@@ -1,7 +1,91 @@ #!/usr/bin/env ruby $LOAD_PATH << File.expand_path('../../lib', __FILE__) + +require 'gli' require 'klipbook' -Klipbook::CLI.start +include GLI::App + +program_desc "Collates the clippings you've saved on your Kindle into a nice html summary for each book." + +SOURCE_HELP = "Two sources are currently supported: a file from a Kindle device and the Kindle site itself.\n\n" + + "Example file formats:\n\n" + + " file:path/to/my-clippings-file.txt\n\n" + + " site:my-kindle-user@blah.com:my-kindle-password\n\n" + + "Note that source and output file defaults can be stored in a file called ~/.klipbookrc\n\n" + + "This file is YAML formatted e.g.\n\n" + + "source: site:my-kindle-user@blah.com:my-kindle-password\n\n" + + "output: my/default/output/directory" + +version Klipbook::VERSION + +desc 'Number of books to process' +arg_name 'count' +default_value 1 +flag [:n, :'num-books'] + +desc 'Collate your clippings into a html file for each book' +arg_name 'source' +long_desc "Clippings are fetched from the specified source before being collated into html " + + "files and written into the current directory.\n\n" + SOURCE_HELP +command :collate do |c| + + c.desc 'Override the directory path where collated files are written' + c.arg_name 'Directory' + c.flag [:o, :'output-dir'] + + c.desc 'Force overwrite of any existing collated files' + c.switch [:f, :force] + + c.action do |globals,options,args| + books = fetch_books(args, globals) + Klipbook::Collator.new(books).collate_books(output_dir(options), options[:f]) + end +end + +desc 'List available books' +arg_name 'source' +long_desc "Clippings are fetched from the specified source before being listed to screen.\n\n" + SOURCE_HELP +command :list do |c| + c.action do |globals,options,args| + books = fetch_books(args, globals) + Klipbook::Printer.new(books).print + end +end + +pre do |globals,command,options,args| + unless source_spec(args) + raise InvalidSourceError + end + + odir = output_dir(options) + if odir && !File.exists?(odir) + raise "Output directory doesn't exist: #{odir}" + end + + if globals[:n].to_i == 0 + raise 'Specify a number of books greater than 0' + end + + true +end + +def source_spec(args) + @source_spec ||= (args[0] || Klipbook::Config.new.read[:source]) +end + +def fetch_books(args, globals) + Klipbook::Fetcher.new(source_spec(args), globals[:n].to_i).fetch_books +end + +def output_dir(options) + @output_dir ||= unless options[:o] == 'Current directory' + options[:o] + else + Klipbook::Config.new.read[:output] || Dir.pwd + end +end + +exit run(ARGV)