hostlist_expression 0.2.1 → 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 +46 -62
  3. metadata +15 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 80ed12d4cf8f0d716de3c766de55a1cb052da4ce
4
- data.tar.gz: ba61a29b097eae918d2623207d5e6824b7227211
2
+ SHA256:
3
+ metadata.gz: d8b842060fa14e03dcd4a646d4305df1288c5b77891b3a7917192d0ed1ebea3b
4
+ data.tar.gz: 27f65117b4e7307ebf734c97caac3872d85b1b7ada0c1c07c697e21e687f4749
5
5
  SHA512:
6
- metadata.gz: f92ce87db337993ab0dc0cc298bc9da0e2cb43dd2f44381c3d4890a4aac77e45756f953f5d77c713993acf983629f5c86de1db0e073bb7b21889f0e936e32db6
7
- data.tar.gz: 64c2e257fc51eaa15d4095b9e73c604fc260f90339d6676e9122751d666b37de932c3a93a1997454e1c52130ca98b3f01b8b94ece8a93010822b32eaa7fd030a
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
@@ -15,83 +17,66 @@
15
17
  #
16
18
  # @since 0.1.0
17
19
  def hostlist_expression(expression, separator = [":", "-"])
18
-
19
20
  # Validate range separator
20
- if separator.class == Array
21
- separator = separator.join("")
22
- end
23
- if not separator.class == String
21
+ separator = separator.join if separator.is_a?(Array)
22
+ if !separator.is_a?(String)
24
23
  raise "Error: Range separator must be either of type String or Array. given: #{separator.class}"
25
- elsif separator.length == 0
24
+ elsif separator.empty?
26
25
  raise "Error: Range separator is empty"
27
26
  end
28
-
29
- # Prepeare separator for use in regular expressions
27
+
28
+ # Prepare separator for use in regular expressions
30
29
  separator = Regexp.escape(separator)
31
-
32
- # Return input, if this is not a hostlist expression
33
- return expression if not expression.match(/\[(?:[\da-z]+(?:[#{separator}][\da-z]+)?,?)+\]/i)
34
-
35
- # hosts array will hold all expanded results, it as well is the working array where partially resolved results are stored
36
- hosts = Array.new
37
- hosts.push(expression)
38
-
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
+
39
38
  # Iterate over all range definitions, e.g. [0:10] or [A:Z]
40
- expression.scan(/\[([\da-z#{separator},]+)\]/i).each do|match|
41
-
39
+ expression.scan(/\[([\da-z#{separator},]+)\]/i).each do |match|
42
40
  # Will hold replacements for each match
43
- replacements = Array.new
44
-
41
+ replacements = []
42
+
45
43
  # The pattern may be a sequence with multiple ranges. Split...
46
44
  match[0].split(",").each do |range|
47
-
48
45
  # Split ranges by range separator
49
46
  range_items = range.split(/[#{separator}]/)
50
-
47
+
51
48
  # If it's not really a range, duplicate the single item
52
- if range_items.length == 1
53
- range_items.push(range_items[0])
54
- end
55
-
56
- # Get lower and higher value of range
57
- if range_items[0] < range_items[1]
58
- from = range_items[0]
59
- to = range_items[1]
60
- else
61
- from = range_items[1]
62
- to = range_items[0]
63
- end
64
-
49
+ range_items.push(range_items[0]) if range_items.length == 1
50
+
65
51
  # Numeric range
66
- if from.match(/^[0-9]+$/) and to.match(/^[0-9]+$/)
52
+ if range_items.all? { |item| item.match(/^[0-9]+$/) }
67
53
  isnum = true
68
- from = from.to_i
69
- to = to.to_i
70
-
54
+ from, to = range_items.map(&:to_i).sort
55
+
71
56
  # Uppercase alphabetic range
72
- elsif from.length == 1 and to.length == 1 and /^[[:upper:]]+$/.match(from) and /^[[:upper:]]+$/.match(to)
73
- alphabet = ('A'..'Z').to_a
74
- from = alphabet.index(from)
75
- to = alphabet.index(to)
76
-
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
+
77
61
  # Lowercase alphabetic range
78
- elsif from.length == 1 and to.length == 1 and /^[[:lower:]]+$/.match(from) and /^[[:lower:]]+$/.match(to)
79
- alphabet = ('a'..'z').to_a
80
- from = alphabet.index(from)
81
- to = alphabet.index(to)
82
-
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
+
83
66
  else
84
67
  raise "Error: Invalid host range definition #{expression}"
85
68
  end
86
-
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
+
87
73
  # Iterate over all hosts and store the resolved patterns in "replacements"
88
- hosts.each do |host|
89
-
74
+ hosts.each do |_host|
90
75
  # Iterate over the range
91
76
  (from..to).each do |i|
92
77
  if isnum
93
78
  # Formatting number with leading zeros
94
- replacements.push("#{i}".rjust(range_items[0].length, "0"))
79
+ replacements.push(i.to_s.rjust(padding, "0"))
95
80
  else
96
81
  # Select correct letter from alphabet
97
82
  replacements.push(alphabet[i])
@@ -99,22 +84,21 @@ def hostlist_expression(expression, separator = [":", "-"])
99
84
  end
100
85
  end
101
86
  end
102
-
103
- # 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
104
90
  hosts.clone.each do |host|
105
-
106
91
  # Remove the original element
107
92
  hosts.delete(host)
108
-
93
+
109
94
  # Iterate over previously stored replacements
110
- replacements.each do|replacement|
111
-
95
+ replacements.each do |replacement|
112
96
  # Adding replacement to hosts array
113
97
  hosts.push(host.sub(/\[#{match[0]}\]/, replacement))
114
98
  end
115
99
  end
116
100
  end
117
-
118
- # Return uniqe results
119
- return hosts.uniq
101
+
102
+ # Return unique results
103
+ hosts.uniq
120
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.1
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: []