Module: Autoshell::Filestuff

Included in:
Base
Defined in:
lib/autoshell/filestuff.rb

Instance Method Summary collapse

Instance Method Details

#cd(path = nil) ⇒ Object

Change directories

Parameters:

  • path (String) (defaults to: nil)

    path to work from

Yield Returns:

  • (self)

    this instance

Returns:

  • result of the block



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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) ⇒ Object

Copy this working dir to another path

Parameters:

  • path (String) (defaults to: nil)

    destination path to copy to



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

copy a path from the working directory to another location

Parameters:

  • path (string)

    local file to copy

  • dest (string)

    destination path to copy to

Raises:



44
45
46
47
48
49
50
# 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 = '.') ⇒ Boolean

Check if the param is a directory

Parameters:

  • path (String) (defaults to: '.')

    local path to check

Returns:

  • (Boolean)


24
25
26
# File 'lib/autoshell/filestuff.rb', line 24

def dir?(path = '.')
  Dir.exist?(expand path)
end

#exist?(path = '.') ⇒ Boolean

Check if the param exists on the system

Parameters:

  • path (String) (defaults to: '.')

    local path to check

Returns:

  • (Boolean)


17
18
19
# File 'lib/autoshell/filestuff.rb', line 17

def exist?(path = '.')
  File.exist?(expand path)
end

#expand(path) ⇒ String

expand a local path for this working directory

Parameters:

  • path (String)

    local path to check

Returns:

  • (String)

    absolute path



10
11
12
# File 'lib/autoshell/filestuff.rb', line 10

def expand(path)
  File.expand_path(path, working_dir)
end

#glob(pattern) ⇒ Array

Get matching files

Parameters:

  • pattern (String)

    glob expression

Returns:

  • (Array)

    list of matching files



31
32
33
# File 'lib/autoshell/filestuff.rb', line 31

def glob(pattern)
  cd { Dir.glob(pattern) }
end

#ls(path = '.') ⇒ Array<String>

Return an array of filenames

Parameters:

  • path (String) (defaults to: '.')

    path to list

Returns:

  • (Array<String>)

    all filenames in this directory



66
67
68
# File 'lib/autoshell/filestuff.rb', line 66

def ls(path = '.')
  Dir.entries(expand path)
end

#mime(path) ⇒ String

Get mime for a file

Parameters:

  • path (String)

    local path of the file

Returns:

  • (String)

    mime type



133
134
135
# File 'lib/autoshell/filestuff.rb', line 133

def mime(path)
  MIME::Types.type_for(expand path).first
end

#mkdir(path = '.') ⇒ Object

Make all the directories for a path

Parameters:

  • path (String) (defaults to: '.')

    local dir path to create



103
104
105
106
# File 'lib/autoshell/filestuff.rb', line 103

def mkdir(path = '.')
  FileUtils.mkdir_p(expand path)
  self
end

#mkpdir(path = '.') ⇒ Object

Make all the parent directories for a path

Parameters:

  • path (String) (defaults to: '.')

    local dir path to create



96
97
98
99
# File 'lib/autoshell/filestuff.rb', line 96

def mkpdir(path = '.')
  mkdir(File.dirname(expand path))
  self
end

#move_to(path) ⇒ Object

Move this working dir to another path

Parameters:

  • path (String)

    destination path to move to



110
111
112
113
114
# 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) ⇒ Object

Move something from the working directory to another location

Parameters:

  • path (string)

    local file to move

  • dest (string)

    destination path to move to

Raises:



55
56
57
58
59
60
61
# 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) ⇒ String

Read and parse a file

Parameters:

  • path (String)

    local path of the file to read

Returns:

  • (String)

    contents of file at path



140
141
142
143
144
145
146
147
# 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) ⇒ String

return the binary contents of a local path

Parameters:

  • path (String)

    local path of the file to read

Returns:

  • (String)

    binary contents of path



182
183
184
# File 'lib/autoshell/filestuff.rb', line 182

def read_binary(path)
  File.binread(expand path) if exist? path
end

#read_json(path) ⇒ Object

Read and parse JSON from a local path

Parameters:

  • path (String)

    local path of the file to read

Returns:

  • contents of JSON



152
153
154
155
156
157
158
# 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) ⇒ String

return the contents of a local path

Parameters:

  • path (String)

    local path of the file to read

Returns:

  • (String)

    text contents of path



175
176
177
# File 'lib/autoshell/filestuff.rb', line 175

def read_text(path)
  File.read(expand path) if exist? path
end

#read_yaml(path) ⇒ Object

Read and parse YAML from a local path

Parameters:

  • path (String)

    local path of the file to read

Returns:

  • contents of YAML



163
164
165
166
167
168
169
170
# 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 = '.') ⇒ Object

Delete a path in the working directory

Parameters:

  • path (String) (defaults to: '.')

    local file to delete



37
38
39
# File 'lib/autoshell/filestuff.rb', line 37

def rm(path = '.')
  FileUtils.rm_rf(expand path)
end