summaryrefslogtreecommitdiffstats
path: root/src/update_assets.rb
blob: 531a8eebfc39a5e4dad0aaf45c450351dbf27156 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env ruby
# frozen_string_literal: true

# http://www.rubydoc.info/github/rest-client/rest-client/RestClient
require 'rest_client'
require 'json'

if ARGV.length < 3
  puts "usage: #{$PROGRAM_NAME} <token> <version> <files...>"
  exit 1
end

token, version, *files = ARGV
base = 'https://api.github.com/repos/junegunn/fzf-bin/releases'

# List releases
rels = JSON.parse(RestClient.get(base, authorization: "token #{token}"))
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') }]

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'
    )
  end
end.each(&:join)