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 28, 2015
1 parent 58d9603 commit 278de3e
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 @@ -13,6 +13,9 @@ appear at the top.
* display more accurate string for commands with spaces being output in `Formatter::Pretty`
[PR #304](https://github.com/capistrano/sshkit/pull/304)
@steved
* allow command map entries 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 278de3e

Please sign in to comment.