Class: Hiera::Backend::Eyaml::Utils
- Inherits:
-
Object
- Object
- Hiera::Backend::Eyaml::Utils
- Defined in:
- lib/hiera/backend/eyaml/utils.rb
Class Method Summary collapse
- .camelcase(string) ⇒ Object
- .confirm?(message) ⇒ Boolean
- .debug(messageinfo) ⇒ Object
- .ensure_key_dir_exists(key_file) ⇒ Object
- .find_all_subclasses_of(args) ⇒ Object
- .find_closest_class(args) ⇒ Object
- .find_editor ⇒ Object
- .hiera? ⇒ Boolean
- .info(messageinfo) ⇒ Object
- .read_password ⇒ Object
- .require_dir(classdir) ⇒ Object
- .secure_file_delete(args) ⇒ Object
- .snakecase(string) ⇒ Object
- .structure_message(messageinfo) ⇒ Object
- .warn(messageinfo) ⇒ Object
- .write_important_file(args) ⇒ Object
- .write_tempfile(data_to_write) ⇒ Object
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
14 15 16 17 18 19 20 21 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 14 def self.confirm? result = ask("#{message} (y/N): ") if result.downcase == "y" or result.downcase == "yes" true else false end end |
.debug(messageinfo) ⇒ Object
159 160 161 162 163 164 165 166 167 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 159 def self.debug = self. = "[#{message[:from]}] #{message[:msg]}" if self.hiera? Hiera.debug if Eyaml.verbosity_level > 1 else STDERR.puts if Eyaml.verbosity_level > 1 end end |
.ensure_key_dir_exists(key_file) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 76 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_all_subclasses_of(args) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 115 def self.find_all_subclasses_of args parent_class = args[ :parent_class ] constants = parent_class.constants candidates = [] constants.each do | candidate | candidates << candidate.to_s.split('::').last if parent_class.const_get(candidate).class.to_s == "Class" end candidates end |
.find_closest_class(args) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 90 def self.find_closest_class args parent_class = args[ :parent_class ] class_name = args[ :class_name ] constants = parent_class.constants candidates = [] constants.each do | candidate | candidates << candidate.to_s if candidate.to_s.downcase == class_name.downcase end if candidates.count > 0 parent_class.const_get candidates.first else nil end end |
.find_editor ⇒ Object
33 34 35 36 37 38 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 33 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 |
.hiera? ⇒ Boolean
125 126 127 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 125 def self.hiera? "hiera".eql? Eyaml::Options[:source] end |
.info(messageinfo) ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 149 def self.info = self. = "[#{message[:from]}] #{message[:msg]}" if self.hiera? Hiera.debug if Eyaml.verbosity_level > 0 else STDERR.puts if Eyaml.verbosity_level > 0 end end |
.read_password ⇒ Object
10 11 12 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 10 def self.read_password ask("Enter password: ") {|q| q.echo = "*" } end |
.require_dir(classdir) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 105 def self.require_dir classdir num_class_hierarchy_levels = self.to_s.split("::").count - 1 root_folder = File.dirname(__FILE__) + "/" + Array.new(num_class_hierarchy_levels).fill("..").join("/") class_folder = root_folder + "/" + classdir Dir[File.("#{class_folder}/*.rb")].uniq.each do |file| # puts "Requiring file: #{file}" require file end end |
.secure_file_delete(args) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 40 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 |
.snakecase(string) ⇒ Object
28 29 30 31 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 28 def self.snakecase string return string if string !~ /[A-Z]/ string.split(/(?=[A-Z])/).collect {|x| x.downcase}.join("_") end |
.structure_message(messageinfo) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 129 def self. = {:from => "hiera-eyaml-core"} case .class.to_s when 'Hash' .merge!() else .merge!({:msg => .to_s}) end end |
.warn(messageinfo) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 139 def self.warn = self. = "[#{message[:from]}] !!! #{message[:msg]}" if self.hiera? Hiera.warn else STDERR.puts end end |
.write_important_file(args) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 63 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
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/hiera/backend/eyaml/utils.rb', line 51 def self.write_tempfile data_to_write file = Tempfile.open('eyaml_edit') path = file.path file.close! file = File.open(path, "w") file.puts data_to_write file.close path end |