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



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

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

.confirm?(message) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.debug(message) ⇒ Object



87
88
89
# File 'lib/hiera/backend/eyaml/utils.rb', line 87

def self.debug message
  STDERR.puts message if Eyaml::Options[:debug]
end

.ensure_key_dir_exists(key_file) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hiera/backend/eyaml/utils.rb', line 69

def self.ensure_key_dir_exists key_file
  key_dir = File.dirname key_file

  unless File.directory? key_dir
    begin
      FileUtils.mkdir_p key_dir
      Utils::info "Created key directory: #{key_dir}"
    rescue
      raise StandardError, "Cannot create key directory: #{key_dir}"
    end
  end

end

.find_editorObject

Raises:

  • (StandardError)


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

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

.info(message) ⇒ Object



83
84
85
# File 'lib/hiera/backend/eyaml/utils.rb', line 83

def self.info message
  STDERR.puts message unless Eyaml::Options[:quiet]
end

.read_passwordObject



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

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

.secure_file_delete(args) ⇒ Object



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

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 args[:file]
end

.write_important_file(args) ⇒ Object



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

def self.write_important_file args
  filename = args[ :filename ]
  content = args[ :content ]
  mode = args[ :mode ]
  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
  File.chmod( mode, filename ) unless mode.nil?
end

.write_tempfile(data_to_write) ⇒ Object



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

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

  file.puts data_to_write
  file.close

  path
end