opswalrus 1.0.76 → 1.0.77

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c14f1f9d393c44f6daefbb576afb28a273569a7fc9e46f211324630745cbc2ff
4
- data.tar.gz: c7e199f0fc237c11cab49f97c4872ff685bb34029678a62467735848741eb0d7
3
+ metadata.gz: c19f0877541baca3fa57340c300382e8819e56d9bfc2225ca1f4a888c0df9fb6
4
+ data.tar.gz: 279d36f24ff0861115ca06e4201d907c84082b764f308faceee9c9c4420a2ef2
5
5
  SHA512:
6
- metadata.gz: 25c1f27303555141b952fa3450dd3c6b1821908c3d97b7df73a892af8808f7d10ad2fe49cfcb62808b503128e3ed623aaf8c93d3040316b7bf286834e48117b1
7
- data.tar.gz: 165658a4614110321554f4461882d6cfb44ef9618f804354c65ce3c4f45d273bbb78ea84a9d9aab91d158c164a9ea8b1ab1b7ef216e8f0d8efdfc50a0739938f
6
+ metadata.gz: 54b958066621d691e460702e41388b76c34c3a9cc379f1fe431e52f70509c581a81f8d1a9d7c11ea15f5d5406c5862659534b5c7b18aa086494e0ac3a2b5c564
7
+ data.tar.gz: 0771e13131d4db57fdc9f730ed80a286aaaf80409b35cdbdfe2091c0e80b35d321ccba1fbad6c89a6ce28438207330bbd944833618aef762ab42fb6ab7b98641
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- opswalrus (1.0.76)
4
+ opswalrus (1.0.77)
5
5
  activesupport (~> 7.0)
6
6
  bcrypt_pbkdf (~> 1.1)
7
7
  binding_of_caller (~> 1.0)
@@ -6,7 +6,7 @@ module OpsWalrus
6
6
  STANDARD_SUDO_PASSWORD_PROMPT = /\[sudo\] password for .*?:\s*/
7
7
  STANDARD_SSH_PASSWORD_PROMPT = /.*?@.*?'s password:\s*/
8
8
 
9
- attr_accessor :input_mappings # Hash[ String | Regex => String ]
9
+ attr_accessor :input_mappings # Hash[ String | Regex => (String | Proc) ]
10
10
 
11
11
  def initialize(mapping)
12
12
  @input_mappings = mapping
@@ -39,7 +39,7 @@ module OpsWalrus
39
39
  # temporarily adds the specified input mapping to the interaction handler while the given block is being evaluated
40
40
  # when the given block returns, then the temporary mapping is removed from the interaction handler
41
41
  #
42
- # mapping : Hash[ String | Regex => String ] | Nil
42
+ # mapping : Hash[ String | Regex => (String | Proc) ] | Nil
43
43
  def with_mapping(mapping = nil, sudo_password: nil, ops_sudo_password: nil, inherit_existing_mappings: true)
44
44
  new_mapping = inherit_existing_mappings ? @input_mappings : {}
45
45
 
@@ -67,7 +67,7 @@ module OpsWalrus
67
67
 
68
68
  # adds the specified input mapping to the interaction handler
69
69
  #
70
- # mapping : Hash[ String | Regex => String ]
70
+ # mapping : Hash[ String | Regex => (String | Proc) ]
71
71
  def add_mapping(mapping)
72
72
  @input_mappings.merge!(mapping)
73
73
  end
@@ -84,13 +84,20 @@ module OpsWalrus
84
84
  # This method returns the data that is emitted to the response channel as a result of having processed the output
85
85
  # from a command that the interaction handler was expecting.
86
86
  def on_data(_command, stream_name, data, response_channel)
87
- response_data = begin
88
- first_matching_key_value_pair = @input_mappings.find {|k, _v| k === data }
89
- first_matching_key_value_pair&.last
87
+ response_data = @input_mappings.find_map do |pattern, mapped_output_value|
88
+ pattern = pattern.is_a?(String) ? Regexp.new(Regexp.escape(pattern)) : pattern
89
+ if pattern_match = data.match(pattern) # pattern_match : MatchData | Nil
90
+ case mapped_output_value
91
+ when Proc, Method
92
+ mapped_output_value.call(pattern_match)
93
+ when String
94
+ mapped_output_value
95
+ end
96
+ end
90
97
  end
91
98
 
92
99
  if response_data.nil?
93
- trace(Style.red("No interaction handler mapping for #{stream_name}: #{data} so no response was sent"))
100
+ trace(Style.red("No interaction handler mapping for #{stream_name}: `#{data}` so no response was sent"))
94
101
  else
95
102
  debug(Style.cyan("Handling #{stream_name} message #{data}"))
96
103
  debug(Style.cyan("Sending response #{response_data}"))
@@ -42,6 +42,17 @@ class Hash
42
42
  end
43
43
  end
44
44
 
45
+ module Enumerable
46
+ # calls the block with successive elements; returns the first truthy object returned by the block
47
+ def find_map(&block)
48
+ each do |element|
49
+ mapped_value = block.call(element)
50
+ return mapped_value if mapped_value
51
+ end
52
+ nil
53
+ end
54
+ end
55
+
45
56
  class Array
46
57
  def has_key?(key)
47
58
  key.is_a?(Integer) && key < size
@@ -1,3 +1,3 @@
1
1
  module OpsWalrus
2
- VERSION = "1.0.76"
2
+ VERSION = "1.0.77"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opswalrus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.76
4
+ version: 1.0.77
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Ellis