From fe89ac8a890b175377a4e613add6c61fbf6cce4c Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 17 Jan 2015 11:57:21 +0900 Subject: [PATCH] Add script for updating release assets --- src/update_assets.rb | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 src/update_assets.rb diff --git a/src/update_assets.rb b/src/update_assets.rb new file mode 100755 index 0000000..e9c750d --- /dev/null +++ b/src/update_assets.rb @@ -0,0 +1,41 @@ +#!/usr/bin/env ruby + +# http://www.rubydoc.info/github/rest-client/rest-client/RestClient +require 'rest_client' + +if ARGV.length < 3 + puts "usage: #$0 " +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 *%w[name id] }] + +files.select { |f| File.exists? f }.each do |file| + name = File.basename file + + if asset_id = assets[name] + 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