Skip to content

Commit

Permalink
allow procs as command map values
Browse files Browse the repository at this point in the history
so this example works properly:

  SSHKit.config.command_map[:bundle] = -> { "#{fetch(:ruby_cmd)} /usr/bin/local/bundle" }
  • Loading branch information
Michal Cichra committed Dec 30, 2015
1 parent 1f6fb49 commit 9ed24c7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ appear at the top.
[PR #308](https://github.com/capistrano/sshkit/pull/308) @mattbrictson
* `SSHKit::Backend::Printer#test` now always returns true
[PR #312](https://github.com/capistrano/sshkit/pull/312) @mikz
* allow command map entries (`SSHKit::CommandMap#[]`) to be Procs
[PR #310]((https://github.com/capistrano/sshkit/pull/310)
@mikz

## 1.8.1

Expand Down
6 changes: 4 additions & 2 deletions lib/sshkit/command_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ def [](command)
end
end

TO_VALUE = ->(obj) { obj.respond_to?(:call) ? obj.call : obj }

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

def [](command)
if prefix[command].any?
prefixes = prefix[command].map{ |prefix| prefix.respond_to?(:call) ? prefix.call : prefix }
prefixes = prefix[command].map(&TO_VALUE)
prefixes = prefixes.join(" ")

"#{prefixes} #{command}"
else
@map[command]
TO_VALUE.(@map[command])
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/unit/test_command_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ def test_setter
assert_equal map[:rake], "/usr/local/rbenv/shims/rake"
end

def test_setter_procs
map = CommandMap.new
i = 0
map[:rake] = -> { i += 1; "/usr/local/rbenv/shims/rake#{i}" }

assert_equal map[:rake], "/usr/local/rbenv/shims/rake1"
assert_equal map[:rake], "/usr/local/rbenv/shims/rake2"
end

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

0 comments on commit 9ed24c7

Please sign in to comment.