Sha256: 3a7461f63cec0366855dc19204f50846ef7051e75c46477c47274f07a231ba29

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

require "open3"
require "rainbow"
require_relative "../utils/project"

module Utils
  def encode_and_split(encoding, text)
    # Convert text to UTF-8 deleting unknown chars
    text ||= "" # Ensure text is not nil
    flag = [:default, "UTF-8"].include? encoding
    return text.encode("UTF-8", invalid: :replace).split("\n") if flag

    # Convert text from input ENCODING to UTF-8
    ec = Encoding::Converter.new(encoding.to_s, "UTF-8")
    begin
      text = ec.convert(text)
    rescue => e
      warn "[ERROR] #{e}"
      warn "        Suggest declare text encoding, for example:"
      warn "        run 'command', on: :host, :encoding => 'ISO-8859-1'"
    end

    text.split("\n")
  end

  def my_execute(cmd, encoding = "UTF-8")
    # TODO: mover a la clase ExecuteManager
    return {exitstatus: 0, content: ""} if Project.debug?

    begin
      text, status = Open3.capture2e(cmd)
      exitstatus = status.exitstatus
    rescue => e
      verbose Rainbow("!").green
      text = e.to_s
      exitstatus = 1
    end
    content = encode_and_split(encoding, text)
    {exitstatus: exitstatus, content: content}
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
teuton-2.9.2 lib/teuton/deprecated/utils.rb
teuton-2.9.1 lib/teuton/deprecated/utils.rb
teuton-2.9.0 lib/teuton/deprecated/utils.rb
teuton-2.8.0 lib/teuton/case/deprecated/utils.rb
teuton-2.7.3 lib/teuton/case/deprecated/utils.rb
teuton-2.7.2 lib/teuton/case/deprecated/utils.rb
teuton-2.7.1 lib/teuton/case/deprecated/utils.rb
teuton-2.7.0 lib/teuton/case/deprecated/utils.rb