hostlist_expression 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/lib/hostlist_expression.rb +35 -31
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9850d3b38f2db45ecab5bc18bf7493188deff215
|
4
|
+
data.tar.gz: 2bde9da4e2e26741a6de79ecf313df1b2daa4913
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 888de47ff1e69726e16d4ff6a950c28d3422d659ce28dd6dd4a8478ddd64a51a68388bf58e0e9600baf691db834547b30cc417bca96db23dc703f0f6609f6a33
|
7
|
+
data.tar.gz: 292d045103e248d93f868288a4ddae615a251dcf77f9222bc044b3fc360d828ff99093a3b3df0467438d0c4ce4b8bdc84ad01b2126bb1af1c4d97f0b5f7c6260
|
data/lib/hostlist_expression.rb
CHANGED
@@ -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
|
-
#
|
7
|
+
# @param [ String ] expression A host list expression.
|
8
|
+
# @param [ String, Array<String> ] separator - Character(s) for separating ranges (default: [":", "-"]).
|
6
9
|
#
|
7
|
-
#
|
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
|
-
#
|
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
|
-
|
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
|
-
|
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
|
67
|
+
if from.match(/^[0-9]+$/) and to.match(/^[0-9]+$/)
|
59
68
|
isnum = true
|
60
|
-
from =
|
61
|
-
to =
|
69
|
+
from = from.to_i
|
70
|
+
to = to.to_i
|
62
71
|
|
63
72
|
# Uppercase alphabetic range
|
64
|
-
elsif
|
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(
|
67
|
-
to = alphabet.index(
|
75
|
+
from = alphabet.index(from)
|
76
|
+
to = alphabet.index(to)
|
68
77
|
|
69
78
|
# Lowercase alphabetic range
|
70
|
-
elsif
|
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(
|
73
|
-
to = alphabet.index(
|
81
|
+
from = alphabet.index(from)
|
82
|
+
to = alphabet.index(to)
|
74
83
|
|
75
84
|
else
|
76
|
-
|
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 |
|
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 |
|
105
|
+
hosts.clone.each do |host|
|
104
106
|
|
105
107
|
# Remove the original element
|
106
|
-
hosts.delete(
|
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(
|
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.
|
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-
|
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 \"
|
15
|
-
|
16
|
-
|
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: []
|