module Autoshell::Filestuff

Public Instance Methods

cd(path = nil) { |self| ... } click to toggle source

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_to(path = nil, force: nil) click to toggle source

Copy this working dir to another path @param path [String] destination path to copy to

Calls superclass method
# 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
cp(path, dest, force: false) click to toggle source

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
dir?(path = '.') click to toggle source

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
exist?(path = '.') click to toggle source

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(path) click to toggle source

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
glob(pattern) click to toggle source

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
ls(path = '.') click to toggle source

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
mime(path) click to toggle source

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
mkdir(path = '.') click to toggle source

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
mkpdir(path = '.') click to toggle source

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_to(path) click to toggle source

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
mv(path, dest, force: false) click to toggle source

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(path) click to toggle source

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
read_binary(path) click to toggle source

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_json(path) click to toggle source

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
read_text(path) click to toggle source

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_yaml(path) click to toggle source

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
rm(path = '.') click to toggle source

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