fzf/src/update_assets.rb

48 lines
1.2 KiB
Ruby
Raw Normal View History

2015-01-17 02:57:21 +00:00
#!/usr/bin/env ruby
# frozen_string_literal: true
2015-01-17 02:57:21 +00:00
# http://www.rubydoc.info/github/rest-client/rest-client/RestClient
require 'rest_client'
2016-08-20 20:00:27 +00:00
require 'json'
2015-01-17 02:57:21 +00:00
if ARGV.length < 3
puts "usage: #{$PROGRAM_NAME} <token> <version> <files...>"
2015-01-23 11:32:56 +00:00
exit 1
2015-01-17 02:57:21 +00:00
end
token, version, *files = ARGV
base = 'https://api.github.com/repos/junegunn/fzf-bin/releases'
2015-01-17 02:57:21 +00:00
# List releases
rels = JSON.parse(RestClient.get(base, authorization: "token #{token}"))
2015-01-17 02:57:21 +00:00
rel = rels.find { |r| r['tag_name'] == version }
unless rel
puts "#{version} not found"
exit 1
end
# List assets
assets = Hash[rel['assets'].map { |a| a.values_at('name', 'id') }]
2015-01-17 02:57:21 +00:00
files.select { |f| File.exist?(f) }.map do |file|
Thread.new do
name = File.basename(file)
if asset_id = assets[name] # rubocop:todo Lint/AssignmentInCondition
puts "#{name} found. Deleting asset id #{asset_id}."
RestClient.delete("#{base}/assets/#{asset_id}",
authorization: "token #{token}")
else
puts "#{name} not found"
end
puts "Uploading #{name}"
RestClient.post(
"#{base.sub('api', 'uploads')}/#{rel['id']}/assets?name=#{name}",
File.read(file),
authorization: "token #{token}",
content_type: 'application/octet-stream'
)
2015-01-17 02:57:21 +00:00
end
end.each(&:join)