Module: Mixin::Shell

Includes:
Guess
Defined in:
lib/clearbooks/interface/thor/mixin/shell.rb

Instance Method Summary collapse

Methods included from Guess

#guess_version

Instance Method Details

#execute(command) ⇒ String

Returns result of command

Parameters:

  • command (String)

    Shell command string with arguments

Returns:

  • (String)

    Returns result of command

Raises:

  • (ArgumentError)


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

Parameters:

  • args (Array)

    Argument array



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.

Parameters:

  • command (String)

    The command to run

  • regexp (RegExp) (defaults to: nil)

    Optional regular expression used for matching output

Returns:

  • (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.message, :red
    result    = ''
  end

  return result
end

#version(command) ⇒ String

Returns version string or empty if command not found / error

Parameters:

  • command (String)

    Command string to probe version for, e.g. ruby

Returns:

  • (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
    banner    = run( command + " " + flag )

    # Guess way to extract and extract semver
    result    = guess_version( banner.to_s )

  rescue Exception => e
    say "(EE) " + e.message, :red
    result    = ""
  end

  return result
end

#which(command) ⇒ Boolean Also known as: exists?

Returns boolean true if command is available, false if not

Parameters:

  • command (String)

    Shell command string with or without arguments. If arguments are given, they are split on first whitespace and discarded

Returns:

  • (Boolean)

    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.message, :red
    result    = false
  end

  return result
end