Result
Kanrisuru::Result
is the class that encapsulates a result returned from a server. This is done by passing in the Kanrisuru::Command
instance as part of the instation class. The result will defer to a block passed during instantiation.
Public Class Methods
new
Creates a new instance of Kanrisuru::Result
. Takes a command instance as the first argument, and an optional block. The block is used to parse the command output data from the remote server, but only when the command considers that it was run succesfully.
command = Kanrisuru::Command.new('cat file.csv')
result = Kanrisuru::Result.new(command) do |cmd|
lines = cmd.to_a
lines.map do |line|
line.split(',')
end
end
result.each do |row|
puts row
end
If a struct is returned in the block as part of the Kanrisuru::Result
initializer, the struct properties will be added to the result class as reader methods.
command = Kanrisuru::Command.new('uname -s')
result = Kanrisuru::Result.new(command) do |cmd|
Struct.new(:kernel).new(cmd.to_s)
end
result.kernel
'Darwin'
Public Instance Methods
[]
Access an attribute reader property of the result instance, or access an array index.
result[0]
['Name', 'Date']
result['path']
'/home/ubuntu'
to_s
Output the returned data as a string.
result = host.whoami
result.to_s
"ubuntu"
to_a
Output the returned data as an array.
result = host.ls(path: '/etc')
result.to_a
[
[ 0] #<Struct:Kanrisuru::Core::Path::FileInfo:0x0000be28
attr_reader :size = 4096,
date = #<DateTime: 2020-11-26T00:00:00+00:00 ((2459180j,0s,0n),+0s,2299161j)>,
group = "root",
hard_links = 3,
inode = 780,
mode = #<Kanrisuru::Mode:0x24400 @numeric=755 @symbolic=drwxr-xr-x>,
owner = "root",
path = "NetworkManager",
type = "directory"
>,
[ 1] .. [189],
[190] #<Struct:Kanrisuru::Core::Path::FileInfo:0x00017048
attr_reader :size = 449,
date = #<DateTime: 2020-03-23T00:00:00+00:00 ((2458932j,0s,0n),+0s,2299161j)>,
group = "root",
hard_links = 1,
inode = 14484,
mode = #<Kanrisuru::Mode:0x47200 @numeric=644 @symbolic=-rw-r--r-->,
owner = "root",
path = "zutilsrc",
type = "file"
>
]
to_i
Output the returned data as an integer.
result = host.get_gid('ubuntu')
result.success?
true
result.to_i
1000
inspect
Returns a class information as a string.
result = host.pwd
"#<Kanrisuru::Result:0x720 @status=0 @data=#<struct Kanrisuru::Core::Path::FilePath path=\"/home/ubuntu\"> @command=sudo -u ubuntu /bin/bash -c \"pwd\">"
failure?
Returns true if the command failed on the remote machine.
result = host.rm("/tmp")
result.failure?
true
success?
Returns true if the command succeded on the remote machine.
result = host.uptime
result.success?
true
status
Returns the status from the command on the remote machine.
result = host.kernel_statistics
result.status
0
each
Iterate over each element returned and parsed from command if itβs in an array format.
result = host.ip('address', 'show')
result.each do |device|
device.each do |address|
puts address.ip
end
end
error
If the command on the remote machine fails, error will return any returned information that was sent to stderr.
result = host.blkid(device: '/dev/vda15')
result.failure?
true
result.error
["blkid: error: /dev/vda15: Permission denied"]
data
Returns the underlying data object that is returned on the block called during the initialization of the result instance.
result = host.pwd
result.data
#<struct Kanrisuru::Core::Path::FilePath path="/home/ubuntu">
command
Returns the underlying command object used to run on the remote machine, handle the data and signal hanlders from the connection.
host.su('root')
result = host.find(paths: "/etc/*.conf")
result.command
<Kanrisuru::Command:0x000055ab1e888550
@valid_exit_codes=[0],
@raw_command="find /etc/*.conf",
@raw_result=["/etc/adduser.conf\n/etc/ca-certificates.conf\n/etc/debconf.conf\n/etc/deluser.conf\n/etc/e2scrub.conf\n/etc/fuse.conf\n/etc/gai.conf\n/etc/hdparm.conf\n/etc/host.conf\n/etc/idmapd.conf\n/etc/kernel-img.conf\n/etc/ld.so.conf\n/etc/libaudit.conf\n/etc/logrotate.conf\n/etc/ltrace.conf\n/etc/mke2fs.conf\n/etc/multipath.conf\n/etc/nsswitch.conf\n/etc/overlayroot.conf\n/etc/overlayroot.local.conf\n/etc/pam.conf\n/etc/popularity-contest.conf\n/etc/request-key.conf\n/etc/resolv.conf\n/etc/rsyslog.conf\n/etc/sysctl.conf\n/etc/ucf.conf\n/etc/xattr.conf\n"],
@exit_status=0,
@remote_user="root",
@remote_shell="/bin/bash",
@remote_path="",
@remote_env=""
>