7
7
require 'stringio'
8
8
require 'uri'
9
9
require 'zlib'
10
+ require 'zip'
10
11
require_relative 'sqldef/version'
11
12
12
13
module Sqldef
@@ -24,6 +25,13 @@ module Sqldef
24
25
]
25
26
private_constant :COMMANDS
26
27
28
+ OS_ARCHIVE = {
29
+ 'linux' => 'tar.gz' ,
30
+ 'windows' => 'zip' ,
31
+ 'darwin' => 'zip' ,
32
+ }
33
+ private_constant :OS_ARCHIVE
34
+
27
35
@bin = Dir . pwd
28
36
29
37
class << self
@@ -79,16 +87,26 @@ def download(command)
79
87
return path if File . executable? ( path )
80
88
81
89
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
83
92
resp = get ( resp [ 'location' ] , code : 302 ) # vX.Y.Z
84
93
resp = get ( resp [ 'location' ] , code : 200 ) # Binary
85
94
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 )
90
109
end
91
- File . binwrite ( path , file . read )
92
110
end
93
111
94
112
FileUtils . chmod ( '+x' , path )
@@ -109,8 +127,10 @@ def build_url(command)
109
127
raise "Unexpected sqldef command: #{ command } "
110
128
end
111
129
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 } "
114
134
end
115
135
116
136
# TODO: Retry transient errors
0 commit comments