module Autoshell::Filestuff
Public Instance Methods
Change directories @param path [String] path to work from @yieldreturn [self] this instance @return result of the block
# File lib/autoshell/filestuff.rb, line 74 def cd(path = nil) unless path.nil? original_dir = working_dir @working_dir = expand(path) end unless Dir.exist? working_dir raise CommandError, "cd: The directory '#{working_dir}' does not exist" end ret = nil Dir.chdir(working_dir) do ret = yield self end @working_dir = original_dir unless path.nil? ret # return end
Copy this working dir to another path @param path [String] destination path to copy to
# File lib/autoshell/filestuff.rb, line 118 def copy_to(path = nil, force: nil) return super if path.nil? unless Dir.exist? working_dir raise CommandError, "copy_to: The directory '#{working_dir}' does not exist" end cp '.', path, force: force self.class.new(expand path) end
copy a path from the working directory to another location @param path [string] local file to copy @param dest [string] destination path to copy to
# File lib/autoshell/filestuff.rb, line 44 def cp(path, dest, force: false) rm(dest) if force raise CommandError, 'Destination exists' if exist?(dest) mkpdir dest FileUtils.cp_r(expand(path), expand(dest)) end
Check if the param is a directory @param path [String] local path to check @return [Boolean]
# File lib/autoshell/filestuff.rb, line 24 def dir?(path = '.') Dir.exist?(expand path) end
Check if the param exists on the system @param path [String] local path to check @return [Boolean]
# File lib/autoshell/filestuff.rb, line 17 def exist?(path = '.') File.exist?(expand path) end
expand a local path for this working directory @param path [String] local path to check @return [String] absolute path
# File lib/autoshell/filestuff.rb, line 10 def expand(path) File.expand_path(path, working_dir) end
Get matching files @param pattern [String] glob expression @return [Array] list of matching files
# File lib/autoshell/filestuff.rb, line 31 def glob(pattern) cd { Dir.glob(pattern) } end
Return an array of filenames @param path [String] path to list @return [Array<String>] all filenames in this directory
# File lib/autoshell/filestuff.rb, line 66 def ls(path = '.') Dir.entries(expand path) end
Get mime for a file @param path [String] local path of the file @return [String] mime type
# File lib/autoshell/filestuff.rb, line 133 def mime(path) MIME::Types.type_for(expand path).first end
Make all the directories for a path @param path [String] local dir path to create
# File lib/autoshell/filestuff.rb, line 103 def mkdir(path = '.') FileUtils.mkdir_p(expand path) self end
Make all the parent directories for a path @param path [String] local dir path to create
# File lib/autoshell/filestuff.rb, line 96 def mkpdir(path = '.') mkdir(File.dirname(expand path)) self end
Move this working dir to another path @param path [String] destination path to move to
# File lib/autoshell/filestuff.rb, line 110 def move_to(path) mv '.', path self.working_dir = expand path self end
Move something from the working directory to another location @param path [string] local file to move @param dest [string] destination path to move to
# File lib/autoshell/filestuff.rb, line 55 def mv(path, dest, force: false) rm(dest) if force raise CommandError, 'Destination exists' if exist?(dest) mkpdir dest FileUtils.mv(expand(path), expand(dest)) end
Read and parse a file @param path [String] local path of the file to read @return [String] contents of file at path
# File lib/autoshell/filestuff.rb, line 140 def read(path) m = mime(path) return read_text(path) unless m return read_json(path) if m.content_type == 'application/json' return read_yaml(path) if m.content_type == 'text/x-yaml' return read_binary(path) if m.binary? read_text(path) end
return the binary contents of a local path @param path [String] local path of the file to read @return [String] binary contents of path
# File lib/autoshell/filestuff.rb, line 182 def read_binary(path) File.binread(expand path) if exist? path end
Read and parse JSON from a local path @param path [String] local path of the file to read @return contents of JSON
# File lib/autoshell/filestuff.rb, line 152 def read_json(path) text = read_text(path) return nil if text.nil? JSON.parse(text) rescue JSON::ParserError nil end
return the contents of a local path @param path [String] local path of the file to read @return [String] text contents of path
# File lib/autoshell/filestuff.rb, line 175 def read_text(path) File.read(expand path) if exist? path end
Read and parse YAML from a local path @param path [String] local path of the file to read @return contents of YAML
# File lib/autoshell/filestuff.rb, line 163 def read_yaml(path) require 'yaml' text = read_text(path) return nil if text.nil? YAML.parse(text).to_h rescue Psych::SyntaxError nil end
Delete a path in the working directory @param path [String] local file to delete
# File lib/autoshell/filestuff.rb, line 37 def rm(path = '.') FileUtils.rm_rf(expand path) end