Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ appear at the top.
## [Unreleased][]

* Your contribution here!
* [#408](https://github.com/capistrano/sshkit/pull/408): Teach upload! and download! to respect within(...) - [@sj26](https://github.com/sj26)

### Potentially breaking changes

* `upload!` and `download!` now support relative remote paths which are
relative to the `within` working directory. They were previously documented
as only supporting absolute paths, but relative paths still worked relative
to the remote working directory. If you rely on the previous behaviour you
may need to adjust your code.

## [1.14.0][] (2017-06-30)

Expand Down
15 changes: 13 additions & 2 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ end
```

## Download a file from disk

```ruby
on roles(:all) do
puts 'Downloading DB Backup File'
Expand All @@ -106,8 +107,18 @@ on hosts do |host|
end
```

**Note:** The `upload!()` method doesn't honor the values of `within()`, `as()`
etc, this will be improved as the library matures, but we're not there yet.
Upload and download will respect the `within()` directories:

```ruby
on hosts do |host|
within 'my/app/directory' do
upload! 'database.yml', 'config/database.yml'
end
end
```

**Note:** The `upload!()` method doesn't honor the values of `as()` etc, this
will be improved as the library matures, but we're not there yet.

## Upload a file from a stream

Expand Down
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ end
if Gem::Requirement.new('>= 2.1.0').satisfied_by?(Gem::Version.new(RUBY_VERSION))
gem 'chandler', '>= 0.1.1'
end

# public_suffix 3+ requires ruby 2.1+
if Gem::Requirement.new('< 2.1').satisfied_by?(Gem::Version.new(RUBY_VERSION))
gem 'public_suffix', '< 3'
end
2 changes: 2 additions & 0 deletions lib/sshkit/backends/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(_ = nil, &block)
end

def upload!(local, remote, options = {})
remote = File.join(pwd_path, remote) unless remote.start_with?("/")
if local.is_a?(String)
if options[:recursive]
FileUtils.cp_r(local, remote)
Expand All @@ -25,6 +26,7 @@ def upload!(local, remote, options = {})
end

def download!(remote, local=nil, _options = {})
remote = File.join(pwd_path, remote) unless remote.start_with?("/")
if local.nil?
FileUtils.cp(remote, File.basename(remote))
else
Expand Down
2 changes: 2 additions & 0 deletions lib/sshkit/backends/netssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ def assign_defaults

def upload!(local, remote, options = {})
summarizer = transfer_summarizer('Uploading', options)
remote = File.join(pwd_path, remote) unless remote.start_with?("/")
with_ssh do |ssh|
ssh.scp.upload!(local, remote, options, &summarizer)
end
end

def download!(remote, local=nil, options = {})
summarizer = transfer_summarizer('Downloading', options)
remote = File.join(pwd_path, remote) unless remote.start_with?("/")
with_ssh do |ssh|
ssh.scp.download!(remote, local, options, &summarizer)
end
Expand Down
17 changes: 17 additions & 0 deletions test/functional/backends/test_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ def test_upload
end
end

def test_upload_within
file_contents = "Some Content"
actual_file_contents = nil
Dir.mktmpdir do |dir|
Local.new do
within dir do
execute(:mkdir, "-p", "foo")
within "foo" do
upload!(StringIO.new(file_contents), "bar")
end
end
actual_file_contents = capture(:cat, File.join(dir, "foo", "bar"))
end.run
assert_equal file_contents, actual_file_contents
end
end

def test_upload_recursive
Dir.mktmpdir do |dir|
Dir.mkdir("#{dir}/local")
Expand Down
17 changes: 17 additions & 0 deletions test/functional/backends/test_netssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ def test_upload_and_then_capture_file_contents
assert_equal "Some Content\nWith a newline and trailing spaces \n ", actual_file_contents
end

def test_upload_within
file_name = SecureRandom.uuid
file_contents = "Some Content"
dir_name = SecureRandom.uuid
actual_file_contents = ""
Netssh.new(a_host) do |_host|
within("/tmp") do
execute :mkdir, "-p", dir_name
within(dir_name) do
upload!(StringIO.new(file_contents), file_name)
end
end
actual_file_contents = capture(:cat, "/tmp/#{dir_name}/#{file_name}", strip: false)
end.run
assert_equal file_contents, actual_file_contents
end

def test_upload_string_io
file_contents = ""
Netssh.new(a_host) do |_host|
Expand Down