module Utils

Define general use methods

Public Instance Methods

encode_and_split(encoding, text) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/teuton/case_manager/utils.rb, line 17
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
ensure_dir(dirname) click to toggle source

Create the directory if it dosn't exist.

# File lib/teuton/case_manager/utils.rb, line 8
def ensure_dir(dirname)
  unless Dir.exist?(dirname)
    FileUtils.mkdir_p(dirname)
    return false
  end
  true
end
my_execute(cmd, encoding = 'UTF-8') click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/teuton/case_manager/utils.rb, line 36
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
verbose(text) click to toggle source
# File lib/teuton/case_manager/utils.rb, line 54
def verbose(text)
  return if Application.instance.quiet?

  print text
end
verboseln(text) click to toggle source
# File lib/teuton/case_manager/utils.rb, line 50
def verboseln(text)
  verbose(text + "\n")
end