Added options for exec command and a new exec_all command.

This commit is contained in:
Jeremy Cook
2014-04-12 15:39:51 -04:00
parent ab603240e4
commit efea18327b
2 changed files with 130 additions and 16 deletions

View File

@@ -279,6 +279,16 @@ class Homesick < Thor
desc 'exec CASTLE COMMAND', desc 'exec CASTLE COMMAND',
'Execute a single shell command inside the root of a castle' 'Execute a single shell command inside the root of a castle'
method_option :pretend,
type: :boolean,
default: false,
required: false,
desc: 'Perform a dry run of the command'
method_option :quiet,
type: :boolean,
default: false,
required: false,
desc: 'Run a command without any output from Homesick'
def exec(castle, *args) def exec(castle, *args)
check_castle_existance castle, 'exec' check_castle_existance castle, 'exec'
unless args.count > 0 unless args.count > 0
@@ -289,10 +299,38 @@ class Homesick < Thor
end end
full_command = args.join(' ') full_command = args.join(' ')
say_status "exec '#{full_command}'", say_status "exec '#{full_command}'",
"Executing command '#{full_command}' in castle '#{castle}'", "#{options[:pretend] ? 'Would execute' : 'Executing command'} '#{full_command}' in castle '#{castle}'",
:green :green unless options[:quiet]
inside repos_dir.join(castle) do inside repos_dir.join(castle) do
system(full_command) system(full_command) unless options[:pretend]
end
end
desc 'exec_all COMMAND',
'Execute a single shell command inside the root of every cloned castle'
method_option :pretend,
type: :boolean,
default: false,
required: false,
desc: 'Perform a dry run of the command'
method_option :quiet,
type: :boolean,
default: false,
required: false,
desc: 'Run a command without any output from Homesick'
def exec_all(*args)
unless args.count > 0
say_status :error,
'You must pass a shell command to execute',
:red
exit(1)
end
full_command = args.join(' ')
inside_each_castle do |castle|
say_status "exec '#{full_command}'",
"#{options[:pretend] ? 'Would execute' : 'Executing command'} '#{full_command}' in castle '#{castle}'",
:green unless options[:quiet]
system(full_command) unless options[:pretend]
end end
end end
@@ -386,7 +424,7 @@ class Homesick < Thor
subdir_filepath = subdir_file(castle) subdir_filepath = subdir_file(castle)
File.open(subdir_filepath, 'a+') do |subdir| File.open(subdir_filepath, 'a+') do |subdir|
subdir.puts path unless subdir.readlines.reduce(false) do |memo, line| subdir.puts path unless subdir.readlines.reduce(false) do |memo, line|
line.eql?("#{path.to_s}\n") || memo line.eql?("#{path}\n") || memo
end end
end end

View File

@@ -660,7 +660,7 @@ describe 'homesick' do
it 'returns an error message when the given castle does not exist' do it 'returns an error message when the given castle does not exist' do
# Set a default just in case none is set # Set a default just in case none is set
allow(ENV).to receive(:[]).with('EDITOR').and_return('vim') allow(ENV).to receive(:[]).with('EDITOR').and_return('vim')
expect(homesick).to receive('say_status').once allow(homesick).to receive('say_status').once
.with(:error, .with(:error,
/Could not open castle_repo, expected .* exist and contain dotfiles/, /Could not open castle_repo, expected .* exist and contain dotfiles/,
:red) :red)
@@ -676,36 +676,112 @@ describe 'homesick' do
end end
describe 'exec' do describe 'exec' do
it 'executes a single command with no arguments inside a given castle' do before do
given_castle 'castle_repo' given_castle 'castle_repo'
expect(homesick).to receive('inside').once.with(kind_of(Pathname)).and_yield end
expect(homesick).to receive('say_status').once it 'executes a single command with no arguments inside a given castle' do
allow(homesick).to receive('inside').once.with(kind_of(Pathname)).and_yield
allow(homesick).to receive('say_status').once
.with(be_a(String), .with(be_a(String),
be_a(String), be_a(String),
:green) :green)
expect(homesick).to receive('system').once.with('ls') allow(homesick).to receive('system').once.with('ls')
Capture.stdout { homesick.exec 'castle_repo', 'ls' } Capture.stdout { homesick.exec 'castle_repo', 'ls' }
end end
it 'executes a single command with arguments inside a given castle' do it 'executes a single command with arguments inside a given castle' do
given_castle 'castle_repo' allow(homesick).to receive('inside').once.with(kind_of(Pathname)).and_yield
expect(homesick).to receive('inside').once.with(kind_of(Pathname)).and_yield allow(homesick).to receive('say_status').once
expect(homesick).to receive('say_status').once
.with(be_a(String), .with(be_a(String),
be_a(String), be_a(String),
:green) :green)
expect(homesick).to receive('system').once.with('ls -la') allow(homesick).to receive('system').once.with('ls -la')
Capture.stdout { homesick.exec 'castle_repo', 'ls', '-la' } Capture.stdout { homesick.exec 'castle_repo', 'ls', '-la' }
end end
it 'raises an error when the method is called without a command' do it 'raises an error when the method is called without a command' do
given_castle 'castle_repo' allow(homesick).to receive('say_status').once
expect(homesick).to receive('say_status').once
.with(:error, .with(:error,
be_a(String), be_a(String),
:red) :red)
expect(homesick).to receive('exit').once.with(1) allow(homesick).to receive('exit').once.with(1)
Capture.stdout { homesick.exec 'castle_repo' } Capture.stdout { homesick.exec 'castle_repo' }
end end
context 'pretend' do
it 'does not execute a command when the pretend option is passed' do
allow(homesick).to receive('say_status').once
.with(be_a(String),
match(/.*Would execute.*/),
:green)
expect(homesick).to receive('system').never
Capture.stdout { homesick.invoke 'exec', %w(castle_repo ls -la), pretend: true }
end
end
context 'quiet' do
it 'does not print status information when quiet is passed' do
expect(homesick).to receive('say_status').never
allow(homesick).to receive('system').once
.with('ls -la')
Capture.stdout { homesick.invoke 'exec', %w(castle_repo ls -la), quiet: true }
end
end
end
describe 'exec_all' do
before do
given_castle 'castle_repo'
given_castle 'another_castle_repo'
end
it 'executes a command without arguments inside the root of each cloned castle' do
allow(homesick).to receive('inside_each_castle').exactly(:twice).and_yield('castle_repo').and_yield('another_castle_repo')
allow(homesick).to receive('say_status').at_least(:once)
.with(be_a(String),
be_a(String),
:green)
allow(homesick).to receive('system').at_least(:once).with('ls')
Capture.stdout { homesick.exec_all 'ls' }
end
it 'executes a command with arguments inside the root of each cloned castle' do
allow(homesick).to receive('inside_each_castle').exactly(:twice).and_yield('castle_repo').and_yield('another_castle_repo')
allow(homesick).to receive('say_status').at_least(:once)
.with(be_a(String),
be_a(String),
:green)
allow(homesick).to receive('system').at_least(:once).with('ls -la')
Capture.stdout { homesick.exec_all 'ls', '-la' }
end
it 'raises an error when the method is called without a command' do
allow(homesick).to receive('say_status').once
.with(:error,
be_a(String),
:red)
allow(homesick).to receive('exit').once.with(1)
Capture.stdout { homesick.exec_all }
end
context 'pretend' do
it 'does not execute a command when the pretend option is passed' do
allow(homesick).to receive('say_status').at_least(:once)
.with(be_a(String),
match(/.*Would execute.*/),
:green)
expect(homesick).to receive('system').never
Capture.stdout { homesick.invoke 'exec_all', %w(ls -la), pretend: true }
end
end
context 'quiet' do
it 'does not print status information when quiet is passed' do
expect(homesick).to receive('say_status').never
allow(homesick).to receive('system').at_least(:once)
.with('ls -la')
Capture.stdout { homesick.invoke 'exec_all', %w(ls -la), quiet: true }
end
end
end end
end end