Class: KubsCLI::FileHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/kubs_cli/file_helper.rb

Overview

Used for copying files within kubs_cli

Instance Method Summary collapse

Instance Method Details

#copy(from:, to:) ⇒ Object

Copies a file or directory from one spot to another

Parameters:

  • from (Dir, File)

    Where to copy from

  • to (Dir, File)

    Where to copy to

Returns:

  • void



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kubs_cli/file_helper.rb', line 13

def copy(from:, to:)
  return if file_does_not_exist(from)

  to_dir = File.dirname(File.expand_path(to))
  FileUtils.mkdir_p(to_dir) unless Dir.exist?(to_dir)
  return FileUtils.cp(from, to) unless File.directory?(from)

  FileUtils.mkdir_p(to)

  Dir["#{from}/*"].each do |dir|
    FileUtils.cp_r(dir, to) # next if File.directory?(dir)

    # FileUtils.cp(dir, to)
  end
end

#load_yaml(file) ⇒ Hash

Loads a YAML file

Parameters:

  • file (File)

    Yaml formatted file

Returns:

  • (Hash)

    Returns a hash from a yaml document



39
40
41
42
43
# File 'lib/kubs_cli/file_helper.rb', line 39

def load_yaml(file)
  YAML.load_file(file)
rescue StandardError => e
  KubsCLI.add_error(e: e, msg: "Unable to parse your YAML file - #{file}")
end

#mkdirs(*dirs) ⇒ Object

Creates dirs using Rake.mkdir_p if it does not exist

Parameters:

  • dirs (Array<String>)

    The names of the dirs to create

Returns:

  • void



32
33
34
# File 'lib/kubs_cli/file_helper.rb', line 32

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