Navigation Menu

Skip to content

Commit

Permalink
Support deleting showterms
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Nov 12, 2013
1 parent 5831fa7 commit 0d0d619
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
22 changes: 22 additions & 0 deletions bin/showterm
Expand Up @@ -14,6 +14,11 @@ class Showterm::Main
ARGV.shift
reupload

elsif ARGV[0] == '--delete'

ARGV.shift
delete

else

if '-e' == ARGV[0] || '--edit' == ARGV[0]
Expand All @@ -37,6 +42,8 @@ class Showterm::Main
def help
puts <<-EOF
Usage: showterm [-e] <command to run>
showterm --retry <script> <times>
showterm --delete <url>
showterm will record the exact output of your session, and upload it to the
internet where it can be replayed by anyone to whom you give the URL.
Expand All @@ -47,6 +54,9 @@ class Showterm::Main
If you would like to attempt an upload again
showterm --retry <script> <times>
To delete a showterm you have uploaded from this computer
showterm --delete <url>
EOF
end

Expand All @@ -58,6 +68,18 @@ class Showterm::Main
end
end

def delete
if ARGV.size == 1
puts "Deleting..."
puts Showterm.delete! ARGV.first
else
puts "Usage: showterm --delete <url>"
end
rescue => e
puts [e] + e.backtrace
puts "-" * 80
end

def upload(sf, tf)
puts "Uploading..."
puts Showterm.upload! sf, tf
Expand Down
52 changes: 42 additions & 10 deletions lib/showterm.rb
@@ -1,6 +1,7 @@
require 'tempfile'
require 'shellwords'
require 'net/https'
require 'securerandom'

module Showterm
extend self
Expand Down Expand Up @@ -43,24 +44,55 @@ def terminal_height
# @param [String] scriptfile The ANSI dump of the terminal
# @param [String] timingfile The timings
# @param [Integer] cols The width of the terminal
def upload!(scriptfile, timingfile, cols=terminal_width, lines=terminal_height)
retried ||= false
request = Net::HTTP::Post.new("/scripts")
request.set_form_data(:scriptfile => scriptfile,
:timingfile => timingfile,
:cols => cols,
:lines => lines)
# @param [String] secret The shared secret that will allow deleting this showterm.
def upload!(scriptfile, timingfile, cols=terminal_width, lines=terminal_height, secret=shared_secret)
with_retry do
request = Net::HTTP::Post.new("/scripts")
request.set_form_data(:scriptfile => scriptfile,
:timingfile => timingfile,
:cols => cols,
:lines => lines,
:secret => secret)

response = http(request)
raise response.body unless Net::HTTPSuccess === response
response.body
end
end

# Delete from showterm.io
#
# @param [String] url The URL of the showterm to delete
# @param [String] secret The secret with which it was uploaded.
def delete!(url, secret=shared_secret)

request = Net::HTTP::Delete.new(URI(url).path)
request.set_form_data(:secret => secret)
response = http(request)

raise response.body unless Net::HTTPSuccess === response
response.body
end

private

def with_retry(n=1, &block)
yield
rescue
raise if retried
retried = true
raise if n == 0
n -= 1
retry
end

private
def shared_secret
path = File.expand_path("~/.showterm")
unless File.exist?(path)
File.open(path, 'w') do |f|
f.write SecureRandom.hex(16)
end
end
File.read(path).strip
end

# Get a temporary file that will be deleted when the program exits.
def temp_file
Expand Down

0 comments on commit 0d0d619

Please sign in to comment.