Class: Microsandbox::SftpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/microsandbox/ssh.rb

Overview

A high-level SFTP session over an SshClient, from Microsandbox::SshClient#sftp. All paths are guest paths. Mirrors the ‘SftpClient` of the official SDKs.

Instance Method Summary collapse

Constructor Details

#initialize(native) ⇒ SftpClient

Returns a new instance of SftpClient.



50
51
52
# File 'lib/microsandbox/ssh.rb', line 50

def initialize(native)
  @native = native
end

Instance Method Details

#closenil

Close the SFTP session. Idempotent.

Returns:

  • (nil)


126
127
128
129
# File 'lib/microsandbox/ssh.rb', line 126

def close
  @native.close
  nil
end

#mkdir(path) ⇒ nil

Create a directory.

Returns:

  • (nil)


79
80
81
82
# File 'lib/microsandbox/ssh.rb', line 79

def mkdir(path)
  @native.mkdir(path.to_s)
  nil
end

#read(path) ⇒ String

Read a file’s full contents.

Returns:

  • (String)

    raw bytes (ASCII-8BIT)



56
57
58
# File 'lib/microsandbox/ssh.rb', line 56

def read(path)
  @native.read(path.to_s)
end

Read a symlink’s target.

Returns:

  • (String)


120
121
122
# File 'lib/microsandbox/ssh.rb', line 120

def read_link(path)
  @native.read_link(path.to_s)
end

#read_text(path) ⇒ String

Read a file and decode it as UTF-8 (lossy — invalid byte sequences are replaced with U+FFFD).

Returns:

  • (String)


63
64
65
# File 'lib/microsandbox/ssh.rb', line 63

def read_text(path)
  read(path).force_encoding(Encoding::UTF_8).scrub
end

#real_path(path) ⇒ String

Resolve a path to its canonical absolute form.

Returns:

  • (String)


114
115
116
# File 'lib/microsandbox/ssh.rb', line 114

def real_path(path)
  @native.real_path(path.to_s)
end

#remove_dir(path) ⇒ nil

Remove an empty directory.

Returns:

  • (nil)


93
94
95
96
# File 'lib/microsandbox/ssh.rb', line 93

def remove_dir(path)
  @native.remove_dir(path.to_s)
  nil
end

#remove_file(path) ⇒ nil

Remove a file.

Returns:

  • (nil)


86
87
88
89
# File 'lib/microsandbox/ssh.rb', line 86

def remove_file(path)
  @native.remove_file(path.to_s)
  nil
end

#rename(old_path, new_path) ⇒ nil

Rename (move) a file or directory.

Returns:

  • (nil)


100
101
102
103
# File 'lib/microsandbox/ssh.rb', line 100

def rename(old_path, new_path)
  @native.rename(old_path.to_s, new_path.to_s)
  nil
end

Create a symlink at link_path pointing to target.

Returns:

  • (nil)


107
108
109
110
# File 'lib/microsandbox/ssh.rb', line 107

def symlink(target, link_path)
  @native.symlink(target.to_s, link_path.to_s)
  nil
end

#write(path, data) ⇒ nil

Write a file, creating or truncating it.

Parameters:

  • data (String)

    raw bytes to write (binary-safe)

Returns:

  • (nil)

Raises:

  • (TypeError)

    if data is not a String



71
72
73
74
75
# File 'lib/microsandbox/ssh.rb', line 71

def write(path, data)
  bytes = Microsandbox.coerce_write_bytes(data)
  @native.write(path.to_s, bytes)
  nil
end