Module: FileHelper

Included in:
VpsCli::Copy
Defined in:
lib/vps_cli/helpers/file_helper.rb

Overview

Used for copying files, making directories, copying directories etc

Class Method Summary collapse

Class Method Details

.backup_file_not_found?(file, verbose = false) ⇒ Boolean

Checks that a backup file does not exist

Parameters:

  • file (File)

    File to be searched for

  • verbose (Boolean) (defaults to: false)

    Will print to console if verbose == true

Returns:

  • (Boolean)

    Returns true if the file is not found



63
64
65
66
67
68
# File 'lib/vps_cli/helpers/file_helper.rb', line 63

def self.backup_file_not_found?(file, verbose = false)
  return true unless File.exist?(file)

  puts "#{file} exists already. No backup created." if verbose
  false
end

.copy_dir(from, to, interactive = false) ⇒ Object

base method to copy a dir and ask for permission prior to copying

Parameters:

  • from (Dir)

    Directory to copy from

  • to (Dir)

    Directory to copy to

  • interactive (Boolean) (defaults to: false)

    (false) asks whether or not to create the file

See Also:



118
119
120
121
122
# File 'lib/vps_cli/helpers/file_helper.rb', line 118

def self.copy_dir(from, to, interactive = false)
  mkdirs(to)
  to_path = File.join(to, File.basename(from))
  Rake.cp_r(from, to) if overwrite?(to_path, interactive)
end

.copy_dirs(opts = {}) ⇒ Object

Copies directories instead of files, called by copy_all

Parameters:

  • opts (Hash) (defaults to: {})

    The options to copy with

Options Hash (opts):

  • :config_file (File)

    The file from the repo to be copied locally

  • :dot_file (File)

    The file that is currently present locally

  • :backup_file (File)

    The file to which to save the currently present local file

  • :interactive (Boolean)

    Will prompt yes or no for each file it creates

  • :verbose (Boolean)

    Will print more info to terminal if true

See Also:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vps_cli/helpers/file_helper.rb', line 44

def self.copy_dirs(opts = {})
  mkdirs(opts[:local_file])

  if create_backup?(opts)
    copy_dir(opts[:local_file], opts[:backup_file], opts[:interactive])
  end

  Dir.each_child(opts[:config_file]) do |dir|
    dir = File.join(opts[:config_file], dir)

    # copies to local dir
    copy_dir(dir, opts[:local_file], opts[:interactive])
  end
end

.copy_file(from, to, interactive = false) ⇒ Object

base method to copy a file and ask for permission prior to copying

Parameters:

  • from (File)

    File to copy from

  • to (File)

    File to copy to

  • config (VpsCli::Configuration)

    (VpsCli#configuration) uses the default config provided by the VpsCli module

See Also:



108
109
110
# File 'lib/vps_cli/helpers/file_helper.rb', line 108

def self.copy_file(from, to, interactive = false)
  Rake.cp(from, to) if overwrite?(to, interactive)
end

.copy_files(opts = {}) ⇒ Object

Copies files, called by copy_all

Parameters:

  • opts (Hash) (defaults to: {})

    The options to copy with

Options Hash (opts):

  • :config_file (File)

    The file from the repo to be copied locally

  • :local_file (File)

    The file that is currently present locally

  • :backup_file (File)

    The file to which to save the currently present local file

  • :interactive (Boolean)

    Will prompt yes or no for each file it creates

  • :verbose (Boolean)

    Will print more info to terminal if true

See Also:



23
24
25
26
27
28
29
30
31
32
# File 'lib/vps_cli/helpers/file_helper.rb', line 23

def self.copy_files(opts = {})
  # if there is an original dot file & no backup file in the backupdir
  # Copy the dot file to the backup dir
  if create_backup?(opts)
    copy_file(opts[:local_file], opts[:backup_file], opts[:interactive])
  end

  # Copies from vps_cli/dotfiles to the location of the dot_file
  copy_file(opts[:config_file], opts[:local_file], opts[:interactive])
end

.create_backup?(opts = {}) ⇒ Boolean

Helper method for determining whether or not to create a backup file

Parameters:

  • opts (Hash) (defaults to: {})

    options hash

  • [File] (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options

Returns:

  • (Boolean)

    Returns true if there is a dotfile that exists And there is no current backup_file found



77
78
79
80
81
82
83
84
# File 'lib/vps_cli/helpers/file_helper.rb', line 77

def self.create_backup?(opts = {})
  return false unless file_found?(opts[:local_file], opts[:verbose])
  unless backup_file_not_found?(opts[:backup_file], opts[:verbose])
    return false
  end

  true
end

.file_found?(file, verbose = false) ⇒ Boolean

Default way of checking if the dotfile already exists

Parameters:

  • file (File)

    File to be searched for

  • verbose (Boolean) (defaults to: false)

    Will print to console if verbose == true

Returns:

  • (Boolean)

    Returns true if the file exists



90
91
92
93
94
95
# File 'lib/vps_cli/helpers/file_helper.rb', line 90

def self.file_found?(file, verbose = false)
  return true if File.exist?(file)

  puts "#{file} does not exist. No backup created." if verbose
  false
end

.mkdirs(*dirs) ⇒ Object

Helper method for making multiple directories

Parameters:

  • Creates (Dir, Array<Dir>)

    either one, or multiple directories



9
10
11
# File 'lib/vps_cli/helpers/file_helper.rb', line 9

def self.mkdirs(*dirs)
  dirs.flatten.each { |dir| Rake.mkdir_p(dir) unless Dir.exist?(dir) }
end

.overwrite?(file, interactive = false) ⇒ Boolean

asks permission to copy a file

Parameters:

  • file (File)

    The file to copy to

  • interactive (Boolean) (defaults to: false)

    (false) If interactive equals false then automatically overwrite

Returns:

  • (Boolean)

    Decides whether or not the file will be created / overwritted



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/vps_cli/helpers/file_helper.rb', line 130

def self.overwrite?(file, interactive = false)
  return true if interactive == false

  string = "Attempting to overwrite file #{file}" if File.exist?(file)
  string ||= "Attempting to create file #{file}"

  # +string is equivalent to string.dup
  string = +string + ' Is this okay? (Y/N)'

  loop do
    puts string
    input = $stdin.gets.chomp.downcase.to_sym
    return true if input == :y
    return false if input == :n
  end
end

.retrieve_file(directory, name) ⇒ Object



97
98
99
# File 'lib/vps_cli/helpers/file_helper.rb', line 97

def self.retrieve_file(directory, name)
  Dir.children(directory).select { |file| name == file }
end