-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
207 additions
and
32 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,56 @@ | ||
module Photocopier | ||
class FTP | ||
class Session | ||
def initialize(options) | ||
@scheme = options[:scheme] | ||
|
||
if sftp? | ||
@session = Net::SFTP.start( | ||
options[:host], | ||
options[:user], | ||
password: options[:password], | ||
port: options[:port] || 22 | ||
) | ||
else | ||
@session = Net::FTP.open( | ||
options[:host], | ||
username: options[:user], | ||
password: options[:password], | ||
port: options[:port] || 21 | ||
) | ||
@session.passive = options[:passive] if options.key?(:passive) | ||
end | ||
end | ||
|
||
def get(remote, local) | ||
if sftp? | ||
@session.download!(remote, local) | ||
else | ||
@session.get(remote, local) | ||
end | ||
end | ||
|
||
def put_file(local, remote) | ||
if sftp? | ||
@session.upload!(local, remote) | ||
else | ||
@session.put(local, remote) | ||
end | ||
end | ||
|
||
def delete(remote) | ||
if sftp? | ||
@session.remove!(remote) | ||
else | ||
@session.delete(remote) | ||
end | ||
end | ||
|
||
private | ||
|
||
def sftp? | ||
@scheme == 'sftp' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_litterla: true | ||
|
||
module Photocopier | ||
VERSION = "1.2.0".freeze | ||
VERSION = "1.3.0".freeze | ||
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,49 @@ | ||
RSpec.describe Photocopier::FTP::Session do | ||
let(:options) do | ||
{ | ||
host: 'host', | ||
user: 'user', | ||
password: 'password', | ||
scheme: 'ftp' | ||
} | ||
end | ||
let(:ftp) { spy('ftp') } | ||
let(:sftp) { spy('sftp') } | ||
|
||
before do | ||
allow(Net::SFTP).to receive(:start).and_return(sftp) | ||
allow(Net::FTP).to receive(:open).and_return(ftp) | ||
end | ||
|
||
context "with ftp scheme" do | ||
let(:session) { described_class.new(options) } | ||
|
||
it "calls ftp methods" do | ||
expect(ftp).to receive(:get).once | ||
expect(ftp).to receive(:put).once | ||
expect(ftp).to receive(:delete).once | ||
|
||
session.get(:remote, :local) | ||
session.put_file(:local, :remote) | ||
session.delete(:remote) | ||
end | ||
end | ||
|
||
context "with sftp scheme" do | ||
before do | ||
options[:scheme] = 'sftp' | ||
end | ||
|
||
let(:session) { described_class.new(options) } | ||
|
||
it "calls sftp methods" do | ||
expect(sftp).to receive(:download!).once | ||
expect(sftp).to receive(:upload!).once | ||
expect(sftp).to receive(:remove!).once | ||
|
||
session.get(:remote, :local) | ||
session.put_file(:local, :remote) | ||
session.delete(:remote) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,30 @@ | |
it_behaves_like "a Photocopier adapter" | ||
|
||
let(:ftp) { Photocopier::FTP.new(options) } | ||
let(:options) do { host: "host", user: "user", password: "password" } end | ||
let(:options) do | ||
{ | ||
host: "host", | ||
user: "user", | ||
password: "password", | ||
port: 2121 | ||
} | ||
end | ||
|
||
context "#session" do | ||
it "retrieves an FTP session" do | ||
expect(Net::FTP).to receive(:open).with("host", "user", "password") | ||
expect(Net::FTP).to receive(:open).with( | ||
"host", | ||
username: "user", | ||
password: "password", | ||
port: 2121 | ||
) | ||
ftp.send(:session) | ||
end | ||
|
||
context "passive mode" do | ||
let(:options) do { host: "host", passive: true } end | ||
let(:options) do | ||
{ host: "host", passive: true } | ||
end | ||
let(:ftp) { double('ftp').as_null_object } | ||
|
||
it "should enable passive mode" do | ||
|
@@ -22,7 +36,7 @@ | |
end | ||
|
||
context "#remote_ftp_url" do | ||
let(:options) do { host: "host" } end | ||
let(:options) { { host: "host" } } | ||
|
||
it "should build an ftp url" do | ||
expect(ftp.send(:remote_ftp_url)).to eq("ftp://host") | ||
|
@@ -96,22 +110,64 @@ | |
{ | ||
host: 'example.com', | ||
user: 'user', | ||
password: "pass!\"',;$u&V^s" | ||
password: "pass!\"',;$u&V^s", | ||
port: 2121 | ||
} | ||
end | ||
|
||
it "should build a lftp command with the right escaping" do | ||
lftp_commands = [ | ||
let(:lftp_commands) do | ||
[ | ||
'set ftp:list-options -a', | ||
'set cmd:fail-exit true', | ||
'open ftp://user:pass%21%22%27%2C%3B%24u%26V%[email protected]', | ||
"open -p #{ftp.inferred_port} #{options[:scheme] || 'ftp'}://user:pass%21%22%27%2C%3B%24u%26V%[email protected]", | ||
'find -d 1 remote\\ dir || mkdir -p remote\\ dir', | ||
'lcd local\\ dir', | ||
'cd remote\\ dir', | ||
'mirror --delete --use-cache --verbose --no-perms --allow-suid --no-umask --parallel=5 --reverse --dereference --exclude-glob .git --exclude-glob *.sql --exclude-glob bin/' | ||
].join("; ") | ||
end | ||
|
||
it "should build a lftp command with the right escaping" do | ||
expect(lftp_commands).to match('-p 2121') | ||
expect(ftp).to receive(:system).with("lftp -c '#{lftp_commands}'") | ||
ftp.send(:lftp, "local dir", "remote dir", true, [".git", "*.sql", "bin/"]) | ||
ftp.send(:lftp, "local dir", "remote dir", true, [".git", "*.sql", "bin/"], options[:port]) | ||
end | ||
|
||
context "without a port expressed" do | ||
before do | ||
options.delete :port | ||
end | ||
let(:ftp) { Photocopier::FTP.new(options) } | ||
|
||
context "if schema is sftp" do | ||
it "uses default port 22" do | ||
options[:scheme] = 'sftp' | ||
|
||
expect(lftp_commands).to match('-p 22') | ||
expect(ftp).to receive(:system).with("lftp -c '#{lftp_commands}'") | ||
ftp.send(:lftp, "local dir", "remote dir", true, [".git", "*.sql", "bin/"]) | ||
end | ||
end | ||
|
||
context "if schema is ftp" do | ||
it "uses default port 21" do | ||
options[:scheme] = 'ftp' | ||
|
||
expect(lftp_commands).to match('-p 21') | ||
expect(ftp).to receive(:system).with("lftp -c '#{lftp_commands}'") | ||
ftp.send(:lftp, "local dir", "remote dir", true, [".git", "*.sql", "bin/"]) | ||
end | ||
end | ||
|
||
context "if schema is ftps" do | ||
it "uses default port 21" do | ||
options[:scheme] = 'ftps' | ||
|
||
expect(lftp_commands).to match('-p 21') | ||
expect(ftp).to receive(:system).with("lftp -c '#{lftp_commands}'") | ||
ftp.send(:lftp, "local dir", "remote dir", true, [".git", "*.sql", "bin/"]) | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
@@ -135,7 +191,7 @@ | |
|
||
context "#put_file" do | ||
it "should send a file to remote" do | ||
expect(session).to receive(:put).with(file_path, remote_path) | ||
expect(session).to receive(:put_file).with(file_path, remote_path) | ||
ftp.put_file(file_path, remote_path) | ||
end | ||
end | ||
|
@@ -154,14 +210,14 @@ | |
context "#get_directory" do | ||
it "should get a remote directory" do | ||
expect(FileUtils).to receive(:mkdir_p).with(local_path) | ||
expect(ftp).to receive(:lftp).with(local_path, remote_path, false, exclude_list) | ||
expect(ftp).to receive(:lftp).with(local_path, remote_path, false, exclude_list, 2121) | ||
ftp.get_directory(remote_path, local_path, exclude_list) | ||
end | ||
end | ||
|
||
context "#put_directory" do | ||
it "should send a directory to remote" do | ||
expect(ftp).to receive(:lftp).with(local_path, remote_path, true, exclude_list) | ||
expect(ftp).to receive(:lftp).with(local_path, remote_path, true, exclude_list, 2121) | ||
ftp.put_directory(local_path, remote_path, exclude_list) | ||
end | ||
end | ||
|