Module: HttpdConfigmapGenerator::Base::FileHelper

Included in:
HttpdConfigmapGenerator::Base
Defined in:
lib/httpd_configmap_generator/base/file_helper.rb

Instance Method Summary collapse

Instance Method Details

#cp_template(file, src_dir, dest_dir = "/") ⇒ Object


11
12
13
14
15
16
17
18
19
# File 'lib/httpd_configmap_generator/base/file_helper.rb', line 11

def cp_template(file, src_dir, dest_dir = "/")
  src_path  = path_join(src_dir, file)
  dest_path = path_join(dest_dir, file.gsub(".erb", ""))
  if src_path.to_s.include?(".erb")
    File.write(dest_path, ERB.new(File.read(src_path), nil, '-').result(binding))
  else
    FileUtils.cp(src_path, dest_path)
  end
end

#create_target_directory(file_path) ⇒ Object


32
33
34
35
36
37
# File 'lib/httpd_configmap_generator/base/file_helper.rb', line 32

def create_target_directory(file_path)
  dirname = File.dirname(file_path)
  return if File.exist?(dirname)
  debug_msg("Creating directory #{dirname} ...")
  FileUtils.mkdir_p(dirname)
end

#delete_target_file(file_path) ⇒ Object


21
22
23
24
25
26
27
28
29
30
# File 'lib/httpd_configmap_generator/base/file_helper.rb', line 21

def delete_target_file(file_path)
  if File.exist?(file_path)
    if opts[:force]
      info_msg("File #{file_path} exists, forcing a delete")
      File.delete(file_path)
    else
      raise "File #{file_path} already exist"
    end
  end
end

#file_binary?(file) ⇒ Boolean

Returns:

  • (Boolean)

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/httpd_configmap_generator/base/file_helper.rb', line 50

def file_binary?(file)
  data = File.read(file)
  ascii = control = binary = total = 0
  data[0..512].each_byte do |c|
    total += 1
    if c < 32
      control += 1
    elsif c >= 32 && c <= 128
      ascii += 1
    else
      binary += 1
    end
  end
  control.to_f / ascii > 0.1 || binary.to_f / ascii > 0.05
end

#path_join(*args) ⇒ Object


44
45
46
47
48
# File 'lib/httpd_configmap_generator/base/file_helper.rb', line 44

def path_join(*args)
  path = Pathname.new(args.shift)
  args.each { |path_seg| path = path.join("./#{path_seg}") }
  path
end

#rm_file(file, dir = "/") ⇒ Object


39
40
41
42
# File 'lib/httpd_configmap_generator/base/file_helper.rb', line 39

def rm_file(file, dir = "/")
  path = path_join(dir, file)
  File.delete(path) if File.exist?(path)
end

#template_directoryObject


6
7
8
9
# File 'lib/httpd_configmap_generator/base/file_helper.rb', line 6

def template_directory
  @template_directory ||=
    Pathname.new(Gem::Specification.find_by_name("httpd_configmap_generator").full_gem_path).join("templates")
end