Module: Kitchen::Util
- Defined in:
- lib/kitchen/util.rb
Overview
Stateless utility methods used in different contexts. Essentially a mini PassiveSupport library.
Class Method Summary collapse
- .camel_case(a_string) ⇒ Object
-
.duration(total) ⇒ String
Returns a formatted string representing a duration in seconds.
-
.from_logger_level(const) ⇒ Symbol
Returns the symbol represenation of a logging levels for a given standard library Logger::Severity constant.
-
.list_directory(path, include_dot: false, recurse: false) ⇒ Object
Lists the contents of the given directory.
-
.outdent!(string) ⇒ String
Modifes the given string to strip leading whitespace on each line, the amount which is calculated by using the first line of text.
-
.safe_glob(path, pattern, *flags) ⇒ Object
Similar to Dir.glob.
-
.shell_helpers ⇒ String
Returns a set of Bourne Shell (AKA /bin/sh) compatible helper functions.
- .snake_case(a_string) ⇒ Object
-
.stringified_hash(obj) ⇒ Object
Returns a new Hash with all key values coerced to strings.
-
.symbolized_hash(obj) ⇒ Object
Returns a new Hash with all key values coerced to symbols.
-
.to_logger_level(symbol) ⇒ Integer
Returns the standard library Logger level constants for a given symbol representation.
-
.wrap_command(cmd) ⇒ String
Generates a command (or series of commands) wrapped so that it can be invoked on a remote instance or locally.
Class Method Details
.camel_case(a_string) ⇒ Object
212 213 214 |
# File 'lib/kitchen/util.rb', line 212 def self.camel_case(a_string) Thor::Util.camel_case(a_string) end |
.duration(total) ⇒ String
Returns a formatted string representing a duration in seconds.
96 97 98 99 100 101 |
# File 'lib/kitchen/util.rb', line 96 def self.duration(total) total = 0 if total.nil? minutes = (total / 60).to_i seconds = (total - (minutes * 60)) format("(%dm%.2fs)", minutes, seconds) end |
.from_logger_level(const) ⇒ Symbol
Returns the symbol represenation of a logging levels for a given standard library Logger::Severity constant.
48 49 50 51 52 53 54 55 56 |
# File 'lib/kitchen/util.rb', line 48 def self.from_logger_level(const) case const when Logger::DEBUG then :debug when Logger::INFO then :info when Logger::WARN then :warn when Logger::ERROR then :error else :fatal end end |
.list_directory(path, include_dot: false, recurse: false) ⇒ Object
You should prefer this method to using Dir.glob directly. The reason is
Dir.chdir is applied to the process, thus it is not thread-safe
Lists the contents of the given directory. path will be prepended to the list returned. ‘.’ and ‘..’ are never returned.
because Dir.glob behaves strangely on Windows. It wont accept ‘' and doesn’t like fake directories (C:Documents and Settings) It also does not do any sort of error checking, so things one would expect to fail just return an empty list
and must be synchronized.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/kitchen/util.rb', line 164 def self.list_directory(path, include_dot: false, recurse: false) # Things (such as tests) are relying on this to not blow up if # the directory does not exist return [] if !Dir.exist?(path) Kitchen.mutex_chdir.synchronize do Dir.chdir(path) do glob_pattern = if recurse "**/*" else "*" end flags = if include_dot [File::FNM_DOTMATCH] else [] end Dir.glob(glob_pattern, *flags) .reject { |f| [".", ".."].include?(f) } .map { |f| File.join(path, f) } end end end |
.outdent!(string) ⇒ String
Modifes the given string to strip leading whitespace on each line, the amount which is calculated by using the first line of text.
133 134 135 |
# File 'lib/kitchen/util.rb', line 133 def self.outdent!(string) string.gsub!(/^ {#{string.index(/[^ ]/)}}/, "") end |
.safe_glob(path, pattern, *flags) ⇒ Object
Dir.chdir is applied to the process, thus it is not thread-safe
Similar to Dir.glob.
The difference is this function forces you to specify where to glob from. You should glob from the path closest to what you want. The reason for this is because if you have symlinks on windows of any kind, Dir.glob will not traverse them.
and must be synchronized.
202 203 204 205 206 207 208 209 210 |
# File 'lib/kitchen/util.rb', line 202 def self.safe_glob(path, pattern, *flags) return [] if !Dir.exist?(path) Kitchen.mutex_chdir.synchronize do Dir.chdir(path) do Dir.glob(pattern, *flags).map { |f| File.join(path, f) } end end end |
.shell_helpers ⇒ String
Returns a set of Bourne Shell (AKA /bin/sh) compatible helper functions. This function is usually called inline in a string that will be executed remotely on a test instance.
142 143 144 145 146 |
# File 'lib/kitchen/util.rb', line 142 def self.shell_helpers IO.read(File.join( File.dirname(__FILE__), %w{.. .. support download_helpers.sh} )) end |
.snake_case(a_string) ⇒ Object
216 217 218 |
# File 'lib/kitchen/util.rb', line 216 def self.snake_case(a_string) Thor::Util.snake_case(a_string) end |
.stringified_hash(obj) ⇒ Object
Returns a new Hash with all key values coerced to strings. All keys within a Hash are coerced by calling #to_s and hashes with arrays and other hashes are traversed.
82 83 84 85 86 87 88 89 90 |
# File 'lib/kitchen/util.rb', line 82 def self.stringified_hash(obj) if obj.is_a?(Hash) obj.inject({}) { |h, (k, v)| h[k.to_s] = stringified_hash(v); h } elsif obj.is_a?(Array) obj.inject([]) { |a, e| a << stringified_hash(e); a } else obj end end |
.symbolized_hash(obj) ⇒ Object
Returns a new Hash with all key values coerced to symbols. All keys within a Hash are coerced by calling #to_sym and hashes within arrays and other hashes are traversed.
65 66 67 68 69 70 71 72 73 |
# File 'lib/kitchen/util.rb', line 65 def self.symbolized_hash(obj) if obj.is_a?(Hash) obj.inject({}) { |h, (k, v)| h[k.to_sym] = symbolized_hash(v); h } elsif obj.is_a?(Array) obj.inject([]) { |a, e| a << symbolized_hash(e); a } else obj end end |
.to_logger_level(symbol) ⇒ Integer
Returns the standard library Logger level constants for a given symbol representation.
35 36 37 38 39 |
# File 'lib/kitchen/util.rb', line 35 def self.to_logger_level(symbol) return nil unless [:debug, :info, :warn, :error, :fatal].include?(symbol) Logger.const_get(symbol.to_s.upcase) end |
.wrap_command(cmd) ⇒ String
Generates a command (or series of commands) wrapped so that it can be invoked on a remote instance or locally.
This method uses the Bourne shell (/bin/sh) to maximize the chance of cross platform portability on Unixlike systems.
111 112 113 114 115 116 117 |
# File 'lib/kitchen/util.rb', line 111 def self.wrap_command(cmd) cmd = "false" if cmd.nil? cmd = "true" if cmd.to_s.empty? cmd = cmd.sub(/\n\Z/, "") if cmd =~ /\n\Z/ "sh -c '\n#{cmd}\n'" end |