Skip to content

Commit

Permalink
feat: add get_r and put methods
Browse files Browse the repository at this point in the history
Merge pull request #13 from mlibrary/add-methods
Add `get_r` and `put` methods
  • Loading branch information
niquerio authored Feb 7, 2024
2 parents 1e66aa5 + 1201758 commit 3e88d99
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
54 changes: 39 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Sftp

This gem wraps shell `sftp` to make working with it in ruby scripts easier
This gem wraps shell `sftp` to make working with it in Ruby scripts easier.

## Installation

Add this line to your application's Gemfile (niquerio todo: figure out what actually needs to be put here):
Add this line to your application's `Gemfile`:

```ruby
gem 'sftp'
gem "sftp",
git: "https://github.com/mlibrary/sftp",
tag: "{latest_tag_goes_here}"
```

And then execute:
Expand All @@ -22,7 +24,7 @@ Or install it yourself as:

Basic configuration:
```
require 'sftp'
require "sftp"
SFTP.configure do |config|
config.user = "your_sftp_user"
Expand All @@ -33,7 +35,7 @@ end
client = SFTP.client
```

`SFTP.client.ls` returns an array of path names to files in the sftp user's directory directory
`SFTP.client.ls` returns an array of path names to files in the SFTP user's directory.

```
SFTP.client.ls
Expand All @@ -43,32 +45,54 @@ SFTP.client.ls("directory")
#returns ["directory/file3.txt","directory/file4.txt"]
```

`SFTP.client.get(from,to)` downloads the file from the `from` path on the sftp server to the `to` path on the local machine
`SFTP.client.get(path, destination)` downloads the file from `path` on the SFTP server to the `destination` path on the local machine.

```
SFTP.client.get("direcotry/file3.txt","./")
```sh
SFTP.client.get("direcotry/file3.txt", "./")
ls .
"file3.txt"
```

`SFTP.client.rename(from, to)` renames a file on the sftp server.
`SFTP.client.get_r(path, destination)` downloads everything (files and/or directories) found at `path` on the SFTP server to the `destination` path on the local machine.
```sh
SFTP.client.ls("directory")
# returns ["file1.txt", "file2.txt"]
SFTP.client.get_r("directory", "./")
ls .
# directory
ls directory
# file1.txt file2.txt
```

`SFTP.client.rename(from, to)` renames a file on the SFTP server.

```sh
SFTP.client.ls
# returns ["file1.txt,"file2.txt","directory"]
SFTP.client.rename("file1.txt,"directory/renamed.txt")
# returns ["file1.txt, "file2.txt", "directory"]
SFTP.client.rename("file1.txt, "directory/renamed.txt")
SFT.client.ls
# returns ["file2.txt","directory"]
# returns ["file2.txt", "directory"]
SFTP.client.ls("directory")
# returns ["directory/file3.txt", "directory/file4.txt", "directory/renamed.txt"]
```
`SFTP.client.put(path, destination)` sends a file from `path` on the local machine to the `destination` path on the SFTP server.
```sh
ls .
# "file1.txt"
SFTP.client.put("file.txt", "directory")
SFTP.client.ls("directory")
#returns ["directory/file3.txt","directory/file4.txt", "directory/renamed.txt"]
# returns ["file1.txt"]
```
## Development
In the application root folder set up the ssh_keys
In the application root folder, set up the ssh_keys:
```
./bin/set_up_development_ssh_keys.sh
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/mlibraray/sftp
Bug reports and pull requests are welcome on GitHub at https://github.com/mlibrary/sftp
8 changes: 8 additions & 0 deletions lib/sftp/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ def get(path, destination)
run_an_sftp_command("$'@get #{path} #{destination}'")
end

def get_r(path, destination)
run_an_sftp_command("$'@get -R #{path} #{destination}'")
end

def rename(from, to)
run_an_sftp_command("$'@rename #{from} #{to}'")
end

def put(path, destination)
run_an_sftp_command("$'@put #{path} #{destination}'")
end

private

def run_an_sftp_command(command)
Expand Down
14 changes: 14 additions & 0 deletions spec/sftp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,18 @@
subject.get("my/source/path", "my/destination/path")
end
end
context "#get_r" do
it "sends appropriate basic message to shell" do
command = [@sftp_command_base, "$'@get -R my/source/path my/destination/path'"].flatten
expect(@shell).to receive(:run).with(command)
subject.get_r("my/source/path", "my/destination/path")
end
end
context "#put" do
it "sends appropriate basic message to shell" do
command = [@sftp_command_base, "$'@put my/source/path my/destination/path'"].flatten
expect(@shell).to receive(:run).with(command)
subject.put("my/source/path", "my/destination/path")
end
end
end

0 comments on commit 3e88d99

Please sign in to comment.