Module: Prepper::Tools::Text

Included in:
Package
Defined in:
lib/prepper/tools/text.rb

Overview

text related helpers

Instance Method Summary collapse

Instance Method Details

#append_text(text, path) ⇒ Object

append text to a file

Parameters:

  • text (String)

    text to append

  • path (String)


8
9
10
# File 'lib/prepper/tools/text.rb', line 8

def append_text(text, path)
  @commands << Command.new("/bin/echo -e '#{text}' | sudo tee -a #{path}", verifier: has_text?(text, path))
end

#has_text?(text, path) ⇒ Boolean

returns a verifier command to test the presence of a string in a file

Parameters:

  • text (String)

    text

  • path (String)

    path to file

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/prepper/tools/text.rb', line 15

def has_text?(text, path)
  regex = Regexp.escape(text)
  Command.new("grep -qPzo '^#{regex}$' #{path} ||", sudo: true)
end