Module: Utils

Included in:
CaseManager
Defined in:
lib/teuton/deprecated/utils.rb,
lib/teuton/case_manager/utils.rb

Instance Method Summary collapse

Instance Method Details

#encode_and_split(encoding, text) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/teuton/deprecated/utils.rb', line 6

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

#ensure_dir(dirname) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/teuton/case_manager/utils.rb', line 5

def ensure_dir(dirname)
  # TODO: Mover a la carpeta Utils
  # Create the directory if it dosn't exist.
  unless Dir.exist?(dirname)
    FileUtils.mkdir_p(dirname)
    return false
  end
  true
end

#my_execute(cmd, encoding = "UTF-8") ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/teuton/deprecated/utils.rb', line 25

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