Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(bug) Use ARGV instead of ARGF #51

Merged
merged 1 commit into from
Feb 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions files/json2timeseriesdb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# puppetlabs-puppet-metrics-viewer/json2graphite.rb
# puppetlabs-puppet_metrics_collector/files/json2timeseriesdb

require 'fcntl'
require 'json'
require 'time'
require 'optparse'
Expand Down Expand Up @@ -349,44 +350,43 @@ end
data_files = []
data_files += Dir.glob($options[:pattern]) if $options[:pattern]

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

while ARGF.filename != '-'
filename = ARGF.filename
data_files += [filename]
ARGF.skip
break if filename == ARGF.filename
data_files += ARGV

# Validate STDIN.

data_files_includes_stdin = data_files.include?('-')
stdin_provided = STDIN.fcntl(Fcntl::F_GETFL, 0) == 0

# Guard against waiting indefinately for non-existant STDIN.
if data_files_includes_stdin && !stdin_provided
STDERR.puts "ERROR: STDIN specified via '-' but STDIN not provided"
exit 1
end

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

# Process collected JSON files (including '-' to process data from STDIN).

data_files.each do |filename|
begin
converted_data = parse_file(filename)
if $options[:host]
$net_output.write(converted_data)
if filename == '-'
converted_data = STDIN.each_line.map { |l| parse_input( JSON.parse(l) )}.flatten.join("\n")
else
STDOUT.puts(converted_data)
converted_data = parse_file(filename)
end
rescue => e
STDERR.puts "ERROR: #{filename}: #{e.message}"
end
end

# Process JSON data from STDIN.

if ARGF.filename == '-'
begin
input = ARGF.read
converted_data = input.lines.map { |l| parse_input( JSON.parse(l) )}
if $options[:host]
$net_output.write(converted_data.flatten.join("\n"))
$net_output.write(converted_data)
else
STDOUT.puts(converted_data)
end
rescue => e
STDERR.puts "ERROR: During read from STDIN: #{e.message}"
STDERR.puts "ERROR: #{filename}: #{e.message}"
end
end

Expand Down