[Groonga-commit] groonga/groonga-schema at 38dfa0f [master] Support http/https/ftp as resource location

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Aug 16 10:56:24 JST 2016


Kouhei Sutou	2016-08-16 10:56:24 +0900 (Tue, 16 Aug 2016)

  New Revision: 38dfa0feeca79556452a9f57359e75d137a7a58f
  https://github.com/groonga/groonga-schema/commit/38dfa0feeca79556452a9f57359e75d137a7a58f

  Message:
    Support http/https/ftp as resource location

  Modified files:
    lib/groonga-schema/command-line/groonga-schema-diff.rb

  Modified: lib/groonga-schema/command-line/groonga-schema-diff.rb (+26 -6)
===================================================================
--- lib/groonga-schema/command-line/groonga-schema-diff.rb    2016-08-16 10:17:32 +0900 (322efd6)
+++ lib/groonga-schema/command-line/groonga-schema-diff.rb    2016-08-16 10:56:24 +0900 (d9d95e5)
@@ -15,6 +15,8 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 require "optparse"
+require "uri"
+require "open-uri"
 
 require "groonga/command/parser"
 
@@ -37,8 +39,8 @@ module GroongaSchema
       def run
         parse_arguments
 
-        from_schema = parse_schema(@from_path)
-        to_schema = parse_schema(@to_path)
+        from_schema = parse_schema(@from)
+        to_schema = parse_schema(@to)
         differ = GroongaSchema::Differ.new(from_schema, to_schema)
         diff = differ.diff
         $stdout.print(diff.to_groonga_command_list(:format => @format))
@@ -70,23 +72,41 @@ module GroongaSchema
           $stderr.puts(parser.help)
           exit(false)
         end
-        @from_path, @to_path = rest_args
+        @from, @to = rest_args
       end
 
-      def parse_schema(path)
-        File.open(path) do |file|
+      def parse_schema(resource_path)
+        open_resource(resource_path) do |resource|
           schema = GroongaSchema::Schema.new
           parser = Groonga::Command::Parser.new
           parser.on_command do |command|
             schema.apply_command(command)
           end
-          file.each_line do |line|
+          resource.each_line do |line|
             parser << line
           end
           parser.finish
           schema
         end
       end
+
+      def open_resource(resource_path)
+        uri = nil
+        begin
+          uri = URI.parse(resource_path)
+        rescue URI::InvalidURIError
+        end
+
+        if uri and uri.respond_to?(:open)
+          uri.open do |response|
+            yield(response)
+          end
+        else
+          File.open(resource_path) do |file|
+            yield(file)
+          end
+        end
+      end
     end
   end
 end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index