ghcurl 0.2.0 → 0.4.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 (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +49 -13
  3. data/bin/ghcurl +232 -0
  4. metadata +19 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe02eff016042725d2f43dc155b0139b786d09d82c7fdd57ad043733e99aa1b0
4
- data.tar.gz: ca8e0788682b7e984bfa64f611d805edd8621213bd72163663a1b154dd53e664
3
+ metadata.gz: 93c6f4a7b36d196072151ee1b8b62785072dac59291080436ff9d387e387e966
4
+ data.tar.gz: 6f441d27a1d0daa7ba05ed44dec04f436286eac3a5b2d2621d76c340eb442b2b
5
5
  SHA512:
6
- metadata.gz: be6714803118ebeec929f7e5e4c769f6e8fba56b6e1ccc959fa59af1058be68a9f11a8c2263541194f03b46715e0a9432e41b9496478e529926498a7212bf5a6
7
- data.tar.gz: 5af8bc919be4cc686df9c6270d7797d84ad09d5e9d803870b341e20b0f29dc52f8be618b0393a8edc76dca9259dcb1d9e7aa0ed1bc09d64e0970f48ef636ecea
6
+ metadata.gz: 66263a60ee4ada61f3901bad75ec026994ccffa9839962c18aa48c6e25212bc74d16e1aa402a2c4b6e50f638d2f6efd453c048ef231bed0f9cc85cb5029402c2
7
+ data.tar.gz: 60544aa30cbc50533763400c9e4296ff4017912585fd0d8adad24bdbf65d88f89557c358de012f768ca2a2b7467d409d692bc17256b6da4ffafb705383f97a83
data/README.md CHANGED
@@ -1,31 +1,67 @@
1
- # ghcurl
1
+ <div align="center">
2
2
 
3
- Download files (and install) from Github releases.
3
+ # ghcurl
4
4
 
5
- ## Installation
5
+ [![Gem Version](https://badge.fury.io/rb/ghcurl.svg)](https://rubygems.org/gems/ghcurl)
6
6
 
7
7
  ```bash
8
8
  gem install ghcurl
9
9
  ```
10
10
 
11
+ <br>
12
+
13
+ Download files (and install) from Github releases.
14
+
15
+ </div>
16
+
11
17
  ## Usage
12
18
 
19
+ Download latest deb/rpm package and install, notice the argument `deb` / `rpm` are just regular expressions.
20
+ ```bash
21
+ ghcurl cli/cli deb -i
22
+ ghcurl cli/cli rpm -i
23
+ ```
24
+
25
+ Normal download
13
26
  ```bash
14
- # Download latest timeleft to ~/.cache/ghcurl
15
- ghcurl BetaPictoris/timeleft timeleft
27
+ # Download latest rbspy-x86_64-unknown-linux-gnu.tar.gz to ~/.cache/ghcurl
28
+ ghcurl rbspy/rbspy x86_64.*linux
16
29
 
17
- # Download timeleft version 1.1.0
18
- ghcurl BetaPictoris/timeleft timeleft -v1.1.0
30
+ # Download rbspy version 0.11.1
31
+ ghcurl rbspy/rbspy 'x86_64.*linux' -v0.11.1
32
+ ```
19
33
 
20
- # Download and install it
34
+ Download a binary and install it to anywhere
35
+ ```bash
36
+ # Install to /usr/local/bin
21
37
  ghcurl BetaPictoris/timeleft timeleft -i
22
38
 
23
- # Download and install it to a path you like
39
+ # Install to ~/tmp/bin
24
40
  ghcurl BetaPictoris/timeleft timeleft -i ~/tmp/bin
25
41
 
26
- # Install and rename it to what you like
27
- 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
28
44
 
29
- # Or, like this
30
- ghcurl BetaPictoris/timeleft timeleft -i ~/tmp/bin/tl
45
+ # or, like this
46
+ ghcurl dlvhdr/gh-dash linux-amd64 -i ~/tmp/bin/gd
31
47
  ```
48
+
49
+ <br>
50
+
51
+ ## How does it works?
52
+
53
+ For future contributers and myself, I'll tell you how to maintain this.
54
+
55
+ In `ghcurl` previous to `0.3.1`, I write this code piece:
56
+ ```ruby
57
+ unless version
58
+ # Notice: releases/tag/vx.x.x/download not works!!!
59
+ # This is a link relocation
60
+ url = "https://github.com/#{repo}/releases/latest/download/#{ware}"
61
+ else
62
+ # The real download link
63
+ url = "https://github.com/#{repo}/releases/download/v#{version}/#{ware}"
64
+ end
65
+ ```
66
+
67
+ As you can see, it considers the Github router when you try to install a **latest** one, which are very unreliable, and you should specify a precise name for the thing you want to download. So from `0.4.0` I changed this via a slow but useful method: check the HTML file in the release page, and search the string user request which is not necessarily accurate, to find the download link directly.
data/bin/ghcurl ADDED
@@ -0,0 +1,232 @@
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-13>
7
+ #
8
+ # ghcurl:
9
+ #
10
+ # Download files (and install) from Github releases
11
+ #
12
+ # ------------------------------------------------------
13
+
14
+ require 'nokogiri'
15
+ require 'open-uri'
16
+
17
+ module Ghcurl
18
+
19
+ VERSION = "0.4.0"
20
+ WAREHOUSE = File.expand_path("~/.cache/ghcurl")
21
+ BIN_PATH = "/usr/local/bin"
22
+
23
+ class Error < StandardError; end
24
+
25
+
26
+ def bold(str) "\e[1m#{str}\e[0m" end
27
+ def green(str) "\e[32m#{str}\e[0m" end
28
+ def blue(str) "\e[34m#{str}\e[0m" end
29
+
30
+
31
+ def log(msg)
32
+ puts blue(bold("ghcurl: #{msg}"))
33
+ end
34
+
35
+
36
+ def curl(url, name)
37
+ #if !test('d', WAREHOUSE)
38
+ # require 'fileutils'
39
+ # FileUtils.mkdir_p(WAREHOUSE)
40
+ #end
41
+ cmd = "curl -L #{url} --create-dirs -o #{WAREHOUSE}/#{name}"
42
+ system cmd
43
+ log "Downloaded to #{WAREHOUSE}/#{name}"
44
+ end
45
+
46
+
47
+ def download(repo, ware, version: nil)
48
+
49
+ if repo =~ /^https:\/\/github.com/
50
+ require 'uri'
51
+ uri = URI(repo)
52
+ # index 1, take 2
53
+ repo = uri.path.split('/')[1,2].join('/')
54
+ end
55
+
56
+ log "checking..."
57
+ unless version
58
+ doc = Nokogiri::HTML5 URI.open("https://github.com/#{repo}/releases/latest")
59
+ else
60
+ doc = Nokogiri::HTML5 URI.open("https://github.com/#{repo}/releases/tag/v#{version}")
61
+ end
62
+
63
+
64
+ links = doc.css("li>a[href^='/#{repo}/releases/download']")
65
+ if links.empty?
66
+ puts doc.css('li a').map(&:to_s)
67
+ log <<~EOE
68
+ The search result is empty, check the args:
69
+ repo: #{repo}
70
+ version: #{version ? version:'nil'}
71
+ regexp: #{ware}
72
+ EOE
73
+ puts
74
+ end
75
+
76
+ link = links.each do
77
+ break _1['href'] if _1['href'] =~ /#{ware}/
78
+ end
79
+
80
+ url = "https://github.com" + link
81
+
82
+ log "Downloading #{url}"
83
+
84
+ $downloaded = link.split('/').last
85
+
86
+ curl(url, $downloaded)
87
+
88
+ end
89
+
90
+ def install(ware, place: BIN_PATH)
91
+ case RUBY_PLATFORM
92
+ when /ucrt/i, /mingw/i
93
+ install_on_windows(ware, place)
94
+ else
95
+ install_on_nix(ware, place)
96
+ end
97
+ log "Install finish!"
98
+ end
99
+
100
+
101
+ def install_on_nix(ware, place)
102
+
103
+ target = "#{WAREHOUSE}/#{ware}"
104
+
105
+ if target.end_with?('.deb')
106
+ log "Install deb package for you"
107
+ system "sudo dpkg -i #{target}"
108
+ return
109
+ end
110
+
111
+ if target.end_with?('.rpm')
112
+ log "Install rpm package for you"
113
+ system "sudo rpm -i #{target}"
114
+ return
115
+ end
116
+
117
+
118
+ if test 'd', place
119
+ cmd = "sudo install -Dt #{place} -m 755 #{target} "
120
+ system cmd
121
+ log "Install #{ware} to " + place
122
+
123
+ else
124
+ unless place.include?('/')
125
+ # User just give it another name
126
+ place = BIN_PATH + '/' + place
127
+ else
128
+ # User give it a path and its name
129
+ end
130
+ log "Installed as " + place
131
+ system "sudo cp #{target} #{place}"
132
+ system "sudo chmod +x #{place}"
133
+ end
134
+
135
+ end
136
+
137
+
138
+ def install_on_windows
139
+ end
140
+
141
+
142
+ def list_wares
143
+
144
+ puts blue("In #{WAREHOUSE}")
145
+ puts
146
+ Dir.children(WAREHOUSE).each_with_index do |dict,i|
147
+ puts " #{blue(i+1)}. #{bold(green(dict))}"
148
+ end
149
+ puts
150
+ end
151
+
152
+
153
+ def help
154
+ puts <<~EOC
155
+ ghcurl (v#{VERSION}): Download files (and install) from Github releases
156
+
157
+ usage:
158
+ ghcurl repo_url regexp => Search in repo_url the latest to download
159
+ ghcurl user/repo regexp => Search in user/repo the latest to download
160
+ ghcurl url re -v tag => Download a specific tag version
161
+ ghcurl url re deb/rpm => Download and install deb/rpm package
162
+ ghcurl url re -i [path] => Download and install to /usr/local/bin
163
+ ghcurl url re -i name => Download and install as 'name'
164
+ ghcurl -l => List downloaded files
165
+ ghcurl -h => Print this help
166
+
167
+ example:
168
+ ghcurl rbspy/rbspy 'x86_64.*linux' -v0.11.1
169
+ ghcurl cli/cli deb -i
170
+
171
+ EOC
172
+ end
173
+
174
+ end
175
+
176
+
177
+
178
+ ####################
179
+ # main: CLI Handling
180
+ ####################
181
+ extend Ghcurl
182
+
183
+ # e.g. ccmywish/binary
184
+ gh_repo, ware = ARGV.shift(2)
185
+ case gh_repo
186
+ when '-l' then list_wares ; exit
187
+ when '-h', '--help' then help ; exit
188
+ when nil then help ; exit
189
+ end
190
+
191
+ if ware.nil?
192
+ log "ghcurl: Download what?"
193
+ log " use: ghcurl repo file"
194
+ exit
195
+ end
196
+
197
+
198
+ arg = ARGV.shift
199
+
200
+ next_op = true
201
+
202
+ case arg
203
+ when '-v'
204
+ download gh_repo, ware, version: ARGV.shift
205
+ when /-v\d.*/
206
+ v = arg.split('-v')[1]
207
+ download gh_repo, ware, version: v
208
+ else
209
+ download gh_repo, ware
210
+ next_op = false
211
+ end
212
+
213
+
214
+ if next_op
215
+ next_op = ARGV.shift
216
+ else
217
+ next_op = arg
218
+ end
219
+
220
+
221
+ case next_op
222
+ when '-i'
223
+ place = ARGV.shift
224
+ if place.nil?
225
+ install($downloaded)
226
+ else
227
+ install($downloaded, place: place)
228
+ end
229
+ when /-i.*/
230
+ place = next_op.split('-i')[1]
231
+ install($downloaded, place: place)
232
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ghcurl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ccmywish
@@ -9,11 +9,26 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2022-04-12 00:00:00.000000000 Z
12
- dependencies: []
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'
13
27
  description: Download files (and install) from Github releases.
14
28
  email:
15
29
  - ccmywish@qq.com
16
- executables: []
30
+ executables:
31
+ - ghcurl
17
32
  extensions: []
18
33
  extra_rdoc_files: []
19
34
  files:
@@ -21,6 +36,7 @@ files:
21
36
  - LICENSE
22
37
  - README.md
23
38
  - Rakefile
39
+ - bin/ghcurl
24
40
  homepage: https://github.com/ccmywish/ghcurl
25
41
  licenses:
26
42
  - MIT