77require 'stringio'
88require 'uri'
99require 'zlib'
10+ require 'zip'
1011require_relative 'sqldef/version'
1112
1213module Sqldef
@@ -24,6 +25,13 @@ module Sqldef
2425 ]
2526 private_constant :COMMANDS
2627
28+ OS_ARCHIVE = {
29+ 'linux' => 'tar.gz' ,
30+ 'windows' => 'zip' ,
31+ 'darwin' => 'zip' ,
32+ }
33+ private_constant :OS_ARCHIVE
34+
2735 @bin = Dir . pwd
2836
2937 class << self
@@ -79,16 +87,26 @@ def download(command)
7987 return path if File . executable? ( path )
8088
8189 print ( "Downloading '#{ command } ' under '#{ bin } '... " )
82- resp = get ( build_url ( command ) , code : 302 ) # Latest
90+ url = build_url ( command )
91+ resp = get ( url , code : 302 ) # Latest
8392 resp = get ( resp [ 'location' ] , code : 302 ) # vX.Y.Z
8493 resp = get ( resp [ 'location' ] , code : 200 ) # Binary
8594
86- gzip = Zlib ::GzipReader . new ( StringIO . new ( resp . body ) )
87- Gem ::Package ::TarReader . new ( gzip ) do |tar |
88- unless file = tar . find { |f | f . full_name == command }
89- raise "'#{ command } ' was not found in the archive"
95+ if url . end_with? ( '.zip' )
96+ Zip ::File . open_buffer ( resp . body ) do |zip |
97+ unless entry = zip . find_entry ( command )
98+ raise "'#{ command } ' was not found in the archive"
99+ end
100+ File . binwrite ( path , zip . read ( entry ) )
101+ end
102+ else
103+ gzip = Zlib ::GzipReader . new ( StringIO . new ( resp . body ) )
104+ Gem ::Package ::TarReader . new ( gzip ) do |tar |
105+ unless file = tar . find { |f | f . full_name == command }
106+ raise "'#{ command } ' was not found in the archive"
107+ end
108+ File . binwrite ( path , file . read )
90109 end
91- File . binwrite ( path , file . read )
92110 end
93111
94112 FileUtils . chmod ( '+x' , path )
@@ -109,8 +127,10 @@ def build_url(command)
109127 raise "Unexpected sqldef command: #{ command } "
110128 end
111129 os = Etc . uname . fetch ( :sysname ) . downcase
112- arch = GOARCH . fetch ( Etc . uname . fetch ( :machine ) )
113- "https://github.com/k0kubun/sqldef/releases/latest/download/#{ command } _#{ os } _#{ arch } .tar.gz"
130+ archive = OS_ARCHIVE . fetch ( os )
131+ arch = Etc . uname . fetch ( :machine )
132+ goarch = GOARCH . fetch ( arch , arch )
133+ "https://github.com/k0kubun/sqldef/releases/latest/download/#{ command } _#{ os } _#{ goarch } .#{ archive } "
114134 end
115135
116136 # TODO: Retry transient errors
0 commit comments