module LoomExt::CoreMods::Files::Actions

Public Instance Methods

append(text="") click to toggle source
# File lib/loomext/coremods/files.rb, line 130
def append(text="")
  if text.index "\n"
    Loom.log.warn "append lines individually until cmd escaping is fixed.... "
  end

  if loom.is_sudo?
    Loom.log.warn "do not use files.append in sudo" +
      ": use files.sudo_append due to poor command escaping"
  end

  each_path do |p|
    # TODO: this shit is broken when escaped in a sudo command. This is
    # why I began work on the harness.
    #     $ cd /home/pi; sudo -u root -- /bin/sh -c "/bin/echo -e 192.168.1.190 rp0\''; '\'" | tee -a /etc/hosts
    #
    text.each_line do |line|
      loom.x :"/bin/echo", "-e", line, :pipe_to => [[:tee, "-a", p]]
    end

    # TODO: fix this broken shit w/ the harness, CmdRedirect and
    # CmdWrapper are dogshit. This was an escaping attempt before harness
    # script.
    #
    # redirect = Loom::Shell::CmdRedirect.append_stdout p
    # cmd = Loom::Shell::CmdWrapper.new(
    #   :"/bin/echo", "-e", text, redirect: redirect)
  end
end
cat() click to toggle source
# File lib/loomext/coremods/files.rb, line 39
def cat
  each_path do |p|
    shell.capture :cat, p
  end
end
chown(user: nil, group: nil) click to toggle source
# File lib/loomext/coremods/files.rb, line 76
def chown(user: nil, group: nil)
  group_arg = group && ":" + group.to_s

  each_path do |p|
    shell.execute :chown, [user, group_arg].compact.map(&:to_s).join, p
  end
end
ensure_line(line, sudo: false) click to toggle source

TODO this sudo line is just another instance of needing the harness to work properly (in this case to provides a hack for sudo_append)

# File lib/loomext/coremods/files.rb, line 94
def ensure_line(line, sudo: false)
  if loom.is_sudo?
    Loom.log.warn "do not use files.ensure_line in sudo  due to poor command escaping" +
      ": use files.ensure_line and pass sudo: true"
  end

  each_path do |p|
    file = shell.capture :cat, p

    matches = file.match(/^#{line}$/)
    unless matches
      if sudo
        sudo_append(line)
      else
        append(line)
      end
    else
      Loom.log.debug(self) { "ensure_line match found: #{matches[0]}"}
    end
  end
end
gsub(pattern: nil, replace: nil, &block) click to toggle source
# File lib/loomext/coremods/files.rb, line 66
def gsub(pattern: nil, replace: nil, &block)
  each_path do |p|
    contents = shell.capture :cat, p
    if contents
      contents.gsub!(pattern, replace, &block) unless pattern.nil?
      write contents
    end
  end
end
match?(pattern: /./) click to toggle source
# File lib/loomext/coremods/files.rb, line 57
def match?(pattern: /./)
  all = true
  each_path do |p|
    file = shell.capture :cat, p
    all &&= file.match(pattern)
  end
  all
end
mkdir(flags: nil, **opts) click to toggle source
# File lib/loomext/coremods/files.rb, line 88
def mkdir(flags: nil, **opts)
  each_path :action => :mkdir, :flags => flags
end
mv(new_path) click to toggle source
# File lib/loomext/coremods/files.rb, line 51
def mv(new_path)
  each_path do |p|
    shell.capture :mv, p, new_path
  end
end
rm() click to toggle source
# File lib/loomext/coremods/files.rb, line 45
def rm
  each_path do |p|
    shell.capture :rm, "-f", p
  end
end
sudo_append(text="") click to toggle source

TODO: Get the harness working, this is a hack to accomodate append being f'd inside sudo blocks

# File lib/loomext/coremods/files.rb, line 118
def sudo_append(text="")
  if text.index "\n"
    Loom.log.warn "append lines individually until cmd escaping is fixed.... "
  end

  each_path do |p|
    text.each_line do |line|
      loom.x "/bin/echo", "-e", line, :pipe_to => [[:sudo, :tee, "-a", p]]
    end
  end
end
touch() click to toggle source
# File lib/loomext/coremods/files.rb, line 84
def touch
  each_path :action => :touch
end
write(text="") click to toggle source
# File lib/loomext/coremods/files.rb, line 159
def write(text="")
  each_path do |p|
    loom.x :"/bin/echo", "-e", text, :pipe_to => [[:tee, p]]
  end
end