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
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! app.render! out = app.display
puts out unless out.empty?
app.exitcode rescue NOMS::Command::Error => e
@log.error "noms error: #{e.message}"
255
end
end
|