Class: CliUtils
- Inherits:
-
Object
- Object
- CliUtils
- Defined in:
- lib/cli_utils.rb
Instance Attribute Summary collapse
-
#utility ⇒ Object
Returns the value of attribute utility.
-
#utility_path ⇒ Object
Returns the value of attribute utility_path.
Instance Method Summary collapse
- #find_utility ⇒ Object
- #get_version ⇒ Object
-
#initialize(utility) ⇒ CliUtils
constructor
A new instance of CliUtils.
- #supp_util_versions ⇒ Object
- #valid? ⇒ Boolean
- #version_pattern ⇒ Object
Constructor Details
#initialize(utility) ⇒ CliUtils
Returns a new instance of CliUtils.
6 7 8 9 10 11 |
# File 'lib/cli_utils.rb', line 6 def initialize(utility) @utility = utility @utility_path @version valid? end |
Instance Attribute Details
#utility ⇒ Object
Returns the value of attribute utility.
4 5 6 |
# File 'lib/cli_utils.rb', line 4 def utility @utility end |
#utility_path ⇒ Object
Returns the value of attribute utility_path.
4 5 6 |
# File 'lib/cli_utils.rb', line 4 def utility_path @utility_path end |
Instance Method Details
#find_utility ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/cli_utils.rb', line 19 def find_utility cmd = "which #{@utility}" ph = IO.popen(cmd) output = ph.readlines() ph.close if($? != 0) puts "Cannot find required utility #{@utility} in path." false else @utility_path = output[0].chomp end end |
#get_version ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cli_utils.rb', line 32 def get_version version = Array.new if(@utility_path == nil) find_utility end cmd = "#{@utility_path} --version" Open3.popen2e(cmd) { |i,oe,t| oe.each do |line| version.push(line) end } @version = version.to_s end |
#supp_util_versions ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cli_utils.rb', line 47 def supp_util_versions case @utility when "rsync" supp_version = 3 version_pattern.match(@version) if($1.to_s < supp_version.to_s) puts "Unsupported version of 'rsync', use version #{supp_version} or higher." false else true end end end |
#valid? ⇒ Boolean
13 14 15 16 17 |
# File 'lib/cli_utils.rb', line 13 def valid? find_utility get_version supp_util_versions end |
#version_pattern ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/cli_utils.rb', line 61 def version_pattern case @utility when "rsync" Regexp.new(/rsync version ([\d\.]+) protocol version (\d+)/) else puts "Not a supported utility: #{utility}" exit 1 end end |