From b8817e6833f2f9b723905e65280f2707c5529381 Mon Sep 17 00:00:00 2001 From: Michal Cichra Date: Mon, 28 Dec 2015 15:24:11 +0100 Subject: [PATCH 1/2] allow procs as command map values so this example works properly: SSHKit.config.command_map[:bundle] = -> { "#{fetch(:ruby_cmd)} /usr/bin/local/bundle" } --- CHANGELOG.md | 3 +++ lib/sshkit/command_map.rb | 6 ++++-- test/unit/test_command_map.rb | 9 +++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b5b2acc..cd3ea0fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,9 @@ appear at the top. and have a cleaner internal API. You can still completely disable the pool by setting `SSHKit::Backend::Netssh.pool.idle_timeout = 0`. @mattbrictson @byroot [PR #328](https://github.com/capistrano/sshkit/pull/328) + * Allow command map entries (`SSHKit::CommandMap#[]`) to be Procs + [PR #310]((https://github.com/capistrano/sshkit/pull/310) + @mikz ### Bug fixes diff --git a/lib/sshkit/command_map.rb b/lib/sshkit/command_map.rb index fde164b4..c4766659 100644 --- a/lib/sshkit/command_map.rb +++ b/lib/sshkit/command_map.rb @@ -31,18 +31,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 diff --git a/test/unit/test_command_map.rb b/test/unit/test_command_map.rb index 65e85f93..06d37dfc 100644 --- a/test/unit/test_command_map.rb +++ b/test/unit/test_command_map.rb @@ -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") From a13b366a73bae641d12a4562886b977523655f5d Mon Sep 17 00:00:00 2001 From: Michal Cichra Date: Mon, 28 Dec 2015 16:27:14 +0100 Subject: [PATCH 2/2] command map prefix should still use map command it is valid use case to set both command map and prefix in case of using different binary, the map should still be active and prefix should be applied as extra so following example correclty executes rake2.2 within bundler: SSHKit.config.command_map[:rake] = 'rake2.2' SSHKit.config.command_map.prefix[:rake] = 'bundle exec' Only drawback is, that resulting command will be: bundle exec /usr/bin/env rake2.2 --- CHANGELOG.md | 3 +++ lib/sshkit/command_map.rb | 10 +++------- test/unit/test_command_map.rb | 16 ++++++++++++---- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd3ea0fb..51728e43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ appear at the top. @beatrichartz * `SSHKit::Backend::Printer#test` now always returns true [PR #312](https://github.com/capistrano/sshkit/pull/312) @mikz + * when using `SSHKit::CommandMap#prefix`, resolve the command through `SSHKit::CommandMap#[]` + [PR #311]((https://github.com/capistrano/sshkit/pull/311) + @mikz ### New features diff --git a/lib/sshkit/command_map.rb b/lib/sshkit/command_map.rb index c4766659..608b1be5 100644 --- a/lib/sshkit/command_map.rb +++ b/lib/sshkit/command_map.rb @@ -38,14 +38,10 @@ def initialize(value = nil) end def [](command) - if prefix[command].any? - prefixes = prefix[command].map(&TO_VALUE) - prefixes = prefixes.join(" ") + prefixes = prefix[command].map(&TO_VALUE) + cmd = TO_VALUE.(@map[command]) - "#{prefixes} #{command}" - else - TO_VALUE.(@map[command]) - end + [*prefixes, cmd].compact.join(' ') end def prefix diff --git a/test/unit/test_command_map.rb b/test/unit/test_command_map.rb index 06d37dfc..99948ebb 100644 --- a/test/unit/test_command_map.rb +++ b/test/unit/test_command_map.rb @@ -30,7 +30,7 @@ def test_prefix 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" + assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec /usr/bin/env rake" end def test_prefix_procs @@ -38,7 +38,7 @@ def test_prefix_procs map.prefix[:rake].push("/home/vagrant/.rbenv/bin/rbenv exec") map.prefix[:rake].push(proc{ "bundle exec" }) - assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec rake" + assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec /usr/bin/env rake" end def test_prefix_unshift @@ -46,7 +46,7 @@ def test_prefix_unshift 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" + assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec /usr/bin/env rake" end def test_indifferent_setter @@ -62,7 +62,7 @@ def test_indifferent_prefix 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" + assert_equal map[:rake], "/home/vagrant/.rbenv/bin/rbenv exec bundle exec /usr/bin/env rake" end def test_prefix_initialization_is_thread_safe @@ -74,5 +74,13 @@ def test_prefix_initialization_is_thread_safe end threads.each(&:join) end + + def test_prefix_setter + map = CommandMap.new({}) + map[:rake] = 'rake2.2' + map.prefix[:rake].push('bundle exec') + + assert_equal map[:rake], 'bundle exec rake2.2' + end end end