Module: Bolt::Util
- Defined in:
- lib/bolt/util.rb,
lib/bolt/util/puppet_log_level.rb
Defined Under Namespace
Modules: PuppetLogLevel
Class Method Summary collapse
- .class_name_to_file_name(cls_name) ⇒ Object
-
.deep_clone(obj, cloned = {}) ⇒ Object
Performs a deep_clone, using an identical copy if the cloned structure contains multiple references to the same object and prevents endless recursion.
- .deep_merge(hash1, hash2) ⇒ Object
-
.exec_docker(cmd, env = {}) ⇒ String, Process::Status
Executes a Docker CLI command.
-
.exec_podman(cmd, env = {}) ⇒ String, Process::Status
Executes a Podman CLI command.
-
.file_stat(path) ⇒ Object
This is stubbed for testing validate_file.
- .first_run? ⇒ Boolean
- .first_runs_free ⇒ Object
-
.format_env_vars_for_cli(env_vars) ⇒ String
Formats a map of environment variables to be passed to a command that accepts repeated ‘–env` flags.
-
.get_arg_input(value) ⇒ Object
Gets input for an argument.
-
.module_name(path) ⇒ Object
Accepts a path with either ‘plans’ or ‘tasks’ in it and determines the name of the module.
-
.postwalk_vals(data, skip_top = false, &block) ⇒ Object
Accepts a Data object and returns a copy with all hash and array values modified by the given block.
-
.powershell? ⇒ Boolean
Returns true if running in PowerShell.
-
.prompt_yes_no(prompt, outputter) ⇒ Object
Prompts yes or no, returning true for yes and false for no.
-
.read_arg_file(file) ⇒ Object
Reads a file passed as an argument to a command.
- .read_json_file(path, filename) ⇒ Object
- .read_optional_json_file(path, file_name) ⇒ Object
- .read_optional_yaml_hash(path, file_name) ⇒ Object
- .read_yaml_hash(path, file_name) ⇒ Object
-
.references?(input) ⇒ Boolean
Recursively searches a data structure for plugin references.
- .snake_name_to_class_name(snake_name) ⇒ Object
-
.symbolize_top_level_keys(hsh) ⇒ Object
Accept hash and return hash with top level keys of type “String” converted to symbols.
- .to_code(string) ⇒ Object
- .unix_basename(path) ⇒ Object
- .validate_file(type, path, allow_dir = false) ⇒ Object
-
.walk_keys(data, &block) ⇒ Object
Accepts a Data object and returns a copy with all hash keys modified by block.
-
.walk_vals(data, skip_top = false, &block) ⇒ Object
Accepts a Data object and returns a copy with all hash and array values Arrays and hashes including the initial object are modified before their descendants are.
-
.windows? ⇒ Boolean
Returns true if windows false if not.
- .windows_basename(path) ⇒ Object
Class Method Details
.class_name_to_file_name(cls_name) ⇒ Object
284 285 286 287 288 |
# File 'lib/bolt/util.rb', line 284 def class_name_to_file_name(cls_name) # Note this turns Bolt::CLI -> 'bolt/cli' not 'bolt/c_l_i' # this won't handle Bolt::Inventory2Foo cls_name.gsub(/([a-z])([A-Z])/, '\1_\2').gsub('::', '/').downcase end |
.deep_clone(obj, cloned = {}) ⇒ Object
Performs a deep_clone, using an identical copy if the cloned structure contains multiple references to the same object and prevents endless recursion. Credit to Jan Molic via github.com/rubyworks/facets/blob/master/LICENSE.txt
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/bolt/util.rb', line 216 def deep_clone(obj, cloned = {}) return cloned[obj.object_id] if cloned.include?(obj.object_id) # The `defined?` method will not reliably find the Java::JavaLang::CloneNotSupportedException constant # presumably due to some sort of optimization that short-cuts doing a bunch of Java introspection. # Java::JavaLang::<...> IS defining the constant (via const_missing or const_get magic perhaps) so # it is safe to reference it in the error_types array when a JRuby interpreter is evaluating the code # (detected by RUBY_PLATFORM == `java`). SO instead of conditionally adding the CloneNotSupportedException # constant to the error_types array based on `defined?` detecting the Java::JavaLang constant it is added # based on detecting a JRuby interpreter. # TypeError handles unclonable Ruby ojbects (TrueClass, Fixnum, ...) # CloneNotSupportedException handles uncloneable Java objects (JRuby only) error_types = [TypeError] error_types << Java::JavaLang::CloneNotSupportedException if RUBY_PLATFORM == 'java' begin # We can't recurse on frozen objects to populate them with cloned # data. Instead we store the freeze-state of the original object, # deep_clone, then set the cloned object to frozen if the original # object was frozen frozen = obj.frozen? cl = begin obj.clone(freeze: false) # Some datatypes, such as FalseClass, can't be unfrozen. These # aren't the types we recurse on, so we can leave them frozen rescue ArgumentError => e if e. =~ /can't unfreeze/ obj.clone else raise e end end rescue *error_types cloned[obj.object_id] = obj obj else cloned[obj.object_id] = cl cloned[cl.object_id] = cl case cl when Hash obj.each { |k, v| cl[k] = deep_clone(v, cloned) } when Array cl.collect! { |v| deep_clone(v, cloned) } when Struct obj.each_pair { |k, v| cl[k] = deep_clone(v, cloned) } end cl.instance_variables.each do |var| v = cl.instance_variable_get(var) v_cl = deep_clone(v, cloned) cl.instance_variable_set(var, v_cl) end cl.freeze if frozen cl end end |
.deep_merge(hash1, hash2) ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/bolt/util.rb', line 152 def deep_merge(hash1, hash2) recursive_merge = proc do |_key, h1, h2| if h1.is_a?(Hash) && h2.is_a?(Hash) h1.merge(h2, &recursive_merge) else h2 end end hash1.merge(hash2, &recursive_merge) end |
.exec_docker(cmd, env = {}) ⇒ String, Process::Status
Executes a Docker CLI command. This is useful for running commands as part of this class without having to go through the ‘execute` function and manage pipes.
342 343 344 |
# File 'lib/bolt/util.rb', line 342 def exec_docker(cmd, env = {}) Open3.capture3(env, 'docker', *cmd, { binmode: true }) end |
.exec_podman(cmd, env = {}) ⇒ String, Process::Status
Executes a Podman CLI command. This is useful for running commands as part of this class without having to go through the ‘execute` function and manage pipes.
353 354 355 |
# File 'lib/bolt/util.rb', line 353 def exec_podman(cmd, env = {}) Open3.capture3(env, 'podman', *cmd, { binmode: true }) end |
.file_stat(path) ⇒ Object
This is stubbed for testing validate_file
276 277 278 |
# File 'lib/bolt/util.rb', line 276 def file_stat(path) File.stat(File.(path)) end |
.first_run? ⇒ Boolean
84 85 86 |
# File 'lib/bolt/util.rb', line 84 def first_run? Bolt::Config.user_path && !File.exist?(first_runs_free) end |
.first_runs_free ⇒ Object
80 81 82 |
# File 'lib/bolt/util.rb', line 80 def first_runs_free Bolt::Config.user_path + '.first_runs_free' end |
.format_env_vars_for_cli(env_vars) ⇒ String
Formats a map of environment variables to be passed to a command that accepts repeated ‘–env` flags
362 363 364 365 366 367 |
# File 'lib/bolt/util.rb', line 362 def format_env_vars_for_cli(env_vars) @env_vars = env_vars.each_with_object([]) do |(key, value), acc| acc << "--env" acc << "#{key}=#{value}" end end |
.get_arg_input(value) ⇒ Object
Gets input for an argument.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/bolt/util.rb', line 7 def get_arg_input(value) if value.start_with?('@') file = value.sub(/^@/, '') read_arg_file(file) elsif value == '-' $stdin.read else value end end |
.module_name(path) ⇒ Object
Accepts a path with either ‘plans’ or ‘tasks’ in it and determines the name of the module
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/bolt/util.rb', line 90 def module_name(path) # Remove extra dots and slashes path = Pathname.new(path).cleanpath.to_s fs = File::SEPARATOR regex = Regexp.new("#{fs}plans#{fs}|#{fs}tasks#{fs}") # Only accept paths with '/plans/' or '/tasks/' unless path.match?(regex) msg = "Could not determine module from #{path}. "\ "The path must include 'plans' or 'tasks' directory" raise Bolt::Error.new(msg, 'bolt/modulepath-error') end # Split the path on the first instance of /plans/ or /tasks/ parts = path.split(regex, 2) # Module name is the last entry before 'plans' or 'tasks' modulename = parts[0].split(fs)[-1] filename = File.basename(path).split('.')[0] # Remove "/init.*" if filename is init or just remove the file # extension if filename == 'init' parts[1].chomp!(File.basename(path)) else parts[1].chomp!(File.extname(path)) end # The plan or task name is the rest of the path [modulename, parts[1].split(fs)].flatten.join('::') end |
.postwalk_vals(data, skip_top = false, &block) ⇒ Object
Accepts a Data object and returns a copy with all hash and array values modified by the given block. Descendants are modified before their parents.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/bolt/util.rb', line 197 def postwalk_vals(data, skip_top = false, &block) new_data = case data when Hash data.transform_values { |v| postwalk_vals(v, &block) } when Array data.map { |v| postwalk_vals(v, &block) } else data end if skip_top new_data else yield(new_data) end end |
.powershell? ⇒ Boolean
Returns true if running in PowerShell.
314 315 316 |
# File 'lib/bolt/util.rb', line 314 def powershell? !!ENV['PSModulePath'] end |
.prompt_yes_no(prompt, outputter) ⇒ Object
Prompts yes or no, returning true for yes and false for no.
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/bolt/util.rb', line 381 def prompt_yes_no(prompt, outputter) choices = { 'y' => true, 'yes' => true, 'n' => false, 'no' => false } loop do outputter.print_prompt("#{prompt} ([y]es/[n]o) ") response = $stdin.gets.to_s.downcase.chomp if choices.key?(response) return choices[response] else outputter.print_prompt_error("Invalid response, must pick [y]es or [n]o") end end end |
.read_arg_file(file) ⇒ Object
Reads a file passed as an argument to a command.
19 20 21 22 23 |
# File 'lib/bolt/util.rb', line 19 def read_arg_file(file) File.read(File.(file)) rescue StandardError => e raise Bolt::FileError.new("Error attempting to read #{file}: #{e}", file) end |
.read_json_file(path, filename) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bolt/util.rb', line 25 def read_json_file(path, filename) require 'json' logger = Bolt::Logger.logger(self) path = File.(path) content = JSON.parse(File.read(path)) logger.trace("Loaded #{filename} from #{path}") content rescue Errno::ENOENT raise Bolt::FileError.new("Could not read #{filename} file at #{path}", path) rescue JSON::ParserError => e msg = "Unable to parse #{filename} file at #{path} as JSON: #{e.}" raise Bolt::FileError.new(msg, path) rescue IOError, SystemCallError => e raise Bolt::FileError.new("Could not read #{filename} file at #{path}\n#{e.}", path) end |
.read_optional_json_file(path, file_name) ⇒ Object
43 44 45 |
# File 'lib/bolt/util.rb', line 43 def read_optional_json_file(path, file_name) File.exist?(path) && !File.zero?(path) ? read_yaml_hash(path, file_name) : {} end |
.read_optional_yaml_hash(path, file_name) ⇒ Object
76 77 78 |
# File 'lib/bolt/util.rb', line 76 def read_optional_yaml_hash(path, file_name) File.exist?(path) ? read_yaml_hash(path, file_name) : {} end |
.read_yaml_hash(path, file_name) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bolt/util.rb', line 47 def read_yaml_hash(path, file_name) require 'yaml' logger = Bolt::Logger.logger(self) path = File.(path) content = File.open(path, "r:UTF-8") { |f| YAML.safe_load(f.read) } || {} unless content.is_a?(Hash) raise Bolt::FileError.new( "Invalid content for #{file_name} file at #{path}\nContent should be a Hash or empty, "\ "not #{content.class}", path ) end logger.trace("Loaded #{file_name} from #{path}") content rescue Errno::ENOENT raise Bolt::FileError.new("Could not read #{file_name} file at #{path}", path) rescue Psych::SyntaxError => e raise Bolt::FileError.new("Could not parse #{file_name} file at #{path}, line #{e.line}, "\ "column #{e.column}\n#{e.problem}", path) rescue Psych::Exception => e raise Bolt::FileError.new("Could not parse #{file_name} file at #{path}\n#{e.}", path) rescue IOError, SystemCallError => e raise Bolt::FileError.new("Could not read #{file_name} file at #{path}\n#{e.}", path) end |
.references?(input) ⇒ Boolean
Recursively searches a data structure for plugin references
324 325 326 327 328 329 330 331 332 333 |
# File 'lib/bolt/util.rb', line 324 def references?(input) case input when Hash input.key?('_plugin') || input.values.any? { |v| references?(v) } when Array input.any? { |v| references?(v) } else false end end |
.snake_name_to_class_name(snake_name) ⇒ Object
280 281 282 |
# File 'lib/bolt/util.rb', line 280 def snake_name_to_class_name(snake_name) snake_name.split('_').map(&:capitalize).join end |
.symbolize_top_level_keys(hsh) ⇒ Object
Accept hash and return hash with top level keys of type “String” converted to symbols.
319 320 321 |
# File 'lib/bolt/util.rb', line 319 def symbolize_top_level_keys(hsh) hsh.each_with_object({}) { |(k, v), h| k.is_a?(String) ? h[k.to_sym] = v : h[k] = v } end |
.to_code(string) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/bolt/util.rb', line 120 def to_code(string) case string when Bolt::PAL::YamlPlan::DoubleQuotedString string.value.inspect when Bolt::PAL::YamlPlan::BareString if string.value.start_with?('$') string.value.to_s else "'#{string.value}'" end when Bolt::PAL::YamlPlan::EvaluableString, Bolt::PAL::YamlPlan::CodeLiteral string.value.to_s when String "'#{string}'" when Hash formatted = String.new("{") string.each do |k, v| formatted << "#{to_code(k)} => #{to_code(v)}, " end formatted.chomp!(", ") formatted << "}" formatted when Array formatted = String.new("[") formatted << string.map { |str| to_code(str) }.join(', ') formatted << "]" formatted else string end end |
.unix_basename(path) ⇒ Object
369 370 371 372 |
# File 'lib/bolt/util.rb', line 369 def unix_basename(path) raise Bolt::ValidationError, "path must be a String, received #{path.class} #{path}" unless path.is_a?(String) path.split('/').last end |
.validate_file(type, path, allow_dir = false) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/bolt/util.rb', line 290 def validate_file(type, path, allow_dir = false) stat = file_stat(path) if !stat.readable? raise Bolt::FileError.new("The #{type} '#{path}' is unreadable", path) elsif !stat.file? && (!allow_dir || !stat.directory?) expected = allow_dir ? 'file or directory' : 'file' raise Bolt::FileError.new("The #{type} '#{path}' is not a #{expected}", path) elsif stat.directory? Dir.foreach(path) do |file| next if %w[. ..].include?(file) validate_file(type, File.join(path, file), allow_dir) end end rescue Errno::ENOENT raise Bolt::FileError.new("The #{type} '#{path}' does not exist", path) end |
.walk_keys(data, &block) ⇒ Object
Accepts a Data object and returns a copy with all hash keys modified by block. use &:to_s to stringify keys or &:to_sym to symbolize them
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/bolt/util.rb', line 165 def walk_keys(data, &block) case data when Hash data.each_with_object({}) do |(k, v), acc| v = walk_keys(v, &block) acc[yield(k)] = v end when Array data.map { |v| walk_keys(v, &block) } else data end end |
.walk_vals(data, skip_top = false, &block) ⇒ Object
Accepts a Data object and returns a copy with all hash and array values Arrays and hashes including the initial object are modified before their descendants are.
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/bolt/util.rb', line 182 def walk_vals(data, skip_top = false, &block) data = yield(data) unless skip_top case data when Hash data.transform_values { |v| walk_vals(v, &block) } when Array data.map { |v| walk_vals(v, &block) } else data end end |
.windows? ⇒ Boolean
Returns true if windows false if not.
309 310 311 |
# File 'lib/bolt/util.rb', line 309 def windows? !!File::ALT_SEPARATOR end |
.windows_basename(path) ⇒ Object
374 375 376 377 |
# File 'lib/bolt/util.rb', line 374 def windows_basename(path) raise Bolt::ValidationError, "path must be a String, received #{path.class} #{path}" unless path.is_a?(String) path.split(%r{[/\\]}).last end |