Class: NOMS::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/noms/command.rb,
lib/noms/command/auth.rb,
lib/noms/command/base.rb,
lib/noms/command/home.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,
lib/noms/command/useragent/cache.rb,
lib/noms/command/useragent/response.rb,
lib/noms/command/useragent/requester.rb,
lib/noms/command/useragent/response/typhoeus.rb,
lib/noms/command/useragent/requester/typhoeus.rb,
lib/noms/command/useragent/response/httpclient.rb,
lib/noms/command/useragent/requester/httpclient.rb

Defined Under Namespace

Classes: Application, Auth, Base, Document, Error, Formatter, URInion, UserAgent, Window, XMLHttpRequest

Constant Summary collapse

VERSION =
"2.1.1"
@@home =
(ENV.has_key?('NOMS_HOME') and ! ENV['NOMS_HOME'].empty?) ? ENV['NOMS_HOME'] : File.join(ENV['HOME'], '.noms')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

Returns a new instance of Command.



32
33
34
35
36
# File 'lib/noms/command.rb', line 32

def initialize(argv)
    @argv = argv
    @log = Logger.new($stderr)
    @log.formatter = lambda { |sev, timestamp, prog, msg| (msg.empty? or msg[-1].chr != "\n") ? msg + "\n" : msg }
end

Class Method Details

.homeObject



17
18
19
# File 'lib/noms/command/home.rb', line 17

def self.home
    @@home
end

.home=(value) ⇒ Object



13
14
15
# File 'lib/noms/command/home.rb', line 13

def self.home=(value)
    @@home = value
end

.run(argv) ⇒ Object



27
28
29
30
# File 'lib/noms/command.rb', line 27

def self.run(argv)
    runner = self.new(argv)
    runner.run
end

Instance Method Details

#runObject



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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/noms/command.rb', line 38

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
        banner <<-USAGE.gsub(/^\s{16}/,'')
            Usage:
              noms [noms-options] { bookmark | url } [options] [arguments]
              noms-options:
        USAGE
        opt :identity, "Identity file", :short => '-i',
                                        :type => :string,
                                        :multi => true
        opt :logout,   "Log out of authentication sessions", :short => '-L'
        opt :verbose,  "Enable verbose output", :short => '-v'
        opt :list,     "List bookmarks", :short => '-l'
        opt :bookmarks, "Bookmark file location (can be specified multiple times)",
            :short => '-b',
            :type => :string,
            :multi => true
        opt :home,    "Use directory as NOMS_HOME instead of #{NOMS::Command.home}",
            :short => '-H',
            :type => :string
        opt :nocache,  "Don't cache files",
            :short => '-C'
        opt :nodefault_bookmarks, "Don't consult default bookmarks files",
            :short => '-X',
            :long => '--nodefault-bookmarks'
        opt :debug,    "Enable debug output", :short => '-d'
        opt :'plaintext-identity', "Save identity credentials in plaintext", :short => '-P'
        stop_on_unknown
    end

    @opt = Trollop::with_standard_exception_handling parser do
        parser.parse(@argv)
    end

    NOMS::Command.home = @opt[:home] if @opt[:home]

    Trollop::with_standard_exception_handling parser do
        raise Trollop::HelpNeeded if @argv.empty? and ! @opt[:list] and ! @opt[:logout]
    end

    @opt[:debug] = true if ENV['NOMS_DEBUG'] and ! ENV['NOMS_DEBUG'].empty?

    default_bookmarks =
        [ File.join(NOMS::Command.home, '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.message}"
            nil
        rescue StandardError => e
            @log.warn "Couldn't open #{file.inspect} (#{e.class}): #{e.message}"
            @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

    if @opt[:logout]
        File.unlink NOMS::Command::Auth::Identity.vault_keyfile if
            File.exist? NOMS::Command::Auth::Identity.vault_keyfile
        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],
                                             :cache => ! @opt[:nocache],
                                             :plaintext_identity => @opt[:'plaintext-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.message}"
        255
    end
end