Class: RightConf::Command
- Inherits:
-
Object
- Object
- RightConf::Command
- Includes:
- ProgressReporter, Singleton
- Defined in:
- lib/rconf/command.rb
Constant Summary collapse
- RVM_COMMANDS =
Builtin RVM commands
[ 'gem', 'rubygems' ]
Instance Method Summary collapse
-
#execute(command, *args) ⇒ Object
Execute given command with given arguments.
-
#execute_in_ruby(command, *args) ⇒ Object
Execute command in context of activated ruby.
-
#execute_linux(command, *args) ⇒ Object
(also: #execute_darwin)
Execute given command on *nix systems.
-
#execute_windows(command, *args) ⇒ Object
Execute given command on Windows systems.
-
#set_ruby(ruby, gemset) ⇒ Object
Set ruby to be used by ‘execute_in_ruby’.
-
#set_verbose ⇒ Object
Enable debug output.
-
#sudo(*args) ⇒ Object
Execute given command with given arguments using ‘sudo’.
Methods included from ProgressReporter
included, report_to_file, report_to_stdout
Methods included from Singleton
Instance Method Details
#execute(command, *args) ⇒ Object
Execute given command with given arguments
Parameters
- command(String)
-
Command to run
- args(Array)
-
Command arguments
Return
- result(CommandResult)
-
Result of execution (output and exit status)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rconf/command.rb', line 32 def execute(command, *args) opts = {} if !args.empty? && args[-1].is_a?(Hash) opts = args[-1] args = args[0..-2] end res = Platform.dispatch(command, *args) { :execute } if @verbose msg = (([command] + args).compact.join(' ') + ' => ' + res.status.to_s + ': ' + res.output).grey report(msg) end if !res.success? && msg = opts[:abort_on_failure] report_fatal("#{msg}: '#{command} #{args.join(' ')}' returned\n#{res.output}") end res end |
#execute_in_ruby(command, *args) ⇒ Object
Execute command in context of activated ruby
Parameters
- command(String)
-
Command to run
- args(Array)
-
Command arguments
Return
- result(CommandResult)
-
Result of execution (output and exit status)
NOTE: This is the result returned by RVM, not the underlying command
and thus could be successful when the command actually failed...
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rconf/command.rb', line 60 def execute_in_ruby(command, *args) report_fatal('Failed to run in ruby context, no ruby specified') unless @rvm_prefix commands = if RVM_COMMANDS.include?(command) [ @rvm_prefix, command ] else [ @rvm_prefix, 'exec', '--', command ] end args = commands + args execute('rvm', *args) end |
#execute_linux(command, *args) ⇒ Object Also known as: execute_darwin
Execute given command on *nix systems
Parameters
- command(String)
-
Command name
- params(Array)
-
List of parameters to pass to command
Return
- result(CommandResult)
-
Result of execution (output and exit status)
92 93 94 95 |
# File 'lib/rconf/command.rb', line 92 def execute_linux(command, *args) out = `#{Shellwords.join([command, *args])} 2>&1` result = CommandResult.new(out, $?.exitstatus) end |
#execute_windows(command, *args) ⇒ Object
Execute given command on Windows systems
Parameters
- command(String)
-
Command name
- params(Array)
-
List of parameters to pass to command
Return
- result(CommandResult)
-
Result of execution (output and exit status)
106 107 108 |
# File 'lib/rconf/command.rb', line 106 def execute_windows(command, *args) raise 'TBD!' end |
#set_ruby(ruby, gemset) ⇒ Object
Set ruby to be used by ‘execute_in_ruby’
Parameters
- ruby(String)
-
RVM ruby
- gemset(String)
-
RVM gemset
Return
- true
-
Always return true
118 119 120 121 |
# File 'lib/rconf/command.rb', line 118 def set_ruby(ruby, gemset) @rvm_prefix = "#{ruby}@#{gemset}" true end |
#set_verbose ⇒ Object
Enable debug output
Return
- true
-
Always return true
127 128 129 |
# File 'lib/rconf/command.rb', line 127 def set_verbose @verbose = true end |
#sudo(*args) ⇒ Object
Execute given command with given arguments using ‘sudo’
Parameters
- command(String)
-
Command to run
- args(Array)
-
Command arguments
Return
- result(CommandResult)
-
Result of execution (output and exit status)
79 80 81 82 |
# File 'lib/rconf/command.rb', line 79 def sudo(*args) args = args.unshift('/usr/bin/sudo') Command.execute(*args) end |