Class: KuberKit::Shell::LocalShell
Constant Summary
AbstractShell::DirNotFoundError, AbstractShell::ShellError
Instance Method Summary
collapse
Instance Method Details
#delete(file_path) ⇒ Object
65
66
67
|
# File 'lib/kuber_kit/shell/local_shell.rb', line 65
def delete(file_path)
exec!("rm #{file_path}")
end
|
#dir_exists?(dir_path) ⇒ Boolean
73
74
75
|
# File 'lib/kuber_kit/shell/local_shell.rb', line 73
def dir_exists?(dir_path)
exec!("test -d #{dir_path} && echo 'true' || echo 'false'", log_command: false) == 'true'
end
|
#exec!(command, log_command: true) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/kuber_kit/shell/local_shell.rb', line 10
def exec!(command, log_command: true)
command_number = command_counter.get_number.to_s.rjust(2, "0")
if log_command
logger.info("Execute: [#{command_number}]: #{command.to_s.cyan}")
end
result = nil
IO.popen(command, err: [:child, :out]) do |io|
result = io.read.chomp.strip
end
if result && result != "" && log_command
logger.info("Finished [#{command_number}] with result: \n#{result.grey}")
end
if $?.exitstatus != 0
raise ShellError, "Shell command failed: #{command}\r\n#{result}"
end
result
end
|
#file_exists?(file_path) ⇒ Boolean
69
70
71
|
# File 'lib/kuber_kit/shell/local_shell.rb', line 69
def file_exists?(file_path)
exec!("test -f #{file_path} && echo 'true' || echo 'false'", log_command: false) == 'true'
end
|
#interactive!(command, log_command: true) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/kuber_kit/shell/local_shell.rb', line 33
def interactive!(command, log_command: true)
command_number = command_counter.get_number.to_s.rjust(2, "0")
if log_command
logger.info("Interactive: [#{command_number}]: #{command.to_s.cyan}")
end
result = system(command)
if !$?.success?
raise ShellError, "Shell command failed: #{command}\r\n#{result}"
end
end
|
#read(file_path) ⇒ Object
51
52
53
|
# File 'lib/kuber_kit/shell/local_shell.rb', line 51
def read(file_path)
File.read(file_path)
end
|
#recursive_list_files(path, name: nil) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/kuber_kit/shell/local_shell.rb', line 77
def recursive_list_files(path, name: nil)
command = %Q{find -L #{path} -type f}
command += " -name #{name}" if name
exec!(command).split(/[\r\n]+/)
rescue => e
if e.message.include?("No such file or directory")
raise DirNotFoundError.new("Dir not found: #{path}")
else
raise e
end
end
|
#sync(local_path, remote_path, exclude: nil, delete: true) ⇒ Object
47
48
49
|
# File 'lib/kuber_kit/shell/local_shell.rb', line 47
def sync(local_path, remote_path, exclude: nil, delete: true)
rsync_commands.rsync(self, local_path, remote_path, exclude: exclude, delete: delete)
end
|
#write(file_path, content) ⇒ Object
55
56
57
58
59
60
61
62
63
|
# File 'lib/kuber_kit/shell/local_shell.rb', line 55
def write(file_path, content)
ensure_directory_exists(file_path)
File.write(file_path, content)
logger.info("Created file #{file_path.to_s.cyan}\r\n#{content.grey}")
true
end
|