Class: Hiera::Backend::Eyaml::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/backend/eyaml/utils.rb

Class Method Summary collapse

Class Method Details

.camelcase(string) ⇒ Object



22
23
24
25
# File 'lib/hiera/backend/eyaml/utils.rb', line 22

def self.camelcase string
  return string if string !~ /_/ && string =~ /[A-Z]+.*/
  string.split('_').map{|e| e.capitalize}.join
end

.confirm?(message) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
# File 'lib/hiera/backend/eyaml/utils.rb', line 13

def self.confirm? message
  result = ask("#{message} (y/N): ")
  if result.downcase == "y" or result.downcase == "yes"
    true
  else
    false
  end
end

.ensure_key_dir_exists(key_file) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hiera/backend/eyaml/utils.rb', line 66

def self.ensure_key_dir_exists key_file
  key_dir = File.dirname key_file

  unless File.directory? key_dir
    begin
      Dir.mkdir key_dir
      puts "Created key directory: #{key_dir}"
    rescue
      raise StandardError, "Cannot create key directory: #{key_dir}"
    end
  end

end

.find_editorObject

Raises:

  • (StandardError)


27
28
29
30
31
32
# File 'lib/hiera/backend/eyaml/utils.rb', line 27

def self.find_editor
  editor = ENV['EDITOR']
  editor ||= %w{ /usr/bin/sensible-editor /usr/bin/editor /usr/bin/vim /usr/bin/vi }.collect {|e| e if FileTest.executable? e}.compact.first
  raise StandardError, "Editor not found. Please set your EDITOR env variable" if editor.nil?
  editor
end

.read_passwordObject



9
10
11
# File 'lib/hiera/backend/eyaml/utils.rb', line 9

def self.read_password
  ask("Enter password: ") {|q| q.echo = "*" }
end

.secure_file_delete(args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/hiera/backend/eyaml/utils.rb', line 34

def self.secure_file_delete args
  file = File.open(args[:file], 'r+')
  num_bytes = args[:num_bytes]
  [0xff, 0x55, 0xaa, 0x00].each do |byte|
    file.seek(0, IO::SEEK_SET)
    num_bytes.times { file.print(byte.chr) }
    file.fsync
  end
  File.delete file
end

.write_important_file(args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/hiera/backend/eyaml/utils.rb', line 55

def self.write_important_file args
  filename = args[ :filename ]
  content = args[ :content ]
  if File.file? "#{filename}"
     raise StandardError, "User aborted" unless Utils::confirm? "Are you sure you want to overwrite \"#{filename}\"?"
  end
  open( "#{filename}", "w" ) do |io|
    io.write(content)
  end
end

.write_tempfile(data_to_write) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/hiera/backend/eyaml/utils.rb', line 45

def self.write_tempfile data_to_write
  file = Tempfile.open('eyaml_edit')
  path = file.path

  file.puts data_to_write
  file.close

  path
end