Class: KuberKit::Shell::LocalShell
- Inherits:
-
AbstractShell
- Object
- AbstractShell
- KuberKit::Shell::LocalShell
- Defined in:
- lib/kuber_kit/shell/local_shell.rb
Direct Known Subclasses
Constant Summary collapse
- MAX_LINES_TO_PRINT =
50
Constants inherited from AbstractShell
AbstractShell::DirNotFoundError, AbstractShell::ShellError
Instance Method Summary collapse
- #delete(file_path) ⇒ Object
- #dir_exists?(dir_path) ⇒ Boolean
- #exec!(command, log_command: true) ⇒ Object
- #file_exists?(file_path) ⇒ Boolean
- #interactive!(command, log_command: true) ⇒ Object
- #read(file_path) ⇒ Object
- #recursive_list_files(path, name: nil) ⇒ Object
- #sync(local_path, remote_path, exclude: nil, delete: true) ⇒ Object
- #wrap_command_with_pid(command) ⇒ Object
- #write(file_path, content) ⇒ Object
Instance Method Details
#delete(file_path) ⇒ Object
79 80 81 |
# File 'lib/kuber_kit/shell/local_shell.rb', line 79 def delete(file_path) exec!("rm #{file_path}") end |
#dir_exists?(dir_path) ⇒ Boolean
87 88 89 |
# File 'lib/kuber_kit/shell/local_shell.rb', line 87 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
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/kuber_kit/shell/local_shell.rb', line 12 def exec!(command, log_command: true) command_number = command_counter.get_number.to_s.rjust(2, "0") if log_command ui.print_debug("LocalShell", "Execute: [#{command_number}]: #{command.to_s.cyan}") end result = nil IO.popen(wrap_command_with_pid(command), err: [:child, :out]) do |io| result = io.read.chomp.strip end if result && result != "" && log_command print_result = result print_result_lines = print_result.split("\n") if print_result_lines.count >= MAX_LINES_TO_PRINT print_result = [ "[Result is too long, showing only first and last items]".yellow, print_result_lines.first, "[#{print_result_lines.count - 2} lines not showing]".yellow, print_result_lines.last ].join("\n") end ui.print_debug("LocalShell", "Finished [#{command_number}] with result: \n ----\n#{print_result.grey}\n ----") end if $?.exitstatus != 0 raise ShellError, "Shell command failed: #{command}\r\n#{result}" end result end |
#file_exists?(file_path) ⇒ Boolean
83 84 85 |
# File 'lib/kuber_kit/shell/local_shell.rb', line 83 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
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kuber_kit/shell/local_shell.rb', line 47 def interactive!(command, log_command: true) command_number = command_counter.get_number.to_s.rjust(2, "0") if log_command ui.print_debug("LocalShell", "Interactive: [#{command_number}]: #{command.to_s.cyan}") end result = system(wrap_command_with_pid(command)) if !$?.success? raise ShellError, "Shell command failed: #{command}\r\n#{result}" end end |
#read(file_path) ⇒ Object
65 66 67 |
# File 'lib/kuber_kit/shell/local_shell.rb', line 65 def read(file_path) File.read(file_path) end |
#recursive_list_files(path, name: nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/kuber_kit/shell/local_shell.rb', line 91 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..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
61 62 63 |
# File 'lib/kuber_kit/shell/local_shell.rb', line 61 def sync(local_path, remote_path, exclude: nil, delete: true) rsync_commands.rsync(self, local_path, remote_path, exclude: exclude, delete: delete) end |
#wrap_command_with_pid(command) ⇒ Object
103 104 105 |
# File 'lib/kuber_kit/shell/local_shell.rb', line 103 def wrap_command_with_pid(command) "KIT=#{Process.pid} #{command}" end |
#write(file_path, content) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/kuber_kit/shell/local_shell.rb', line 69 def write(file_path, content) ensure_directory_exists(file_path) File.write(file_path, content) ui.print_debug("LocalShell", "Created file #{file_path.to_s.cyan}\r\n ----\r\n#{content.grey}\r\n ----") true end |