hostlist_expression 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hostlist_expression.rb +35 -31
  3. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4444bb6780f552999628387b893e98e8df483562
4
- data.tar.gz: 97cf032750d191be707b748bce011ea29b7d57ba
3
+ metadata.gz: 9850d3b38f2db45ecab5bc18bf7493188deff215
4
+ data.tar.gz: 2bde9da4e2e26741a6de79ecf313df1b2daa4913
5
5
  SHA512:
6
- metadata.gz: 86108d43e27579172276c5c17e0c9b4922a33b4fc1bb0554325304fd50f55a4f80bd0bcfdfc76a8f9bde511d82b6f5db37ab9d65d934cd5556e2ebedaec60bea
7
- data.tar.gz: 784f9d4f7ddc2e14ae4c2d2f62b57a6d927e8fbb2f3df77182451dfc093cb8ca4d2fd1165bba3da5107537d66f05d6b07456832dde395941ff7d2b833291cac5
6
+ metadata.gz: 888de47ff1e69726e16d4ff6a950c28d3422d659ce28dd6dd4a8478ddd64a51a68388bf58e0e9600baf691db834547b30cc417bca96db23dc703f0f6609f6a33
7
+ data.tar.gz: 292d045103e248d93f868288a4ddae615a251dcf77f9222bc044b3fc360d828ff99093a3b3df0467438d0c4ce4b8bdc84ad01b2126bb1af1c4d97f0b5f7c6260
@@ -1,18 +1,20 @@
1
1
  # Author: Daniel Schroeder
2
2
  # License: MIT
3
3
  # Home: https://github.com/udondan/hostlist_expression-ruby
4
+
5
+ # Expand hostlist expression
4
6
  #
5
- # Public: Expand hostlist expression
7
+ # @param [ String ] expression A host list expression.
8
+ # @param [ String, Array<String> ] separator - Character(s) for separating ranges (default: [":", "-"]).
6
9
  #
7
- # expression - Hostlist expression.
8
- # separator - Character(s) for separating ranges (default: [":", "-"]).
9
- #
10
- # Examples
10
+ # @example
11
11
  #
12
12
  # hostlist_expression("your-host-[1-3].com")
13
13
  # # => ["your-host-1.com", "your-host-2.com", "your-host-3.com"]
14
14
  #
15
- # Returns the expanded host list as array.
15
+ # @return [ Array<String> ] A list of expanded hosts.
16
+ #
17
+ # @since 0.1.0
16
18
  def hostlist_expression(expression, separator = [":", "-"])
17
19
 
18
20
  # Validate range separator
@@ -20,11 +22,9 @@ def hostlist_expression(expression, separator = [":", "-"])
20
22
  separator = separator.join("")
21
23
  end
22
24
  if not separator.class == String
23
- puts "Error: Range separator must be either of type String or Array. given: #{separator.class}"
24
- exit 1
25
+ raise "Error: Range separator must be either of type String or Array. given: #{separator.class}"
25
26
  elsif separator.length == 0
26
- puts "Error: Range separator is empty"
27
- exit 1
27
+ raise "Error: Range separator is empty"
28
28
  end
29
29
 
30
30
  # Prepeare separator for use in regular expressions
@@ -54,37 +54,39 @@ def hostlist_expression(expression, separator = [":", "-"])
54
54
  range_items.push(range_items[0])
55
55
  end
56
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
+
57
66
  # Numeric range
58
- if range_items[0].match(/^[0-9]+$/) and range_items[1].match(/^[0-9]+$/)
67
+ if from.match(/^[0-9]+$/) and to.match(/^[0-9]+$/)
59
68
  isnum = true
60
- from = range_items[0].to_i
61
- to = range_items[1].to_i
69
+ from = from.to_i
70
+ to = to.to_i
62
71
 
63
72
  # Uppercase alphabetic range
64
- elsif range_items[0].length == 1 and range_items[1].length == 1 and /^[[:upper:]]+$/.match(range_items[0]) and /^[[:upper:]]+$/.match(range_items[1])
73
+ elsif from.length == 1 and to.length == 1 and /^[[:upper:]]+$/.match(from) and /^[[:upper:]]+$/.match(to)
65
74
  alphabet = ('A'..'Z').to_a
66
- from = alphabet.index(range_items[0])
67
- to = alphabet.index(range_items[1])
75
+ from = alphabet.index(from)
76
+ to = alphabet.index(to)
68
77
 
69
78
  # Lowercase alphabetic range
70
- elsif range_items[0].length == 1 and range_items[1].length == 1 and /^[[:lower:]]+$/.match(range_items[0]) and /^[[:lower:]]+$/.match(range_items[1])
79
+ elsif from.length == 1 and to.length == 1 and /^[[:lower:]]+$/.match(from) and /^[[:lower:]]+$/.match(to)
71
80
  alphabet = ('a'..'z').to_a
72
- from = alphabet.index(range_items[0])
73
- to = alphabet.index(range_items[1])
81
+ from = alphabet.index(from)
82
+ to = alphabet.index(to)
74
83
 
75
84
  else
76
- puts "Error: Invalid host range definition #{expression}"
77
- exit 1
78
- end
79
-
80
- # Fail if "to" is higher than "from"
81
- if from>to
82
- puts "Error: Invalid host range definition. 'to' part must be higher than 'from' part: #{expression}"
83
- exit 1
85
+ raise "Error: Invalid host range definition #{expression}"
84
86
  end
85
87
 
86
88
  # Iterate over all hosts and store the resolved patterns in "replacements"
87
- hosts.each do |exitem|
89
+ hosts.each do |host|
88
90
 
89
91
  # Iterate over the range
90
92
  (from..to).each do |i|
@@ -100,18 +102,20 @@ def hostlist_expression(expression, separator = [":", "-"])
100
102
  end
101
103
 
102
104
  # We clone the hosts array, because we can't modify it while iterating over its elements. So we iterate over the clone instead
103
- hosts.clone.each do |exitem|
105
+ hosts.clone.each do |host|
104
106
 
105
107
  # Remove the original element
106
- hosts.delete(exitem)
108
+ hosts.delete(host)
107
109
 
108
110
  # Iterate over previously stored replacements
109
111
  replacements.each do|replacement|
112
+
110
113
  # Adding replacement to hosts array
111
- hosts.push(exitem.sub(/\[#{match[0]}\]/, replacement))
114
+ hosts.push(host.sub(/\[#{match[0]}\]/, replacement))
112
115
  end
113
116
  end
114
117
  end
115
118
 
119
+ # Return uniqe results
116
120
  return hosts.uniq
117
121
  end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hostlist_expression
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Schroeder
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-23 00:00:00.000000000 Z
11
+ date: 2014-12-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: "Expand hostlist expression like those defined in pdsh or Ansible inventory
14
- files.\n \nAn expression like \"your-host-[1-3].com\" will expand into an array
15
- containing the elements:\n - your-host-1.com\n - your-host-2.com\n - your-host-3.com\n\nSee
16
- https://github.com/udondan/hostlist_expression-ruby for documentation\n"
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"
17
17
  email: daniel@phatthanan.com
18
18
  executables: []
19
19
  extensions: []