From 619f937dee0424a33f72e9bb7f7e0f4d8647a217 Mon Sep 17 00:00:00 2001
From: Steve Purcell <steve@sanityinc.com>
Date: Wed, 7 Oct 2015 07:39:35 +1300
Subject: [PATCH] Support input from stdin

Closes #2.
---
 bin/sqlint | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/bin/sqlint b/bin/sqlint
index 68bb429..350a1d0 100755
--- a/bin/sqlint
+++ b/bin/sqlint
@@ -7,7 +7,7 @@ require 'optparse'
 
 options = { limit: 1000 }
 optparse = OptionParser.new do |opts|
-  opts.banner = "Usage: #{File.basename($0)} [options] file.sql ..."
+  opts.banner = "Usage: #{File.basename($0)} [options] [file.sql ...]"
   opts.separator ""
   opts.separator "Options:"
   opts.on("--limit=N", Integer, "Limit checking to N errors (default: #{options[:limit]})") do |n|
@@ -23,10 +23,6 @@ optparse = OptionParser.new do |opts|
   end
 end
 optparse.parse!(ARGV)
-if ARGV.empty?
-  puts optparse
-  exit 1
-end
 
 ERROR_TYPES = {error: "ERROR", warning: "WARNING"}
 
@@ -51,15 +47,25 @@ def display_lint(lint)
   end
 end
 
-saw_errors = false
-ARGV.each do |filename|
-  File.open(filename, 'r') do |file|
-    results = SQLint::Linter.new(filename, file).run.first(options[:limit])
-    results.each do |lint|
-      display_lint(lint)
+def each_input_file(&block)
+  if ARGV.empty?
+    yield [STDIN, "stdin"]
+  else
+    ARGV.each do |filename|
+      File.open(filename, 'r') do |file|
+        yield [file, filename]
+      end
     end
-    saw_errors ||= results.any? { |lint| lint.type == :error }
   end
 end
 
+saw_errors = false
+each_input_file do |file, filename|
+  results = SQLint::Linter.new(filename, file).run.first(options[:limit])
+  results.each do |lint|
+    display_lint(lint)
+  end
+  saw_errors ||= results.any? { |lint| lint.type == :error }
+end
+
 exit 1 if saw_errors
-- 
GitLab