Created
January 10, 2014 13:21
-
-
Save plexus/8351795 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Remote control for Trello | |
# | |
# To configure, get a Trello developer key, and stick it in `~/.trello-remote` | |
# | |
# # ~/.trello-remote | |
# TRELLO_DEVELOPER_PUBLIC_KEY=<your key> | |
# | |
# Next run `trello-remote token`, follow the instructions in your | |
# browser, you'll get a token back. Add it to `~/.trello-remote` | |
# | |
# # ~/.trello-remote | |
# TRELLO_DEVELOPER_PUBLIC_KEY=<your key> | |
# TRELLO_MEMBER_TOKEN=<your token> | |
# | |
# Now you can start using your remote! Try this | |
# | |
# trello-remote console | |
# | |
# You'll get a Pry console, for example to archive all cards in the | |
# "Development" board in the "Done" list, you can do | |
# | |
# find_list(/dev/i, /done/i).cards.each(&:close!) | |
# | |
# find_board and find_list can both take an exact string, or a regexp | |
# to match. | |
# | |
# * trello-remote show board_name#id | |
# display a ticket, board_name can be partial and is case | |
# insentive. id is the number you find in the ticket's url | |
# | |
# * trello-remote comment board_name#id -m "message" | |
# add a comment to a card | |
# | |
# * trello-remote list board_name:list_name | |
# list all cards in a list | |
# | |
require 'optparse' | |
require 'pp' | |
require 'pathname' | |
HOME = Pathname(ENV['HOME']) | |
SCRIPT_NAME = File.basename $0 | |
TRELLO_LOCAL_COPY = HOME.join('github', 'ruby-trello', 'lib') | |
$:.unshift TRELLO_LOCAL_COPY if TRELLO_LOCAL_COPY.directory? | |
require 'trello' | |
begin | |
load HOME.join('.trello-remote') | |
rescue => e | |
puts %q{ ~/.trello-remote should look like | |
TRELLO_MEMBER_TOKEN='...' | |
TRELLO_DEVELOPER_PUBLIC_KEY='...'} | |
exit 1 | |
end | |
BOARDS={} | |
def trello_get_token | |
expiration = '30days' # 'never' | |
url = "https://trello.com/1/authorize?key=#{TRELLO_DEVELOPER_PUBLIC_KEY}&name=#{SCRIPT_NAME}&expiration=#{expiration}&response_type=token&scope=read,write" | |
%w[www-browser firefox chromium-browser chrome].each do |browser| | |
unless `which #{browser}`.strip == "" | |
`#{browser} '#{url}'` | |
break | |
end | |
end | |
end | |
def trello_init | |
Trello.configure do |config| | |
config.developer_public_key = TRELLO_DEVELOPER_PUBLIC_KEY | |
config.member_token = TRELLO_MEMBER_TOKEN | |
end | |
end | |
def find_board(board_name) | |
return board_name if board_name.is_a? Trello::Board | |
BOARDS[board_name] ||= Trello::Board.all.detect {|board| board_name === board.name } | |
end | |
def find_list_id(board_name, list_name) | |
find_list(board_name, list_name).id | |
end | |
def find_list(board_name, list_name) | |
find_board(board_name).lists.detect {|list| list_name === list.name } | |
end | |
def org_to_board(board_name, list_name, org_file) | |
list_id = find_list_id(board_name, list_name) | |
org=File.read(org_file) | |
org.split(/^\* /).reject(&:empty?).each do |content| | |
name = content.lines.first.strip | |
desc = content.lines.drop(1).join.gsub(/^\*+/) {|x| | |
' '*([0,x.length-2].max)+(x.length > 1 ? '*' : "\n") | |
}.strip | |
Trello::Card.create( | |
name: name, | |
list_id: list_id, | |
desc: desc | |
) | |
end | |
end | |
OPTIONS={} | |
opt_parse = OptionParser.new do |opts| | |
opts.banner = "Usage: #{SCRIPT_NAME} <command> [options]\n use '#{SCRIPT_NAME} commands' to get a list of commands" | |
opts.on("-m", "--message MESSAGE", "Set the message/comment to add") do |m| | |
OPTIONS[:message] = m | |
end | |
opts.on( '-h', '--help', 'Display this screen' ) do | |
$stderr.puts opts; exit 1 | |
end | |
end | |
opt_parse.parse!(ARGV) | |
unless ARGV.count >= 1 | |
$stderr.puts "Expected a command" | |
$stderr.puts opt_parse | |
exit 1 | |
end | |
TOP_LEVEL=binding | |
Class.new do | |
def console | |
require 'pry' | |
TOP_LEVEL.pry | |
end | |
def token | |
trello_get_token | |
end | |
def show | |
trello_init | |
puts_card find_card_from_input | |
end | |
def comment | |
trello_init | |
card = find_card_from_input | |
card.add_comment OPTIONS.fetch(:message) { | |
fail_msg "Use '-m <message>' to set the comment" | |
} | |
end | |
def commands | |
puts self.class.public_instance_methods(false) | |
end | |
def list | |
trello_init | |
board, list = ARGV.shift.split(':') | |
board = find_board(Regexp.new(board, true)) | |
if list | |
cards = find_list(board, Regexp.new(list, true)).cards | |
else | |
cards = board.cards | |
end | |
cards.each &method(:puts_card_short) | |
end | |
private | |
def find_card_from_input | |
board, num = ARGV.shift.split('#') | |
find_board(Regexp.new(board, true)).find_card(num.to_i) | |
end | |
def puts_card(card) | |
title = "##{card.short_id} [#{card.list.name}] #{card.name} (#{card.labels.map(&:name).join(', ')})" | |
puts title | |
puts card.url | |
puts "-" * title.length | |
puts card.desc | |
puts "-" * title.length | |
end | |
def puts_card_short(card) | |
puts "##{card.short_id} [#{card.list.name}] #{card.name} (#{card.labels.map(&:name).join(', ')})" | |
end | |
def fail_msg(msg) | |
$stderr.puts msg | |
exit 2 | |
end | |
end.new.public_send(ARGV.shift) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment