Module: Utils
- Included in:
- Case, CaseManager
- Defined in:
- lib/teuton/case_manager/utils.rb
Instance Method Summary collapse
- #encode_and_split(encoding, text) ⇒ Object
- #ensure_dir(dirname) ⇒ Object
- #my_execute(cmd, encoding = "UTF-8") ⇒ Object
- #verbose(text) ⇒ Object
- #verboseln(text) ⇒ Object
Instance Method Details
#encode_and_split(encoding, text) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/teuton/case_manager/utils.rb', line 14 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 puts "[ERROR] #{e}: Declare text encoding..." puts " 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 |
# File 'lib/teuton/case_manager/utils.rb', line 5 def ensure_dir(dirname) # 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
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/teuton/case_manager/utils.rb', line 32 def my_execute(cmd, encoding = "UTF-8") return {exitstatus: 0, content: ""} if Application.instance.debug begin text = `#{cmd}` exitstatus = $CHILD_STATUS.exitstatus rescue => e verbose "!" puts("[ERROR] #{e}: Local exec: #{cmd}") end content = encode_and_split(encoding, text) {exitstatus: exitstatus, content: content} end |
#verbose(text) ⇒ Object
50 51 52 53 54 |
# File 'lib/teuton/case_manager/utils.rb', line 50 def verbose(text) return if Application.instance.quiet? print text end |
#verboseln(text) ⇒ Object
46 47 48 |
# File 'lib/teuton/case_manager/utils.rb', line 46 def verboseln(text) verbose(text + "\n") end |