Class: NOMS::Command
- Inherits:
-
Object
- Object
- NOMS::Command
- Defined in:
- lib/noms/command.rb,
lib/noms/command/auth.rb,
lib/noms/command/base.rb,
lib/noms/command/error.rb,
lib/noms/command/window.rb,
lib/noms/command/urinion.rb,
lib/noms/command/version.rb,
lib/noms/command/document.rb,
lib/noms/command/formatter.rb,
lib/noms/command/useragent.rb,
lib/noms/command/application.rb,
lib/noms/command/urinion/data.rb,
lib/noms/command/auth/identity.rb,
lib/noms/command/xmlhttprequest.rb
Defined Under Namespace
Classes: Application, Auth, Base, Document, Error, Formatter, URInion, UserAgent, Window, XMLHttpRequest
Constant Summary collapse
- VERSION =
"0.5.0"
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(argv) ⇒ Command
constructor
A new instance of Command.
- #run ⇒ Object
Constructor Details
#initialize(argv) ⇒ Command
Returns a new instance of Command.
23 24 25 26 27 |
# File 'lib/noms/command.rb', line 23 def initialize(argv) @argv = argv @log = Logger.new($stderr) @log.formatter = lambda { |sev, , prog, msg| msg[-1].chr == "\n" ? msg : msg + "\n" } end |
Class Method Details
.run(argv) ⇒ Object
18 19 20 21 |
# File 'lib/noms/command.rb', line 18 def self.run(argv) runner = self.new(argv) runner.run end |
Instance Method Details
#run ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/noms/command.rb', line 29 def run # Find my own configuration for bookmarks and stuff # Interpret either environment variables or (maybe) initial options parser = Trollop::Parser.new do version NOMS::Command::VERSION <<-USAGE.gsub(/^\s{16}/,'') Usage: noms [noms-options] { bookmark | url } [options] [arguments] noms-options: USAGE opt :identity, "Identity file", :type => :string, :multi => true opt :verbose, "Enable verbose output" opt :list, "List bookmarks" opt :bookmarks, "Bookmark file location (can be specified multiple times)", :type => :string, :multi => true opt :nodefault_bookmarks, "Don't consult default bookmarks files", :short => '-X', :long => '--nodefault-bookmarks' opt :debug, "Enable debug output" stop_on_unknown end @opt = Trollop::with_standard_exception_handling parser do parser.parse(@argv) end Trollop::with_standard_exception_handling parser do raise Trollop::HelpNeeded if @argv.empty? and ! @opt[:list] end @opt[:debug] = true if ENV['NOMS_DEBUG'] and ! ENV['NOMS_DEBUG'].empty? default_bookmarks = [ File.join(ENV['HOME'], '.noms/bookmarks.json'), '/usr/local/etc/noms/bookmarks.json', '/etc/noms/bookmarks.json'].select { |f| File.exist? f } @opt[:bookmarks].concat default_bookmarks unless @opt[:nodefault_bookmarks] @log.level = Logger::WARN @log.level = Logger::INFO if @opt[:verbose] @log.level = Logger::DEBUG if @opt[:debug] @bookmark = @opt[:bookmarks].map do |file| begin File.open(file, 'r') { |fh| JSON.load fh } rescue JSON::ParserError => j @log.warn "Couldn't load bookmarks from invalid JSON file #{file.inspect}" @log.debug "JSON error: #{file.inspect}:#{j.}" nil rescue StandardError => e @log.warn "Couldn't open #{file.inspect} (#{e.class}): #{e.}" @log.debug "Error opening #{file.inspect}: #{e.backtrace.join("\n")}" nil end end.compact.reverse.inject({}) { |h1, h2| h1.merge h2 } if @opt[:list] puts @bookmark.map { |pair| '%-15s -> %s' % pair } return 0 end begin origin = @bookmark[@argv[0].split('/').first] || @argv[0] app = NOMS::Command::Application.new(origin, @argv, :logger => @log, :specified_identities => @opt[:identity]) app.fetch! # Retrieve page app.render! # Run scripts out = app.display puts out unless out.empty? app.exitcode # Return exitcode rescue NOMS::Command::Error => e @log.error "noms error: #{e.}" 255 end end |