Skip to content

Commit

Permalink
Merge pull request #45 from kirs/command-map
Browse files Browse the repository at this point in the history
Improved command map (possible breaking change, see the changelog.)
  • Loading branch information
leehambley committed Nov 22, 2013
2 parents 8b916b6 + d3fa36d commit 7e2da3c
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
This file is written in reverse chronological order, newer releases will
appear at the top.

## 1.1.0 (not released yet)

* Improved command map with prefix feature

## 1.0.0

* The gem now supports a run_locally, although it's nothing to do with SSH,
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ One can override the hash map for individual commands:
puts SSHKit.config.command_map[:rake]
# => /usr/local/rbenv/shims/rake

Another oportunity is to add command prefixes:

SSHKit.config.command_map.prefix[:rake].push("bundle exec")
puts SSHKit.config.command_map[:rake]
# => bundle exec rake

SSHKit.config.command_map.prefix[:rake].unshift("/usr/local/rbenv/bin exec")
puts SSHKit.config.command_map[:rake]
# => /usr/local/rbenv/bin exec bundle exec rake

One can also override the command map completely, this may not be wise, but it
would be possible, for example:

Expand Down
1 change: 1 addition & 0 deletions lib/sshkit/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_relative 'host'

require_relative 'command'
require_relative 'command_map'
require_relative 'configuration'
require_relative 'coordinator'

Expand Down
51 changes: 51 additions & 0 deletions lib/sshkit/command_map.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module SSHKit
class CommandMap
class PrefixProvider
def initialize
@storage = {}
end

def [](command)
@storage[command] ||= []

@storage[command]
end
end

def initialize(value = nil)
@map = value || defaults
end

def [](command)
if prefix[command].any?
prefixes = prefix[command].join(" ")

"#{prefixes} #{command}"
else
@map[command]
end
end

def prefix
@prefix ||= PrefixProvider.new
end

def []=(command, new_command)
@map[command] = new_command
end

def clear
@map = defaults
end

def defaults
Hash.new do |hash, command|
if %w{if test time}.include? command.to_s
hash[command] = command.to_s
else
hash[command] = "/usr/bin/env #{command}"
end
end
end
end
end
16 changes: 6 additions & 10 deletions lib/sshkit/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module SSHKit
class Configuration

attr_accessor :umask, :output_verbosity
attr_writer :output, :backend, :default_env, :command_map
attr_writer :output, :backend, :default_env

def output
@output ||= formatter(:pretty)
Expand All @@ -30,15 +30,11 @@ def format=(format)
end

def command_map
@command_map ||= begin
Hash.new do |hash, command|
if %w{if test time}.include? command.to_s
hash[command] = command.to_s
else
hash[command] = "/usr/bin/env #{command}"
end
end
end
@command_map ||= SSHKit::CommandMap.new
end

def command_map=(value)
@command_map = SSHKit::CommandMap.new(value)
end

private
Expand Down
40 changes: 40 additions & 0 deletions test/unit/test_command_map.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'helper'
require 'sshkit'

module SSHKit
class TestCommandMap < UnitTest

def setup
SSHKit.reset_configuration!
end

def test_defaults
map = CommandMap.new
assert_equal map[:rake], "/usr/bin/env rake"
assert_equal map[:test], "test"
end

def test_setter
map = CommandMap.new
map[:rake] = "/usr/local/rbenv/shims/rake"
assert_equal map[:rake], "/usr/local/rbenv/shims/rake"
end

def test_prefix
map = CommandMap.new
map.prefix[:rake].push("/home/vagrant/.rbenv/bin/rbenv exec")
map.prefix[:rake].push("bundle exec")

assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec rake"
end

def test_prefix_unshift
map = CommandMap.new
map.prefix[:rake].push("bundle exec")
map.prefix[:rake].unshift("/home/vagrant/.rbenv/bin/rbenv exec")

assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec rake"
end

end
end
7 changes: 4 additions & 3 deletions test/unit/test_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ def test_backend
end

def test_command_map
assert_equal SSHKit.config.command_map.is_a?(SSHKit::CommandMap), true

cm = Hash.new { |h,k| h[k] = "/opt/sites/example/current/bin #{k}"}
assert_equal Hash.new, SSHKit.config.command_map
assert_equal "/usr/bin/env ruby", SSHKit.config.command_map[:ruby]

assert SSHKit.config.command_map = cm
assert_equal cm, SSHKit.config.command_map
assert_equal SSHKit.config.command_map.is_a?(SSHKit::CommandMap), true
assert_equal "/opt/sites/example/current/bin ruby", SSHKit.config.command_map[:ruby]
end

Expand Down

0 comments on commit 7e2da3c

Please sign in to comment.