ghcurl 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/ghcurl +196 -0
  3. metadata +4 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1c45d8416cf634acf751c84dfbed618f3d60b40c886f2eb8f0d5827a687e3ffa
4
- data.tar.gz: a9ad039e7dd52435f09a6650b6a8dd8dab1f9d9e78f455147de812a427dde14b
3
+ metadata.gz: 3771f9aa9548747795af7e008d722600ba0fff7c1e0eb5bfd18a8584ea318a10
4
+ data.tar.gz: 934f5db9d22eedb5a2542d32db9a90a39848cdd3317e598362f2155fbb5365b8
5
5
  SHA512:
6
- metadata.gz: cf0588b0f92a469aee6e6d996efa199f232dc95a348e960e43492482c5d1205a34ff6ff786444bcc2fa9c6ea755629bbad70d44127f83d7932a26830563fa6f9
7
- data.tar.gz: 3df90ee0ee3fad0505b0d2c5ac36923cf4ccd767c83be93cf328468dc67ced1f8ee63558b2faf2d62d3fd8a6d3ca55a1fec60345745978b241ed59e42c2a19b8
6
+ metadata.gz: feb421e23ab902a6b6feecfe8420f8b6a3cfe4ab674fbba7730efa7c6495a89af967b257097a897a7c68fee5892b7710b7a61f1d7a46e2f8540b4e1657717450
7
+ data.tar.gz: 21d1b9d1bc3e634866fdc691dca923d77e8ba2f9c04ebd86e9ce08ae9d86ec82a742b35faa19f0236790f5e9cea77cfcc8d1183eae7917adf83a1b8590d4eaf1
data/bin/ghcurl ADDED
@@ -0,0 +1,196 @@
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-12>
7
+ #
8
+ # ghcurl:
9
+ #
10
+ # Download files (and install) from Github releases
11
+ #
12
+ # ------------------------------------------------------
13
+
14
+ module Ghcurl
15
+
16
+ VERSION = "0.3.1"
17
+ WAREHOUSE = File.expand_path("~/.cache/ghcurl")
18
+ BIN_PATH = "/usr/local/bin"
19
+
20
+ class Error < StandardError; end
21
+
22
+
23
+ def curl(url, ware)
24
+ #if !test('d', WAREHOUSE)
25
+ # require 'fileutils'
26
+ # FileUtils.mkdir_p(WAREHOUSE)
27
+ #end
28
+ cmd = "curl -L #{url} --create-dirs -o #{WAREHOUSE}/#{ware}"
29
+ system cmd
30
+ puts "ghcurl: Downloaded to #{WAREHOUSE}/#{ware}"
31
+ end
32
+
33
+
34
+ def download(repo, ware, version: nil)
35
+
36
+ if repo =~ /^https:\/\/github.com/
37
+ require 'uri'
38
+ uri = URI(repo)
39
+ # index 1, take 2
40
+ repo = uri.path.split('/')[1,2].join('/')
41
+ end
42
+
43
+ unless version
44
+ # Notice: releases/tag/vx.x.x/download not works!!!
45
+ # This is a link relocation
46
+ url = "https://github.com/#{repo}/releases/latest/download/#{ware}"
47
+ else
48
+ # The real download link
49
+ url = "https://github.com/#{repo}/releases/download/v#{version}/#{ware}"
50
+ end
51
+
52
+ puts url
53
+
54
+ curl(url, ware)
55
+
56
+ end
57
+
58
+ def install(ware, place: BIN_PATH)
59
+ case RUBY_PLATFORM
60
+ when /ucrt/i, /mingw/i
61
+ install_on_windows(ware, place)
62
+ else
63
+ install_on_nix(ware, place)
64
+ end
65
+ end
66
+
67
+
68
+ def install_on_nix(ware, place)
69
+
70
+ target = "#{WAREHOUSE}/#{ware}"
71
+
72
+ if target.end_with?('.deb')
73
+ system "sudo dpkg -i #{target}"
74
+ return
75
+ end
76
+
77
+ if target.end_with?('.rpm')
78
+ system "sudo rpm -i #{target}"
79
+ return
80
+ end
81
+
82
+
83
+ if test 'd', place
84
+ cmd = "sudo install -Dt #{place} -m 755 #{target} "
85
+ system cmd
86
+ puts "\nghcurl: Install #{ware} to " + place
87
+
88
+ else
89
+ unless place.include?('/')
90
+ # User just give it another name
91
+ place = BIN_PATH + '/' + place
92
+ else
93
+ # User give it a path and its name
94
+ end
95
+ puts "\nghcurl: Installed as " + place
96
+ system "sudo cp #{target} #{place}"
97
+ system "sudo chmod +x #{place}"
98
+ end
99
+
100
+ end
101
+
102
+
103
+ def install_on_windows
104
+ end
105
+
106
+
107
+ def list_wares
108
+
109
+ def bold(str) "\e[1m#{str}\e[0m" end
110
+ def green(str) "\e[32m#{str}\e[0m" end
111
+ def blue(str) "\e[34m#{str}\e[0m" end
112
+
113
+ puts blue("In #{WAREHOUSE}")
114
+ puts
115
+ Dir.children(WAREHOUSE).each_with_index do |dict,i|
116
+ puts " #{blue(i+1)}. #{bold(green(dict))}"
117
+ end
118
+ puts
119
+ end
120
+
121
+
122
+ def help
123
+ puts <<~EOC
124
+ ghcurl (v#{VERSION}): Download files (and install) from Github releases
125
+
126
+ usage:
127
+ ghcurl repo_url file => Specify a repo's url
128
+ ghcurl user/repo file => Specify by user/repo
129
+ ghcurl xxx -v tag => Specify a tag version
130
+ ghcurl xxx -i [path] => Download and install, default to /usr/local/bin
131
+ ghcurl xxx -i filename => Download and install it as filename
132
+ ghcurl -l => List downloaded files
133
+ ghcurl -h => Print this help
134
+
135
+ EOC
136
+ end
137
+
138
+ end
139
+
140
+
141
+
142
+ ####################
143
+ # main: CLI Handling
144
+ ####################
145
+ extend Ghcurl
146
+
147
+ # e.g. ccmywish/binary
148
+ gh_repo, ware = ARGV.shift(2)
149
+ case gh_repo
150
+ when '-l' then list_wares ; exit
151
+ when '-h', '--help' then help ; exit
152
+ when nil then help ; exit
153
+ end
154
+
155
+ if ware.nil?
156
+ puts "ghcurl: Download what?"
157
+ puts " use: ghcurl repo file"
158
+ exit
159
+ end
160
+
161
+
162
+ arg = ARGV.shift
163
+
164
+ next_op = true
165
+
166
+ case arg
167
+ when '-v'
168
+ download gh_repo, ware, version: ARGV.shift
169
+ when /-v\d.*/
170
+ v = arg.split('-v')[1]
171
+ download gh_repo, ware, version: v
172
+ else
173
+ download gh_repo, ware
174
+ next_op = false
175
+ end
176
+
177
+
178
+ if next_op
179
+ next_op = ARGV.shift
180
+ else
181
+ next_op = arg
182
+ end
183
+
184
+
185
+ case next_op
186
+ when '-i'
187
+ place = ARGV.shift
188
+ if place.nil?
189
+ install(ware)
190
+ else
191
+ install(ware, place: place)
192
+ end
193
+ when /-i.*/
194
+ place = next_op.split('-i')[1]
195
+ install(ware, place: place)
196
+ 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.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ccmywish
@@ -13,7 +13,8 @@ dependencies: []
13
13
  description: Download files (and install) from Github releases.
14
14
  email:
15
15
  - ccmywish@qq.com
16
- executables: []
16
+ executables:
17
+ - ghcurl
17
18
  extensions: []
18
19
  extra_rdoc_files: []
19
20
  files:
@@ -21,6 +22,7 @@ files:
21
22
  - LICENSE
22
23
  - README.md
23
24
  - Rakefile
25
+ - bin/ghcurl
24
26
  homepage: https://github.com/ccmywish/ghcurl
25
27
  licenses:
26
28
  - MIT