-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from kirs/command-map
Improved command map (possible breaking change, see the changelog.)
- Loading branch information
Showing
7 changed files
with
116 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters