hostlist_expression 0.2.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/hostlist_expression.rb +47 -64
  3. metadata +15 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 9850d3b38f2db45ecab5bc18bf7493188deff215
4
- data.tar.gz: 2bde9da4e2e26741a6de79ecf313df1b2daa4913
2
+ SHA256:
3
+ metadata.gz: d8b842060fa14e03dcd4a646d4305df1288c5b77891b3a7917192d0ed1ebea3b
4
+ data.tar.gz: 27f65117b4e7307ebf734c97caac3872d85b1b7ada0c1c07c697e21e687f4749
5
5
  SHA512:
6
- metadata.gz: 888de47ff1e69726e16d4ff6a950c28d3422d659ce28dd6dd4a8478ddd64a51a68388bf58e0e9600baf691db834547b30cc417bca96db23dc703f0f6609f6a33
7
- data.tar.gz: 292d045103e248d93f868288a4ddae615a251dcf77f9222bc044b3fc360d828ff99093a3b3df0467438d0c4ce4b8bdc84ad01b2126bb1af1c4d97f0b5f7c6260
6
+ metadata.gz: 8bfa2df39099d32a1ca7c126aff098fef0ca3f67ec8baac61c046638db353e9f8377d90329dc5d76a23498d2d147b5e13056a0ae4dcd35c60504ce1be3bfb628
7
+ data.tar.gz: 6646f9aa7bb1decd99e9deb296f5e4b194ec672d6eda80501770b4725651c445a2f2e8d4a6bd6baa6cfd2847078c9d98a78516b8a3d87e9e5e04440fdfdfbe44
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Author: Daniel Schroeder
2
4
  # License: MIT
3
5
  # Home: https://github.com/udondan/hostlist_expression-ruby
@@ -5,10 +7,9 @@
5
7
  # Expand hostlist expression
6
8
  #
7
9
  # @param [ String ] expression A host list expression.
8
- # @param [ String, Array<String> ] separator - Character(s) for separating ranges (default: [":", "-"]).
10
+ # @param [ String, Array<String> ] separator - Character(s) for separating ranges.
9
11
  #
10
12
  # @example
11
- #
12
13
  # hostlist_expression("your-host-[1-3].com")
13
14
  # # => ["your-host-1.com", "your-host-2.com", "your-host-3.com"]
14
15
  #
@@ -16,83 +17,66 @@
16
17
  #
17
18
  # @since 0.1.0
18
19
  def hostlist_expression(expression, separator = [":", "-"])
19
-
20
20
  # Validate range separator
21
- if separator.class == Array
22
- separator = separator.join("")
23
- end
24
- if not separator.class == String
21
+ separator = separator.join if separator.is_a?(Array)
22
+ if !separator.is_a?(String)
25
23
  raise "Error: Range separator must be either of type String or Array. given: #{separator.class}"
26
- elsif separator.length == 0
24
+ elsif separator.empty?
27
25
  raise "Error: Range separator is empty"
28
26
  end
29
-
30
- # Prepeare separator for use in regular expressions
27
+
28
+ # Prepare separator for use in regular expressions
31
29
  separator = Regexp.escape(separator)
32
-
33
- # Return input, if this is not a hostlist expression
34
- return expression if not expression.match(/\[(?:[\da-z]+(?:[#{separator}][\da-z]+)?,?)+\]/i)
35
-
36
- # hosts array will hold all expanded results, it as well is the working array where partially resolved results are stored
37
- hosts = Array.new
38
- hosts.push(expression)
39
-
30
+
31
+ # Return input as single host, if this is not a hostlist expression
32
+ return [expression] unless expression.match(/\[(?:[\da-z]+(?:[#{separator}][\da-z]+)?,?)+\]/i)
33
+
34
+ # hosts array will hold all expanded results,
35
+ # it as well is the working array where partially resolved results are stored
36
+ hosts = [expression]
37
+
40
38
  # Iterate over all range definitions, e.g. [0:10] or [A:Z]
41
- expression.scan(/\[([\da-z#{separator},]+)\]/i).each do|match|
42
-
39
+ expression.scan(/\[([\da-z#{separator},]+)\]/i).each do |match|
43
40
  # Will hold replacements for each match
44
- replacements = Array.new
45
-
41
+ replacements = []
42
+
46
43
  # The pattern may be a sequence with multiple ranges. Split...
47
44
  match[0].split(",").each do |range|
48
-
49
45
  # Split ranges by range separator
50
46
  range_items = range.split(/[#{separator}]/)
51
-
47
+
52
48
  # If it's not really a range, duplicate the single item
53
- if range_items.length == 1
54
- range_items.push(range_items[0])
55
- end
56
-
57
- # Get lower and higher value of range
58
- if range_items[0].to_i < range_items[1].to_i
59
- from = range_items[0]
60
- to = range_items[1]
61
- else
62
- from = range_items[1]
63
- to = range_items[0]
64
- end
65
-
49
+ range_items.push(range_items[0]) if range_items.length == 1
50
+
66
51
  # Numeric range
67
- if from.match(/^[0-9]+$/) and to.match(/^[0-9]+$/)
52
+ if range_items.all? { |item| item.match(/^[0-9]+$/) }
68
53
  isnum = true
69
- from = from.to_i
70
- to = to.to_i
71
-
54
+ from, to = range_items.map(&:to_i).sort
55
+
72
56
  # Uppercase alphabetic range
73
- elsif from.length == 1 and to.length == 1 and /^[[:upper:]]+$/.match(from) and /^[[:upper:]]+$/.match(to)
74
- alphabet = ('A'..'Z').to_a
75
- from = alphabet.index(from)
76
- to = alphabet.index(to)
77
-
57
+ elsif range_items.all? { |item| item.match(/^[A-Z]$/) }
58
+ alphabet = ("A".."Z").to_a
59
+ from, to = range_items.map { |item| alphabet.index(item) }.sort
60
+
78
61
  # Lowercase alphabetic range
79
- elsif from.length == 1 and to.length == 1 and /^[[:lower:]]+$/.match(from) and /^[[:lower:]]+$/.match(to)
80
- alphabet = ('a'..'z').to_a
81
- from = alphabet.index(from)
82
- to = alphabet.index(to)
83
-
62
+ elsif range_items.all? { |item| item.match(/^[a-z]$/) }
63
+ alphabet = ("a".."z").to_a
64
+ from, to = range_items.map { |item| alphabet.index(item) }.sort
65
+
84
66
  else
85
67
  raise "Error: Invalid host range definition #{expression}"
86
68
  end
87
-
69
+
70
+ # Pad numbers only if a bound has leading zeros, to the width of the longest bound
71
+ padding = range_items.any? { |item| item.match(/^0[0-9]/) } ? range_items.map(&:length).max : 0
72
+
88
73
  # Iterate over all hosts and store the resolved patterns in "replacements"
89
- hosts.each do |host|
90
-
74
+ hosts.each do |_host|
91
75
  # Iterate over the range
92
76
  (from..to).each do |i|
93
77
  if isnum
94
78
  # Formatting number with leading zeros
95
- replacements.push("#{i}".rjust(range_items[0].length, "0"))
79
+ replacements.push(i.to_s.rjust(padding, "0"))
96
80
  else
97
81
  # Select correct letter from alphabet
98
82
  replacements.push(alphabet[i])
@@ -100,22 +84,21 @@ def hostlist_expression(expression, separator = [":", "-"])
100
84
  end
101
85
  end
102
86
  end
103
-
104
- # We clone the hosts array, because we can't modify it while iterating over its elements. So we iterate over the clone instead
87
+
88
+ # We clone the hosts array, because we can't modify it while iterating over its elements.
89
+ # So we iterate over the clone instead
105
90
  hosts.clone.each do |host|
106
-
107
91
  # Remove the original element
108
92
  hosts.delete(host)
109
-
93
+
110
94
  # Iterate over previously stored replacements
111
- replacements.each do|replacement|
112
-
95
+ replacements.each do |replacement|
113
96
  # Adding replacement to hosts array
114
97
  hosts.push(host.sub(/\[#{match[0]}\]/, replacement))
115
98
  end
116
99
  end
117
100
  end
118
-
119
- # Return uniqe results
120
- return hosts.uniq
101
+
102
+ # Return unique results
103
+ hosts.uniq
121
104
  end
metadata CHANGED
@@ -1,19 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hostlist_expression
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schroeder
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2014-12-24 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
- description: "Expand hostlist expression like those defined in pdsh or Ansible inventory
14
- files.\n \nAn expression like \"host-[1-3].com\" will expand into an array containing
15
- the elements host-1.com, host-2.com and host-3.com\n\nSee https://github.com/udondan/hostlist_expression-ruby
16
- for documentation\n"
12
+ description: |
13
+ Expand hostlist expression like those defined in pdsh or Ansible inventory files.
14
+
15
+ An expression like "host-[1-3].com" will expand into an array containing the elements host-1.com, host-2.com and host-3.com
16
+
17
+ See https://github.com/udondan/hostlist_expression-ruby for documentation
17
18
  email: daniel@phatthanan.com
18
19
  executables: []
19
20
  extensions: []
@@ -23,25 +24,24 @@ files:
23
24
  homepage: https://github.com/udondan/hostlist_expression-ruby
24
25
  licenses:
25
26
  - MIT
26
- metadata: {}
27
- post_install_message:
27
+ metadata:
28
+ source_code_uri: https://github.com/udondan/hostlist_expression-ruby
29
+ rubygems_mfa_required: 'true'
28
30
  rdoc_options: []
29
31
  require_paths:
30
32
  - lib
31
33
  required_ruby_version: !ruby/object:Gem::Requirement
32
34
  requirements:
33
- - - '>='
35
+ - - ">="
34
36
  - !ruby/object:Gem::Version
35
- version: '0'
37
+ version: '3.3'
36
38
  required_rubygems_version: !ruby/object:Gem::Requirement
37
39
  requirements:
38
- - - '>='
40
+ - - ">="
39
41
  - !ruby/object:Gem::Version
40
42
  version: '0'
41
43
  requirements: []
42
- rubyforge_project:
43
- rubygems_version: 2.0.14
44
- signing_key:
44
+ rubygems_version: 4.0.20
45
45
  specification_version: 4
46
46
  summary: Expand hostlist expression
47
47
  test_files: []