Module: LoomExt::CoreMods::Files::Actions

Defined in:
lib/loomext/coremods/files.rb

Instance Method Summary collapse

Instance Method Details

#append(text = "") ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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

#catObject



39
40
41
42
43
# 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) ⇒ Object



76
77
78
79
80
81
82
# 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) ⇒ Object

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)



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
# 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: /./) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
# 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) ⇒ Object



88
89
90
# File 'lib/loomext/coremods/files.rb', line 88

def mkdir(flags: nil, **opts)
  each_path :action => :mkdir, :flags => flags
end

#mv(new_path) ⇒ Object



51
52
53
54
55
# File 'lib/loomext/coremods/files.rb', line 51

def mv(new_path)
  each_path do |p|
    shell.capture :mv, p, new_path
  end
end

#rmObject



45
46
47
48
49
# File 'lib/loomext/coremods/files.rb', line 45

def rm
  each_path do |p|
    shell.capture :rm, "-f", p
  end
end

#sudo_append(text = "") ⇒ Object

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



118
119
120
121
122
123
124
125
126
127
128
# 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

#touchObject



84
85
86
# File 'lib/loomext/coremods/files.rb', line 84

def touch
  each_path :action => :touch
end

#write(text = "") ⇒ Object



159
160
161
162
163
# 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