Skip to content

Commit 60a2265

Browse files
Merge pull request #51 from tkishel/argf_is_arguable
(bug) Use ARGV instead of ARGF
2 parents 6f67271 + e7ebac2 commit 60a2265

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

files/json2timeseriesdb

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# puppetlabs-puppet-metrics-viewer/json2graphite.rb
55
# puppetlabs-puppet_metrics_collector/files/json2timeseriesdb
66

7+
require 'fcntl'
78
require 'json'
89
require 'time'
910
require 'optparse'
@@ -349,44 +350,43 @@ end
349350
data_files = []
350351
data_files += Dir.glob($options[:pattern]) if $options[:pattern]
351352

352-
# Collect JSON files to process from ARGF.
353-
# http://ruby-doc.org/core-1.9.3/ARGF.html#method-i-filename
353+
# Collect JSON files to process from ARGV.
354354

355-
while ARGF.filename != '-'
356-
filename = ARGF.filename
357-
data_files += [filename]
358-
ARGF.skip
359-
break if filename == ARGF.filename
355+
data_files += ARGV
356+
357+
# Validate STDIN.
358+
359+
data_files_includes_stdin = data_files.include?('-')
360+
stdin_provided = STDIN.fcntl(Fcntl::F_GETFL, 0) == 0
361+
362+
# Guard against waiting indefinately for non-existant STDIN.
363+
if data_files_includes_stdin && !stdin_provided
364+
STDERR.puts "ERROR: STDIN specified via '-' but STDIN not provided"
365+
exit 1
360366
end
361367

362-
# Process collected JSON files.
368+
# Guard against not reading STDIN.
369+
if !data_files_includes_stdin && stdin_provided
370+
STDERR.puts "WARNING: STDIN provided but not specified via '-'"
371+
data_files += ['-']
372+
end
363373

374+
# Process collected JSON files (including '-' to process data from STDIN).
375+
364376
data_files.each do |filename|
365377
begin
366-
converted_data = parse_file(filename)
367-
if $options[:host]
368-
$net_output.write(converted_data)
378+
if filename == '-'
379+
converted_data = STDIN.each_line.map { |l| parse_input( JSON.parse(l) )}.flatten.join("\n")
369380
else
370-
STDOUT.puts(converted_data)
381+
converted_data = parse_file(filename)
371382
end
372-
rescue => e
373-
STDERR.puts "ERROR: #{filename}: #{e.message}"
374-
end
375-
end
376-
377-
# Process JSON data from STDIN.
378-
379-
if ARGF.filename == '-'
380-
begin
381-
input = ARGF.read
382-
converted_data = input.lines.map { |l| parse_input( JSON.parse(l) )}
383383
if $options[:host]
384-
$net_output.write(converted_data.flatten.join("\n"))
384+
$net_output.write(converted_data)
385385
else
386386
STDOUT.puts(converted_data)
387387
end
388388
rescue => e
389-
STDERR.puts "ERROR: During read from STDIN: #{e.message}"
389+
STDERR.puts "ERROR: #{filename}: #{e.message}"
390390
end
391391
end
392392

0 commit comments

Comments
 (0)