Module: Mrsk::Utils
Instance Method Summary collapse
-
#abbreviate_version(version) ⇒ Object
Abbreviate a git revhash for concise display.
-
#argumentize(argument, attributes, redacted: false) ⇒ Object
Return a list of escaped shell arguments using the same named argument against the passed attributes (hash or array).
-
#argumentize_env_with_secrets(env) ⇒ Object
Return a list of shell arguments using the same named argument against the passed attributes, but redacts and expands secrets.
-
#escape_shell_value(value) ⇒ Object
Escape a value to make it safe for shell use.
-
#flatten_args(args) ⇒ Object
Flattens a one-to-many structure into an array of two-element arrays each containing a key-value pair.
-
#optionize(args, with: nil) ⇒ Object
Returns a list of shell-dashed option arguments.
-
#redact(arg) ⇒ Object
Copied from SSHKit::Backend::Abstract#redact to be available inside Commands classes.
Instance Method Details
#abbreviate_version(version) ⇒ Object
Abbreviate a git revhash for concise display
53 54 55 |
# File 'lib/mrsk/utils.rb', line 53 def abbreviate_version(version) version[0...7] if version end |
#argumentize(argument, attributes, redacted: false) ⇒ Object
Return a list of escaped shell arguments using the same named argument against the passed attributes (hash or array).
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/mrsk/utils.rb', line 5 def argumentize(argument, attributes, redacted: false) Array(attributes).flat_map do |key, value| if value.present? escaped_pair = [ key, escape_shell_value(value) ].join("=") [ argument, redacted ? redact(escaped_pair) : escaped_pair ] else [ argument, key ] end end end |
#argumentize_env_with_secrets(env) ⇒ Object
Return a list of shell arguments using the same named argument against the passed attributes, but redacts and expands secrets.
18 19 20 21 22 23 24 |
# File 'lib/mrsk/utils.rb', line 18 def argumentize_env_with_secrets(env) if (secrets = env["secret"]).present? argumentize("-e", secrets.to_h { |key| [ key, ENV.fetch(key) ] }, redacted: true) + argumentize("-e", env["clear"]) else argumentize "-e", env.fetch("clear", env) end end |
#escape_shell_value(value) ⇒ Object
Escape a value to make it safe for shell use.
48 49 50 |
# File 'lib/mrsk/utils.rb', line 48 def escape_shell_value(value) value.to_s.dump.gsub(/`/, '\\\\`') end |
#flatten_args(args) ⇒ Object
Flattens a one-to-many structure into an array of two-element arrays each containing a key-value pair
38 39 40 |
# File 'lib/mrsk/utils.rb', line 38 def flatten_args(args) args.flat_map { |key, value| value.try(:map) { |entry| [key, entry] } || [ [ key, value ] ] } end |
#optionize(args, with: nil) ⇒ Object
Returns a list of shell-dashed option arguments. If the value is true, it’s treated like a value-less option.
27 28 29 30 31 32 33 34 35 |
# File 'lib/mrsk/utils.rb', line 27 def optionize(args, with: nil) = if with flatten_args(args).collect { |(key, value)| value == true ? "--#{key}" : "--#{key}#{with}#{escape_shell_value(value)}" } else flatten_args(args).collect { |(key, value)| [ "--#{key}", value == true ? nil : escape_shell_value(value) ] } end .flatten.compact end |
#redact(arg) ⇒ Object
Copied from SSHKit::Backend::Abstract#redact to be available inside Commands classes
43 44 45 |
# File 'lib/mrsk/utils.rb', line 43 def redact(arg) # Used in execute_command to hide redact() args a user passes in arg.to_s.extend(SSHKit::Redaction) # to_s due to our inability to extend Integer, etc end |