ghcurl 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +21 -15
  3. data/bin/ghcurl +369 -0
  4. data/lib/ghcurl.rb +65 -0
  5. metadata +49 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1c45d8416cf634acf751c84dfbed618f3d60b40c886f2eb8f0d5827a687e3ffa
4
- data.tar.gz: a9ad039e7dd52435f09a6650b6a8dd8dab1f9d9e78f455147de812a427dde14b
3
+ metadata.gz: 9b4840d95efdb75df9837f357689f9a4e1de17d22149d95a0843d26925923341
4
+ data.tar.gz: 13f087583da4f41ff0e94be251e74fe565981e8c01e73867a1be0e25ab88065f
5
5
  SHA512:
6
- metadata.gz: cf0588b0f92a469aee6e6d996efa199f232dc95a348e960e43492482c5d1205a34ff6ff786444bcc2fa9c6ea755629bbad70d44127f83d7932a26830563fa6f9
7
- data.tar.gz: 3df90ee0ee3fad0505b0d2c5ac36923cf4ccd767c83be93cf328468dc67ced1f8ee63558b2faf2d62d3fd8a6d3ca55a1fec60345745978b241ed59e42c2a19b8
6
+ metadata.gz: be2b7c9bd8de0b137d8bdd07d49bdb9a30a3906e8034e87ac850047809ba2ec6dc24909b82feed5a53550532001222c0ae999e7441af27322c9ea34d18930289
7
+ data.tar.gz: 19d2d408baa93e56c2ce1145b7e2319283f9a18053f0cadf1fe46c9d4139356a7802794c534d5a4b6362189884e0089b2b0248781d800896e550cec1ea2e10a7
data/README.md CHANGED
@@ -16,28 +16,34 @@ Download files (and install) from Github releases.
16
16
 
17
17
  ## Usage
18
18
 
19
+ Download latest deb/rpm package and install, notice the argument `deb` / `rpm` are just regular expressions.
19
20
  ```bash
20
- # Download latest timeleft to ~/.cache/ghcurl
21
- ghcurl BetaPictoris/timeleft timeleft
22
-
23
- # Download timeleft version 1.1.0
24
- ghcurl BetaPictoris/timeleft timeleft -v1.1.0
25
-
21
+ ghcurl cli/cli deb -i
22
+ ghcurl cli/cli rpm -i
23
+ ```
26
24
 
27
- # Download deb/rpm and install it
28
- ghcurl cli/cli gh_2.7.0_linux_amd64.deb -i
29
- ghcurl cli/cli gh_2.7.0_linux_amd64.rpm -i
25
+ Normal download
26
+ ```bash
27
+ # Download latest rbspy-x86_64-unknown-linux-gnu.tar.gz to ~/.cache/ghcurl
28
+ ghcurl rbspy/rbspy x86_64.*linux
30
29
 
30
+ # Download rbspy version 0.11.1
31
+ ghcurl rbspy/rbspy 'x86_64.*linux' -v0.11.1
32
+ ```
31
33
 
32
- # Download a binary and install it
34
+ Download a binary and install it to anywhere
35
+ ```bash
36
+ # Install to /usr/local/bin
33
37
  ghcurl BetaPictoris/timeleft timeleft -i
34
38
 
35
- # Download and install it to a path you like
39
+ # Install to ~/tmp/bin
36
40
  ghcurl BetaPictoris/timeleft timeleft -i ~/tmp/bin
37
41
 
38
- # Install and rename it to what you like
39
- ghcurl BetaPictoris/timeleft timeleft -i tl
42
+ # Install and rename it to, here, 'gd' in /usr/local/bin
43
+ ghcurl dlvhdr/gh-dash linux-amd64 -i gd
40
44
 
41
- # Or, like this
42
- ghcurl BetaPictoris/timeleft timeleft -i ~/tmp/bin/tl
45
+ # or, like this
46
+ ghcurl dlvhdr/gh-dash linux-amd64 -i ~/tmp/bin/gd
43
47
  ```
48
+
49
+ <br>
data/bin/ghcurl ADDED
@@ -0,0 +1,369 @@
1
+ #!/usr/bin/env ruby
2
+ # ------------------------------------------------------
3
+ # File : ghcurl.rb
4
+ # Authors : ccmywish <ccmywish@qq.com>
5
+ # Created on : <2022-04-12>
6
+ # Last modified : <2022-04-14>
7
+ #
8
+ # ghcurl:
9
+ #
10
+ # Download files (and install) from Github releases
11
+ #
12
+ # ------------------------------------------------------
13
+
14
+ require 'ghcurl'
15
+ require 'nokogiri'
16
+ require 'open-uri'
17
+ require 'highline'
18
+ require 'cliswitch'
19
+
20
+ module Ghcurl
21
+
22
+ WAREHOUSE = File.expand_path("~/.cache/ghcurl")
23
+ BIN_PATH = ENV['GHCURL_BIN_PATH'] || "/usr/local/bin"
24
+ HL = HighLine.new
25
+
26
+ class Error < StandardError; end
27
+
28
+
29
+ def bold(str) "\e[1m#{str}\e[0m" end
30
+ def green(str) "\e[32m#{str}\e[0m" end
31
+ def blue(str) "\e[34m#{str}\e[0m" end
32
+
33
+
34
+ def log(msg)
35
+ puts blue(bold("ghcurl: #{msg}"))
36
+ end
37
+
38
+
39
+ def curl(url, name)
40
+ #if !test('d', WAREHOUSE)
41
+ # require 'fileutils'
42
+ # FileUtils.mkdir_p(WAREHOUSE)
43
+ #end
44
+ cmd = "curl -L #{url} --create-dirs -o #{WAREHOUSE}/#{name}"
45
+ system cmd
46
+ log "Downloaded to #{WAREHOUSE}/#{name}"
47
+ end
48
+
49
+
50
+ def get_filters
51
+
52
+ arches = [
53
+ ['amd64', 'x86_64', 'x64'],
54
+ ['386', 'i686'],
55
+ ['arm64', 'aarch64'],
56
+ ['armv6', 'arm'],
57
+ ]
58
+
59
+ oses = [
60
+ ['linux'],
61
+ ['mac', 'apple'],
62
+ ['windows']
63
+ ]
64
+
65
+ fs = RUBY_PLATFORM.split('-')
66
+ os = ""
67
+ arch = ""
68
+ fs.each do |f|
69
+ case f
70
+ when 'linux' then os = 'linux'
71
+ when 'mac','macOS' then os = 'mac'
72
+ when 'ucrt', 'mingw', 'windows' then os = 'windows'
73
+ when 'x86_64' then arch = 'x86_64'
74
+ when 'x86' then arch = 'x86'
75
+ when 'amd64' then arch = 'amd64'
76
+ when 'arm64' then arch = 'arm64'
77
+ when 'armv6' then arch = 'armv6'
78
+ end
79
+ end
80
+
81
+ approval, refuse = [], []
82
+ # [os, arch].each { approval << _1 unless _1.nil? }
83
+
84
+ if os
85
+ i = oses.each_with_index do |type, index|
86
+ break index if type.include? os
87
+ end
88
+ if !i.nil?
89
+ tmp = oses.dup
90
+ tmp.delete_at(i)
91
+ refuse.concat tmp.flatten
92
+ end
93
+ end
94
+
95
+
96
+ if arch
97
+ i = arches.each_with_index do |type, index|
98
+ break index if type.include? arch
99
+ end
100
+ if !i.nil?
101
+ tmp = arches.dup
102
+ tmp.delete_at(i)
103
+ refuse.concat tmp.flatten
104
+ end
105
+ end
106
+
107
+ # Debug
108
+ # puts "=> Approval and refuse"
109
+ # p [os, arch]
110
+ # p refuse
111
+
112
+ # Now we only refuse others
113
+ return refuse
114
+
115
+ end
116
+
117
+
118
+ def download(repo, regexp, version: nil)
119
+
120
+ if repo =~ /^https:\/\/github.com/
121
+ require 'uri'
122
+ uri = URI(repo)
123
+ # index 1, take 2
124
+ repo = uri.path.split('/')[1,2].join('/')
125
+ elsif !repo.include?('/')
126
+ got_repo = DEFAULT_WARES[repo.to_sym]
127
+ if not got_repo
128
+ user = HL.ask "Who developed the awesome '#{repo}' ? "
129
+ repo = user + '/' + repo
130
+ else
131
+ repo = got_repo
132
+ log "Use the popular repo #{repo}"
133
+ end
134
+ end
135
+
136
+ log "checking..."
137
+ unless version
138
+ doc = Nokogiri::HTML5 URI.open("https://github.com/#{repo}/releases/latest")
139
+ else
140
+ doc = Nokogiri::HTML5 URI.open("https://github.com/#{repo}/releases/tag/v#{version}")
141
+ end
142
+
143
+
144
+ links = doc.css("li>a[href^='/#{repo}/releases/download']")
145
+ if links.empty?
146
+ puts doc.css('li a').map(&:to_s)
147
+ log <<~EOE
148
+ The search result is empty, check the args:
149
+ repo: #{repo}
150
+ version: #{version ? version:'nil'}
151
+
152
+ EOE
153
+ puts
154
+ end
155
+
156
+
157
+ links = links.map { _1['href'] }
158
+
159
+
160
+ if regexp
161
+ filtered = links.select do
162
+ _1 =~ /#{regexp}/
163
+ end
164
+ else
165
+ refuse = get_filters()
166
+ filtered = links.select do |l|
167
+ refuse.all? do |f|
168
+ l !~ /#{f}/
169
+ end
170
+ end
171
+ end
172
+
173
+
174
+ if filtered.size == 1
175
+ link = filtered[0].split('/').last
176
+ else
177
+ if filtered.size == 0
178
+ links_for_user = links.map { _1.split('/').last }
179
+ else
180
+ links_for_user = filtered.map { _1.split('/').last }
181
+ end
182
+ link = HL.choose do |menu|
183
+ menu.index_color = :rgb_77bbff
184
+ menu.prompt = "Which one do you want to download? "
185
+ menu.choices( *links_for_user )
186
+ end
187
+ end
188
+
189
+ url = "https://github.com" + links[0].split('/')[0..-2].join('/') + '/' + link
190
+
191
+ log "Downloading #{url}"
192
+
193
+ $downloaded = link.split('/').last
194
+
195
+ curl(url, $downloaded)
196
+
197
+ end
198
+
199
+
200
+ def install(ware, place: BIN_PATH)
201
+
202
+ ware_name = ''
203
+ if ware.include?('/')
204
+ ware_name = ware.split('/').last
205
+ else
206
+ ware_name = ware
207
+ end
208
+
209
+ if (! $downloaded.include?(ware_name)) and (!$downloaded.end_with?('.deb')) and (!$downloaded.end_with?('.rpm'))
210
+ log "Do you want to rename or install it to a different path?"
211
+ re = HL.ask "Input path or name, or just enter to say no."
212
+ if !re.empty?
213
+ place = re
214
+ end
215
+ end
216
+
217
+ case RUBY_PLATFORM
218
+ when /ucrt/i, /mingw/i
219
+ install_on_windows($downloaded, place)
220
+ else
221
+ install_on_nix($downloaded, place)
222
+ end
223
+ log "Install finish!"
224
+ end
225
+
226
+
227
+ def install_on_nix(ware, place)
228
+
229
+ target = "#{WAREHOUSE}/#{ware}"
230
+
231
+ if target.end_with?('.deb')
232
+ log "Install deb package for you"
233
+ system "sudo dpkg -i #{target}"
234
+ return
235
+ end
236
+
237
+ if target.end_with?('.rpm')
238
+ log "Install rpm package for you"
239
+ system "sudo rpm -i #{target}"
240
+ return
241
+ end
242
+
243
+
244
+ if test 'd', place
245
+ cmd = "sudo install -Dt #{place} -m 755 #{target} "
246
+ system cmd
247
+ log "Install #{ware} to " + place
248
+
249
+ else
250
+ unless place.include?('/')
251
+ # User just give it another name
252
+ place = BIN_PATH + '/' + place
253
+ else
254
+ # User give it a path and its name
255
+ end
256
+ log "Installed as " + place
257
+ system "sudo cp #{target} #{place}"
258
+ system "sudo chmod +x #{place}"
259
+ end
260
+
261
+ end
262
+
263
+
264
+ def install_on_windows
265
+ log "Sorry, not implemented yet!"
266
+ end
267
+
268
+
269
+ def list_wares
270
+
271
+ puts blue("In #{WAREHOUSE}")
272
+ puts
273
+ Dir.children(WAREHOUSE).each_with_index do |dict,i|
274
+ puts " #{blue(i+1)}. #{bold(green(dict))}"
275
+ end
276
+ puts
277
+ end
278
+
279
+
280
+ def help
281
+ puts <<~EOC
282
+ ghcurl (v#{VERSION}): Download files and install from Github releases
283
+
284
+ Default install to env 'GHCURL_BIN_PATH' or /usr/local/bin
285
+
286
+ usage:
287
+ ghcurl [user]/repo [regexp] => Search latest version with regexp to download
288
+ ghcurl repo [re] -v tag => Download a specific tag version
289
+ ghcurl repo [deb/rpm] => Download and install deb/rpm package
290
+ ghcurl repo [re] -i [path] => Download and install to path
291
+ ghcurl repo [re] -i name => Download and install as 'name'
292
+ ghcurl -l => List downloaded files
293
+ ghcurl -h => Print this help
294
+
295
+ example:
296
+ ghcurl bat => Search sharkdp/bat the latest
297
+ ghcurl cli deb -i => Search cli/cli /deb/
298
+ ghcurl rbspy/rbspy 'x86_64.*linux' -v0.11.1
299
+
300
+ EOC
301
+ end
302
+
303
+ end
304
+
305
+
306
+
307
+ ####################
308
+ # main: CLI Handling
309
+ ####################
310
+ extend Ghcurl
311
+
312
+ CO = CliSwitch::Option
313
+ op_config = [] << CO.new(name: 'install', short: '-i', arg_required: 'optional') <<
314
+ CO.new(name: 'version', short: '-v', arg_required: 'required') <<
315
+ CO.new(name: 'help', short: '-h', long: '--help', arg_required: 'noarg') <<
316
+ CO.new(name: 'list', short: '-l', arg_required: 'noarg')
317
+
318
+ cli = CliSwitch.new(op_config)
319
+ args, opts = cli.parse(ARGV)
320
+
321
+ if args.empty? and opts.empty?
322
+ help
323
+ exit
324
+ end
325
+
326
+ if args.size >= 2
327
+ repo_or_name, regexp = args[0], args[1]
328
+ else
329
+ repo_or_name, regexp = args[0], nil
330
+ end
331
+
332
+
333
+ version = nil
334
+ need_install = false
335
+ install_to = nil
336
+
337
+ opts.each do
338
+ case _1.name
339
+ when 'help'
340
+ help
341
+ exit
342
+ when 'list'
343
+ list_wares
344
+ exit
345
+ when 'version'
346
+ version = _1.next_arg
347
+ when 'install'
348
+ need_install = true
349
+ install_to = _1.next_arg
350
+ end
351
+ end
352
+
353
+ # Debug
354
+ # p repo_or_name
355
+ # p regexp
356
+ # p need_install
357
+ # p install_to
358
+
359
+ begin
360
+ download(repo_or_name, regexp, version: version)
361
+ if need_install
362
+ if install_to
363
+ install(repo_or_name, place: install_to)
364
+ else
365
+ install(repo_or_name)
366
+ end
367
+ end
368
+ rescue Interrupt
369
+ end
data/lib/ghcurl.rb ADDED
@@ -0,0 +1,65 @@
1
+ # ------------------------------------------------------
2
+ # File : ghcurl.rb
3
+ # Authors : ccmywish <ccmywish@qq.com>
4
+ # Created on : <2022-04-12>
5
+ # Last modified : <2022-04-14>
6
+ #
7
+ # ghcurl:
8
+ #
9
+ # This is the lib file.
10
+ #
11
+ # ------------------------------------------------------
12
+
13
+ module Ghcurl
14
+
15
+ VERSION = "0.5.0"
16
+
17
+ end
18
+
19
+ #
20
+ # We consider from this:
21
+ #
22
+ # https://github.com/ibraheemdev/modern-unix
23
+ #
24
+ Ghcurl::DEFAULT_WARES = {
25
+
26
+ cli: 'cli/cli',
27
+
28
+ fd: 'sharkdp/fd',
29
+ bat: 'sharkdp/bat',
30
+ hyperfine: 'sharkdp/hyperfine',
31
+ hexyl: 'sharkdp/hexyl',
32
+
33
+ exa: 'ogham/exa',
34
+ dog: 'ogham/dog',
35
+
36
+ lsd: 'Peltoche/lsd',
37
+ delta: 'dandavison/delta',
38
+ dust: 'bootandy/dust',
39
+ duf: 'muesli/duf',
40
+ broot: 'Canop/broot',
41
+
42
+ ripgrep: 'BurntSushi/ripgrep',
43
+ rg: 'BurntSushi/ripgrep',
44
+ ag: 'ggreer/the_silver_searcher',
45
+
46
+ fzf: 'junegunn/fzf',
47
+ fzy: 'jhawthorn/fzy',
48
+
49
+ mcfly: 'cantino/mcfly',
50
+ choose: 'theryangeary/choose',
51
+ jq: 'stedolan/jq',
52
+ sd: 'chmln/sd',
53
+ cheat: 'cheat/cheat',
54
+ bottom: 'ClementTsang/bottom',
55
+ glances: 'nicolargo/glances',
56
+ gtop: 'aksakalli/gtop',
57
+ gping: 'orf/gping',
58
+ procs: 'dalance/procs',
59
+ httpie: 'httpie/httpie',
60
+ curlie: 'rs/curlie',
61
+ xh: 'ducaale/xh',
62
+ zoxide: 'ajeetdsouza/zoxide',
63
+
64
+ scc: 'boyter/scc'
65
+ }
metadata CHANGED
@@ -1,19 +1,62 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ghcurl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ccmywish
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-12 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-04-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: highline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: cliswitch
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.2'
13
55
  description: Download files (and install) from Github releases.
14
56
  email:
15
57
  - ccmywish@qq.com
16
- executables: []
58
+ executables:
59
+ - ghcurl
17
60
  extensions: []
18
61
  extra_rdoc_files: []
19
62
  files:
@@ -21,6 +64,8 @@ files:
21
64
  - LICENSE
22
65
  - README.md
23
66
  - Rakefile
67
+ - bin/ghcurl
68
+ - lib/ghcurl.rb
24
69
  homepage: https://github.com/ccmywish/ghcurl
25
70
  licenses:
26
71
  - MIT