Skip to content

Instantly share code, notes, and snippets.

@jamis
Created July 25, 2011 14:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamis/1104315 to your computer and use it in GitHub Desktop.
Save jamis/1104315 to your computer and use it in GitHub Desktop.
Capistrano Transcriber: a wrapper script for Capistrano that writes session transcripts to a timestamped file.
#!/usr/bin/env ruby
require 'capistrano/cli'
class TranscriptLogger
module TranscriptLoggerMetadata
attr_accessor :tx_logger_metadata
def self.apply_to(object, options={})
object.extend(self) unless object.respond_to?(:tx_logger_metadata)
object.tx_logger_metadata ||= {}
object.tx_logger_metadata.update(options)
object
end
end
def initialize(file1, file2)
@file1 = prepare_stream(file1)
@file2 = prepare_stream(file2)
end
def close
@file1.close if @file1.tx_logger_metadata[:needs_close]
@file2.close if @file2.tx_logger_metadata[:needs_close]
end
def puts(line)
@file1.puts(line)
@file2.puts(line)
end
private
def prepare_stream(file)
if file.nil?
TranscriptLoggerMetadata.apply_to($stderr, :needs_close => false)
elsif file.respond_to?(:puts)
TranscriptLoggerMetadata.apply_to(file, :needs_close => false)
else
TranscriptLoggerMetadata.apply_to(File.open(file.to_str, "a"), :needs_close => true)
end
end
end
module TranscriptLoggerOptions
def option_parser
parser = super
parser.separator ""
parser.separator "Transcript Logging Options:"
parser.on("--[no-]transcript", "write logging to a timestanped file, as well as to whatever -l is set to (default TRUE)") do |tx|
options[:tx] = tx
end
parser
end
def parse_options!
super
options[:tx] = true unless options.key?(:tx)
if options[:tx]
transcript = Time.now.strftime("cap-%Y%m%d%H%M%S.log")
options[:output] = TranscriptLogger.new(options[:output], transcript)
end
options.delete :tx
end
end
Capistrano::CLI.send :include, TranscriptLoggerOptions
Capistrano::CLI.execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment