Sha256: fcee1dd1a325e3d7cb8ee489b1e586c75ca07333c15f1a85c74bcdeeae865255

Contents?: true

Size: 1.44 KB

Versions: 9

Compression:

Stored size: 1.44 KB

Contents

require 'fileutils'

class KuberKit::Shell::LocalShell < KuberKit::Shell::AbstractShell
  include KuberKit::Import[
    "tools.logger",
    "shell.command_counter"
  ]

  def exec!(command)
    command_number = command_counter.get_number.to_s.rjust(2, "0")
    
    logger.info("Executed command [#{command_number}]: #{command.to_s.cyan}")

    result = nil
    IO.popen(command, err: [:child, :out]) do |io|
      result = io.read.chomp.strip
    end

    if result && result != ""
      logger.info("Finished command [#{command_number}] with result: \n#{result.grey}")
    end

    if $?.exitstatus != 0
      raise ShellError, "Shell command failed: #{command}\r\n#{result}"
    end

    result
  end

  def read(file_path)
    File.read(file_path)
  end

  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

  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

  private
    def ensure_directory_exists(file_path)
      dir_path = File.dirname(file_path)

      unless Dir.exists?(dir_path)
        FileUtils.mkdir_p(dir_path)
      end
    end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
kuber_kit-0.1.8 lib/kuber_kit/shell/local_shell.rb
kuber_kit-0.1.7 lib/kuber_kit/shell/local_shell.rb
kuber_kit-0.1.6 lib/kuber_kit/shell/local_shell.rb
kuber_kit-0.1.5 lib/kuber_kit/shell/local_shell.rb
kuber_kit-0.1.4 lib/kuber_kit/shell/local_shell.rb
kuber_kit-0.1.3 lib/kuber_kit/shell/local_shell.rb
kuber_kit-0.1.2 lib/kuber_kit/shell/local_shell.rb
kuber_kit-0.1.1 lib/kuber_kit/shell/local_shell.rb
kuber_kit-0.1.0 lib/kuber_kit/shell/local_shell.rb