Sha256: f907d74a762abdb6267bcb797a46be24b6ee3303aa871750b8c73a98d2caf135

Contents?: true

Size: 1.49 KB

Versions: 9

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require_relative '../application'

# Define general use methods
module Utils
  # Create the directory if it dosn't exist.
  def ensure_dir(dirname)
    unless Dir.exist?(dirname)
      FileUtils.mkdir_p(dirname)
      return false
    end
    true
  end

  # rubocop:disable Metrics/MethodLength
  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 StandardError => e
      puts "[ERROR] #{e}: Declare text encoding..."
      puts "        goto :host, :exec => 'command', :encoding => 'ISO-8859-1'"
    end

    text.split("\n")
  end
  # rubocop:enable Metrics/MethodLength

  def my_execute(cmd, encoding = 'UTF-8')
    return { exitstatus: 0, content: '' } if Application.instance.debug

    begin
      text = `#{cmd}`
      exitstatus = $CHILD_STATUS.exitstatus
    rescue StandardError => e # rescue Exception => e
      verbose '!'
      puts("[ERROR] #{e}: Local exec: #{cmd}")
    end
    content = encode_and_split(encoding, text)
    { exitstatus: exitstatus, content: content }
  end

  def verboseln(text)
    verbose(text + "\n")
  end

  def verbose(text)
    return if Application.instance.quiet?

    print text
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
teuton-2.1.11 lib/teuton/case_manager/utils.rb
teuton-2.1.10 lib/teuton/case_manager/utils.rb
teuton-2.1.9 lib/teuton/case_manager/utils.rb
teuton-2.1.8 lib/teuton/case_manager/utils.rb
teuton-2.1.8dev1 lib/teuton/case_manager/utils.rb
teuton-2.1.7 lib/teuton/case_manager/utils.rb
teuton-2.1.6 lib/teuton/case_manager/utils.rb
teuton-2.1.5dev lib/teuton/case_manager/utils.rb
teuton-2.1.4 lib/teuton/case_manager/utils.rb