Class: Kanrisuru::Remote::File
- Inherits:
-
Object
- Object
- Kanrisuru::Remote::File
- Includes:
- Enumerable
- Defined in:
- lib/kanrisuru/remote/file.rb
Constant Summary collapse
- WRITE_LINE_COUNT =
5_000
- READ_FILE_SIZE =
3_000_000
Instance Attribute Summary collapse
-
#blocks ⇒ Object
readonly
Returns the value of attribute blocks.
-
#chars ⇒ Object
readonly
Returns the value of attribute chars.
-
#gid ⇒ Object
readonly
Returns the value of attribute gid.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#inode ⇒ Object
readonly
Returns the value of attribute inode.
-
#last_access ⇒ Object
readonly
Returns the value of attribute last_access.
-
#last_changed ⇒ Object
readonly
Returns the value of attribute last_changed.
-
#last_modified ⇒ Object
readonly
Returns the value of attribute last_modified.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#uid ⇒ Object
readonly
Returns the value of attribute uid.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #append(&block) ⇒ Object
- #chmod(mode) ⇒ Object
- #chown(owner, group) ⇒ Object
- #delete ⇒ Object
- #dir? ⇒ Boolean
- #each(&block) ⇒ Object
- #exists? ⇒ Boolean
- #expand_path ⇒ Object
- #file? ⇒ Boolean
- #find_and_append(regex, new_line) ⇒ Object
- #find_and_prepend(regex, new_line) ⇒ Object
- #find_and_remove(regex) ⇒ Object
- #find_and_replace_line(regex, new_line) ⇒ Object
- #find_and_replace_value(regex, value) ⇒ Object
-
#initialize(path, host, backup = nil) ⇒ File
constructor
A new instance of File.
- #prepend(&block) ⇒ Object
- #reload! ⇒ Object
- #rename(new_path) ⇒ Object
- #touch ⇒ Object
- #write(&block) ⇒ Object
- #writeable? ⇒ Boolean
- #zero? ⇒ Boolean
Constructor Details
#initialize(path, host, backup = nil) ⇒ File
Returns a new instance of File.
16 17 18 19 20 21 22 23 24 |
# File 'lib/kanrisuru/remote/file.rb', line 16 def initialize(path, host, backup = nil) @path = path @host = host @backup = backup @file_lines = [] reload! if exists? end |
Instance Attribute Details
#blocks ⇒ Object (readonly)
Returns the value of attribute blocks.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def blocks @blocks end |
#chars ⇒ Object (readonly)
Returns the value of attribute chars.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def chars @chars end |
#gid ⇒ Object (readonly)
Returns the value of attribute gid.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def gid @gid end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def group @group end |
#inode ⇒ Object (readonly)
Returns the value of attribute inode.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def inode @inode end |
#last_access ⇒ Object (readonly)
Returns the value of attribute last_access.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def last_access @last_access end |
#last_changed ⇒ Object (readonly)
Returns the value of attribute last_changed.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def last_changed @last_changed end |
#last_modified ⇒ Object (readonly)
Returns the value of attribute last_modified.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def last_modified @last_modified end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def lines @lines end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def mode @mode end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def path @path end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def size @size end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def type @type end |
#uid ⇒ Object (readonly)
Returns the value of attribute uid.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def uid @uid end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def user @user end |
#words ⇒ Object (readonly)
Returns the value of attribute words.
13 14 15 |
# File 'lib/kanrisuru/remote/file.rb', line 13 def words @words end |
Instance Method Details
#[](index) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/kanrisuru/remote/file.rb', line 41 def [](index) if chunk_read_file? && index < @lines chunk_read_line_by_index(index) elsif index < @lines @file_lines[index] else raise ArgumentError, "Out of bounds index access for #{index}" end end |
#append(&block) ⇒ Object
159 160 161 162 163 164 165 166 167 |
# File 'lib/kanrisuru/remote/file.rb', line 159 def append(&block) return unless writeable? new_lines = [] block.call(new_lines) backup_file if should_backup? write_lines_to_file(new_lines, 'append') end |
#chmod(mode) ⇒ Object
59 60 61 62 |
# File 'lib/kanrisuru/remote/file.rb', line 59 def chmod(mode) result = @host.chmod(@path, mode) update_file_attributes(result) if result.success? end |
#chown(owner, group) ⇒ Object
64 65 66 67 |
# File 'lib/kanrisuru/remote/file.rb', line 64 def chown(owner, group) result = @host.chown(@path, owner: owner, group: group) update_file_attributes(result) if result.success? end |
#delete ⇒ Object
202 203 204 205 206 207 208 |
# File 'lib/kanrisuru/remote/file.rb', line 202 def delete result = @host.unlink(@path) init_file_attirbutes if result.success? result.success? end |
#dir? ⇒ Boolean
51 52 53 |
# File 'lib/kanrisuru/remote/file.rb', line 51 def dir? @host.dir?(@path) end |
#each(&block) ⇒ Object
210 211 212 213 214 215 216 |
# File 'lib/kanrisuru/remote/file.rb', line 210 def each(&block) if chunk_read_file? each_chunk(&block) else @file_lines.each { |line| block.call(line) } end end |
#exists? ⇒ Boolean
33 34 35 |
# File 'lib/kanrisuru/remote/file.rb', line 33 def exists? @host.inode?(@path) end |
#expand_path ⇒ Object
26 27 28 29 30 31 |
# File 'lib/kanrisuru/remote/file.rb', line 26 def return '' unless exists? result = @host.realpath(@path) result.success? ? result.path : '' end |
#file? ⇒ Boolean
37 38 39 |
# File 'lib/kanrisuru/remote/file.rb', line 37 def file? @host.file?(@path) end |
#find_and_append(regex, new_line) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/kanrisuru/remote/file.rb', line 117 def find_and_append(regex, new_line) new_lines = [] each do |existing_line| new_lines << existing_line new_lines << new_line if regex.match(existing_line) end write_lines_to_file(new_lines, 'write') end |
#find_and_prepend(regex, new_line) ⇒ Object
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/kanrisuru/remote/file.rb', line 128 def find_and_prepend(regex, new_line) new_lines = [] each do |existing_line| new_lines << new_line if regex.match(existing_line) new_lines << existing_line end write_lines_to_file(new_lines, 'write') end |
#find_and_remove(regex) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/kanrisuru/remote/file.rb', line 139 def find_and_remove(regex) new_lines = [] each do |existing_line| new_lines << existing_line unless regex.match(existing_line) end write_lines_to_file(new_lines, 'write') end |
#find_and_replace_line(regex, new_line) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/kanrisuru/remote/file.rb', line 103 def find_and_replace_line(regex, new_line) new_lines = [] each do |existing_line| new_lines << if regex.match(existing_line) new_line else existing_line end end write_lines_to_file(new_lines, 'write') end |
#find_and_replace_value(regex, value) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/kanrisuru/remote/file.rb', line 89 def find_and_replace_value(regex, value) new_lines = [] each do |existing_line| new_lines << if regex.match(existing_line) existing_line.gsub(regex, value) else existing_line end end write_lines_to_file(new_lines, 'write') end |
#prepend(&block) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/kanrisuru/remote/file.rb', line 169 def prepend(&block) return unless writeable? new_lines = [] block.call(new_lines) backup_file if should_backup? ## If large file, use tmp file to prepend to if chunk_read_file? tmp_file = "/tmp/kanrisuru-tempfile-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S-%L')}" @host.echo(new_lines.join('\\n'), new_file: tmp_file, backslash: true, mode: 'write') @host.cat(@path, new_file: tmp_file, mode: 'append') @host.cp(tmp_file, @path) @host.rm(tmp_file) reload! else all_lines = new_lines + @file_lines write_lines_to_file(all_lines, 'write') end end |
#reload! ⇒ Object
218 219 220 221 222 223 224 225 |
# File 'lib/kanrisuru/remote/file.rb', line 218 def reload! @writeable = nil @file_lines = [] wc_file stat_file read end |
#rename(new_path) ⇒ Object
193 194 195 |
# File 'lib/kanrisuru/remote/file.rb', line 193 def rename(new_path) # @host.mv() end |
#touch ⇒ Object
197 198 199 200 |
# File 'lib/kanrisuru/remote/file.rb', line 197 def touch result = @host.touch(@path) update_file_attributes(result[0]) if result.success? end |
#write(&block) ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/kanrisuru/remote/file.rb', line 149 def write(&block) return unless writeable? new_lines = [] block.call(new_lines) backup_file if should_backup? write_lines_to_file(new_lines, 'write') end |
#writeable? ⇒ Boolean
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/kanrisuru/remote/file.rb', line 69 def writeable? return @writeable if [true, false].include?(@writeable) if !file? && !zero? @writeable = false return false end current_user = @host.remote_user result = @host.get_user(current_user) raise 'Invalid result' unless result.success? groups = result.groups.map(&:name) @writeable = @mode.other.write? || (@mode.group.write? && groups.include?(@group)) || (@mode.owner.write? && @user == current_user) end |
#zero? ⇒ Boolean
55 56 57 |
# File 'lib/kanrisuru/remote/file.rb', line 55 def zero? @host.empty_file?(@path) end |