Module: Mixin::Shell
- Includes:
- Guess
- Defined in:
- lib/clearbooks/interface/thor/mixin/shell.rb
Instance Method Summary collapse
-
#execute(command) ⇒ String
Returns result of command.
- #initialize(*args) ⇒ Object
-
#run(command, regexp = nil) ⇒ String
Returns empty string if command not found or other problem, otherwise result of command.
-
#version(command) ⇒ String
Returns version string or empty if command not found / error.
-
#which(command) ⇒ Boolean
(also: #exists?)
Returns boolean true if command is available, false if not.
Methods included from Guess
Instance Method Details
#execute(command) ⇒ String
Returns result of command
70 71 72 73 74 75 76 77 |
# File 'lib/clearbooks/interface/thor/mixin/shell.rb', line 70 def execute command exists = which( command ) raise ArgumentError, "Command not found" unless( exists ) result = `#{command}` return result end |
#initialize(*args) ⇒ Object
27 28 29 |
# File 'lib/clearbooks/interface/thor/mixin/shell.rb', line 27 def initialize *args super end |
#run(command, regexp = nil) ⇒ String
Returns empty string if command not found or other problem, otherwise result of command.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/clearbooks/interface/thor/mixin/shell.rb', line 41 def run command, regexp = nil result = '' begin if( regexp.nil? ) result = execute( command ).strip else raise ArgumentError, 'Regular Expression input needs to be of type Regexp' unless( regexp.is_a?( Regexp ) ) result = execute( command ).andand.send( :match, regexp ).andand.send( :to_s ).strip end rescue Exception => e say '(EE) ' + e., :red result = '' end return result end |
#version(command) ⇒ String
Returns version string or empty if command not found / error
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/clearbooks/interface/thor/mixin/shell.rb', line 113 def version command result = "" begin # Sanity raise ArgumentError, "Command not found" unless( which( command ) ) # Get usage help screen for command help = usage( command ) # Get version flag from help screen flag = version_flag( help ) return result if( flag.empty? ) # some stupid commands don't follow standard rules, e.g. bundle # Get actual version string = run( command + " " + flag ) # Guess way to extract and extract semver result = guess_version( .to_s ) rescue Exception => e say "(EE) " + e., :red result = "" end return result end |
#which(command) ⇒ Boolean Also known as: exists?
Returns boolean true if command is available, false if not
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/clearbooks/interface/thor/mixin/shell.rb', line 90 def which command result = false begin partial = command.to_s partial = partial.split(' ').first.to_s if( partial =~ %r{ }i ) path = File.which( partial ) result = true unless( path.nil? ) rescue Exception => e say "(EE) " + e., :red result = false end return result end |