File
The Kanrisuru::Remote::File
class encapsulates the management of a file on a remote host. This helps with creating a new file, writing data, finding and replacing data in a file, as well as changing perimissions and ownership of the file.
Attributes
- [R] mode, blocks, size, type, gid, group, uid, user, inode, last_access, last_modified, last_changed, lines, words, chars, path
Public Class Methods
new
Instantiates a new instace of a remote file. Requires a host instance to read from.
file = Kanrisuru::Remote::File.new('/home/ubuntu/file1.txt', host)
file.file?
false
file.touch
file.expand_path
'/home/ubuntu/file1.txt'
file.file?
true
Public Instance Methods
expand_path
Returns the full path of where the file exists, expanding any symlinks if present.
host.os.release
'ubuntu'
file = Kanrisuru::Remote::File.new('/etc/os-release', host)
file.expand_path
'/usr/lib/os-release'
exists?
Returns if the file exists on the remote host.
file = Kanrisuru::Remote::File.new('~/newfile.conf', host)
file.exists?
false
file.touch
file.exists?
true
file?
Returns if the path is a file.
file = Kanrisuru::Remote::File.new('~/file.txt', host)
file.file?
true
Kanrisuru::Remote::File.new('/var', host).file?
false
dir?
Returns if the path is a directory.
file = Kanrisuru::Remote::File.new('~/', host)
file.dir?
true
zero?
Returns true
if the file is an empty file
type.
file = Kanrisuru::Remote::File.new('~/newfile.txt', host)
file.exists?
false
file.touch
file.zero?
true
file.append do |f|
f << 'text'
end
file.zero?
false
chmod
Change the file permissions bits. See Mode for full documentation of how the class interface works.
file = Kanrisuru::Remote::File.new('/etc/nginx/nginx.conf', host)
file.chmod('rw-rw-r--')
chown
Change the owner and/or group of the file. See Chown for full documentation of how the command works.
file = Kanrisuru::Remote::File.new('/home/ubuntu/file3', host)
file.chown('kanrisuru', 'kanrisuru')
writeable?
Returns true
if the file is writeable by the current user set on the remote host.
host.remote_user
'ubuntu'
file = Kanrisuru::Remote::File.new('/etc/nginx/nginx.conf', host)
file.writeable?
false
file.chown('ubuntu', 'ubuntu')
file.writeable?
true
[]
Access a line in a file by index.
file = Kanrisuru::Remote::File.new('/home/ubuntu/petnames.txt', host)
file[4]
'bentley'
find_and_replace_value
Replaces any occurence of a regex pattern with value.
host.cat('/home/ubuntu/file')
[
'lorem ispum dolor sit amet',
'lorem ipsum dolor sit amet',
'lorem dolor sit amet'
]
file = Kanrisuru::Remote::File.new('/home/ubuntu/file', host)
file.find_and_replace_value(/ipsum/, 'catz')
file.read
[
'lorem catz dolor sit amet',
'lorem catz dolor sit amet',
'lorem dolor sit amet'
]
find_and_replace_line
Replaces any occurence of a regex pattern with a line.
file = Kanrisuru::Remote::File.new('/home/ubuntu/file', host)
file.find_and_replace_line(/ipsum/, '## this is a comment')
file.read
[
'## this is a comment',
'## this is a comment',
'lorem dolor sit amet'
]
find_and_append
Appends a new line for each instance a regex pattern matches.
file = Kanrisuru::Remote::File.new('/home/ubuntu/file', host)
file.find_and_append(/ipsum/, 'lorem lorem lorem')
file.read
[
'lorem ispum dolor sit amet',
'lorem lorem lorem',
'lorem ipsum dolor sit amet',
'lorem lorem lorem',
'lorem dolor sit amet'
]
find_and_prepend
Prepends a new line for each instance a regex pattern matches.
file = Kanrisuru::Remote::File.new('/home/ubuntu/file', host)
file.find_and_prepend(/ipsum/, 'ipsum ipsum ipsum')
file.read
[
'ipsum ipsum ipsum',
'lorem ispum dolor sit amet',
'ipsum ipsum ipsum',
'lorem ipsum dolor sit amet',
'lorem dolor sit amet'
]
find_and_remove
Removes a line for each instances a regex pattern matches.
file = Kanrisuru::Remote::File.new('/home/ubuntu/file', host)
file.find_and_remove(/ipsum/)
file.read
[
'lorem dolor sit amet'
]
write
Writes to the file. If the remote file already had content, the file will be overwritten.
file = Kanrisuru::Remote::File.new('~/test', host)
file.write do |f|
f << 'this is a line'
f << 'this is another line'
f << 'yet another line getting written'
end
file.read
[
'this is a line',
'this is another line',
'yet another line getting written'
]
apppend
Appends to the file.
file.append do |f|
f << 'adding some more text later'
f << 'lines getting appended to the end'
end
file.read
[
'this is a line',
'this is another line',
'yet another line getting written',
'adding some more text later',
'lines getting appended to the end'
]
prepend
Prepends to the beginning file.
file.prepend do |f|
f << 'adding some text at the beginning'
f << 'which is squeezed in early'
end
file.read
[
'adding some text at the beginning',
'which is squeezed in early',
'this is a line',
'this is another line',
'yet another line getting written',
'adding some more text later',
'lines getting appended to the end'
]
touch
Update the file timestamps. If file doesn’t exist, touch will create an empty file on the remote host.
file = Kanrisuru::Remote::File.new('/home/ubuntu/new-file', host)
file.exists?
false
file.touch
file.exists?
true
delete
Delete the file from the remote host.
file = Kanrisuru::Remote::File.new('/home/ubuntu/existing-file', host)
file.exists?
true
file.delete
file.exists?
false
each
Iterate through each line in the file.
file.each do |line|
## Do something with the line
end
reload!
Fetches and updates the various attributes about the file, such as the inode, permissions, number of lines, etc.