Class: BFS::Bucket::SCP
- Inherits:
-
Abstract
- Object
- Abstract
- BFS::Bucket::SCP
- Defined in:
- lib/bfs/bucket/scp.rb
Overview
SCP buckets are operating on SCP/SSH connections.
Defined Under Namespace
Classes: CommandError
Instance Method Summary collapse
-
#close ⇒ Object
Closes the underlying connection.
-
#cp(src, dst, **_opts) ⇒ Object
Copies src to dst.
-
#create(path, encoding: self.encoding, perm: self.perm, **opts, &block) ⇒ Object
Creates a new file and opens it for writing.
-
#glob(pattern = '**/*', **_opts) ⇒ Object
Iterates over the contents of a bucket using a glob pattern.
-
#info(path, **_opts) ⇒ Object
Info returns the object info.
-
#initialize(host, prefix: nil, **opts) ⇒ SCP
constructor
Initializes a new bucket.
-
#ls(pattern = '**/*', **_opts) ⇒ Object
Lists the contents of a bucket using a glob pattern.
-
#mv(src, dst, **_opts) ⇒ Object
Moves src to dst.
-
#open(path, encoding: self.encoding, tempdir: nil, **_opts, &block) ⇒ Object
Opens an existing file for reading.
-
#rm(path, **_opts) ⇒ Object
Deletes a file.
Constructor Details
#initialize(host, prefix: nil, **opts) ⇒ SCP
Initializes a new bucket
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bfs/bucket/scp.rb', line 31 def initialize(host, prefix: nil, **opts) super(**opts) @prefix = prefix @client = Net::SCP.start(host, nil, **opts.slice(*Net::SSH::VALID_OPTIONS)) if @prefix # rubocop:disable Style/GuardClause @prefix = "#{norm_path(@prefix)}/" mkdir_p abs_path(@prefix) end end |
Instance Method Details
#close ⇒ Object
Closes the underlying connection
131 132 133 |
# File 'lib/bfs/bucket/scp.rb', line 131 def close @client.session.close unless @client.session.closed? end |
#cp(src, dst, **_opts) ⇒ Object
Copies src to dst
106 107 108 109 110 111 112 113 114 |
# File 'lib/bfs/bucket/scp.rb', line 106 def cp(src, dst, **_opts) full_src = full_path(src) full_dst = full_path(dst) mkdir_p File.dirname(full_dst) sh! %(cp -a -f #{Shellwords.escape(full_src)} #{Shellwords.escape(full_dst)}) rescue CommandError => e e.status == 1 ? raise(BFS::FileNotFound, src) : raise end |
#create(path, encoding: self.encoding, perm: self.perm, **opts, &block) ⇒ Object
Creates a new file and opens it for writing
74 75 76 77 78 79 80 81 82 |
# File 'lib/bfs/bucket/scp.rb', line 74 def create(path, encoding: self.encoding, perm: self.perm, **opts, &block) full = full_path(path) opts[:preserve] = true if perm && !opts.key?(:preserve) BFS::Writer.new(path, encoding: encoding, perm: perm) do |temp_path| mkdir_p File.dirname(full) @client.upload!(temp_path, full, **opts) end.perform(&block) end |
#glob(pattern = '**/*', **_opts) ⇒ Object
Iterates over the contents of a bucket using a glob pattern
51 52 53 54 55 |
# File 'lib/bfs/bucket/scp.rb', line 51 def glob(pattern = '**/*', **_opts) Enumerator.new do |acc| walk(pattern, with_stat: true) {|info| acc << info } end end |
#info(path, **_opts) ⇒ Object
Info returns the object info
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bfs/bucket/scp.rb', line 58 def info(path, **_opts) full = full_path(path) path = norm_path(path) out = sh! %(stat -c '%F;%s;%Z;%a' #{Shellwords.escape full}) type, size, epoch, mode = out.strip.split(';', 4) raise BFS::FileNotFound, path unless type.include?('file') BFS::FileInfo.new(path: path, size: size.to_i, mtime: Time.at(epoch.to_i), mode: BFS.norm_mode(mode)) rescue CommandError => e e.status == 1 ? raise(BFS::FileNotFound, path) : raise end |
#ls(pattern = '**/*', **_opts) ⇒ Object
Lists the contents of a bucket using a glob pattern
44 45 46 47 48 |
# File 'lib/bfs/bucket/scp.rb', line 44 def ls(pattern = '**/*', **_opts) Enumerator.new do |acc| walk(pattern) {|path| acc << path } end end |
#mv(src, dst, **_opts) ⇒ Object
Moves src to dst
120 121 122 123 124 125 126 127 128 |
# File 'lib/bfs/bucket/scp.rb', line 120 def mv(src, dst, **_opts) full_src = full_path(src) full_dst = full_path(dst) mkdir_p File.dirname(full_dst) sh! %(mv -f #{Shellwords.escape(full_src)} #{Shellwords.escape(full_dst)}) rescue CommandError => e e.status == 1 ? raise(BFS::FileNotFound, src) : raise end |
#open(path, encoding: self.encoding, tempdir: nil, **_opts, &block) ⇒ Object
Opens an existing file for reading
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bfs/bucket/scp.rb', line 85 def open(path, encoding: self.encoding, tempdir: nil, **_opts, &block) full = full_path(path) temp = Tempfile.new(File.basename(path), tempdir, encoding: encoding) temp.close @client.download!(full, temp.path) File.open(temp.path, encoding: encoding, &block) rescue Net::SCP::Error raise BFS::FileNotFound, path end |
#rm(path, **_opts) ⇒ Object
Deletes a file.
97 98 99 100 |
# File 'lib/bfs/bucket/scp.rb', line 97 def rm(path, **_opts) path = full_path(path) sh! %(rm -f #{Shellwords.escape(path)}) end |