Navigation Menu

Skip to content

Commit

Permalink
Support http/https/ftp as resource location
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 16, 2016
1 parent a7049c0 commit 38dfa0f
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions lib/groonga-schema/command-line/groonga-schema-diff.rb
Expand Up @@ -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"

Expand All @@ -37,8 +39,8 @@ def initialize(args)
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))
Expand Down Expand Up @@ -70,23 +72,41 @@ def parse_arguments
$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

0 comments on commit 38dfa0f

Please sign in to comment.