Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Commit

Permalink
Allow Kumade::CLI to be initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Sep 9, 2011
1 parent 28f79b2 commit 2879fbc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 54 deletions.
2 changes: 1 addition & 1 deletion bin/kumade
Expand Up @@ -2,4 +2,4 @@

require 'kumade'

Kumade::CLI.run
Kumade::CLI.new
69 changes: 36 additions & 33 deletions lib/kumade/cli.rb
Expand Up @@ -4,35 +4,57 @@
module Kumade
class CLI
class << self
attr_reader :environment
attr_writer :deployer

def deployer
@deployer || Kumade::Deployer
end
end

def self.run(args = ARGV, out = StringIO.new)
@out = out
@options = parse_arguments!(args)
def initialize(args = ARGV, out = StringIO.new)
@options = {}
parse_arguments!(args)
@environment = args.shift || 'staging'

swapping_stdout_for(@out) do
self.class.swapping_stdout_for(out, pretending?) do
deploy
end
end

def self.deploy
def self.swapping_stdout_for(io, pretending = false)
if pretending
yield
else
begin
real_stdout = $stdout
$stdout = io
yield
rescue Kumade::DeploymentError
io.rewind
real_stdout.print(io.read)
ensure
$stdout = real_stdout
end
end
end

private

def deploy
if pretending?
puts "==> In Pretend Mode"
end
puts "==> Deploying to: #{environment}"
Deployer.new(environment, pretending?).deploy
puts "==> Deployed to: #{environment}"
puts "==> Deploying to: #{@environment}"
self.class.deployer.new(@environment, pretending?).deploy
puts "==> Deployed to: #{@environment}"
end

def self.parse_arguments!(args)
options = {}
def parse_arguments!(args)
OptionParser.new do |opts|
opts.banner = "Usage: kumade <environment> [options]"

opts.on("-p", "--pretend", "Pretend mode: print what kumade would do") do |p|
options[:pretend] = p
@options[:pretend] = p
end

opts.on_tail('-v', '--version', 'Show version') do
Expand All @@ -45,29 +67,10 @@ def self.parse_arguments!(args)
exit
end
end.parse!(args)

options
end

def self.swapping_stdout_for(io)
if pretending?
yield
else
begin
real_stdout = $stdout
$stdout = io
yield
rescue Kumade::DeploymentError
io.rewind
real_stdout.print(io.read)
ensure
$stdout = real_stdout
end
end
end

def self.pretending?
@options[:pretend]
def pretending?
!!@options[:pretend]
end
end
end
53 changes: 33 additions & 20 deletions spec/kumade/cli_spec.rb
Expand Up @@ -4,27 +4,44 @@
let(:out) { StringIO.new }
let(:environment) { 'my-environment' }

subject { Kumade::CLI }

%w(-p --pretend).each do |pretend_arg|
it "sets pretend mode when run with #{pretend_arg}" do
subject.stub(:deploy)

subject.run([environment, pretend_arg], out)
subject.pretending?.should be_true
let(:deployer) { stub("Deployer", :new => deployer_instance) }
let(:deployer_instance) { stub("DeployerInstance", :deploy => nil) }

before { Kumade::CLI.deployer = deployer }
after { Kumade::CLI.deployer = nil }

context "when pretending" do
%w(-p --pretend).each do |pretend_flag|
context pretend_flag do
subject { Kumade::CLI.new([pretend_flag, environment], out) }

it "deploys correctly" do
deployer.should_receive(:new).with(environment, true)
deployer_instance.should_receive(:deploy)
subject
end
end
end
end

it "defaults to staging" do
subject.stub(:deploy)
subject.run([], out)
subject.environment.should == 'staging'
context "running normally" do
subject { Kumade::CLI.new([environment], out) }

it "deploys correctly" do
deployer.should_receive(:new).with(environment, false)
deployer_instance.should_receive(:deploy)
subject
end
end
end

it "deploys" do
Kumade::Deployer.any_instance.should_receive(:deploy)
describe Kumade::CLI, ".deployer" do
after { Kumade::CLI.deployer = nil }
it { Kumade::CLI.deployer.should == Kumade::Deployer }

subject.run
it "can override deployer" do
Kumade::CLI.deployer = "deployer!"
Kumade::CLI.deployer.should == "deployer!"
end
end

Expand Down Expand Up @@ -53,14 +70,10 @@
end

context "in pretend mode" do
before do
Kumade::CLI.should_receive(:pretending?).and_return(true)
end

it 'prints everything' do
stdout.should_receive(:puts)

Kumade::CLI.swapping_stdout_for(output) do
Kumade::CLI.swapping_stdout_for(output, true) do
$stdout.puts "Hello, you can see me!"
end
end
Expand Down

0 comments on commit 2879fbc

Please sign in to comment.