Class: RightConf::Command

Inherits:
Object
  • Object
show all
Includes:
ProgressReporter, Singleton
Defined in:
lib/rconf/command.rb

Instance Method Summary collapse

Methods included from ProgressReporter

included, report_to_file, report_to_stdout

Methods included from Singleton

included

Instance Method Details

#execute(command, *args) ⇒ Object

Execute given command with given arguments

Parameters

command(String)

Command to run

args(Array)

Command arguments

Options

abort_on_failure(bool)

Whether to raise an exception in case command returns an error (false by default)

env

Hash of environment variables keyed by name

Return

result(CommandResult)

Result of execution (output and exit status)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rconf/command.rb', line 34

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, opts[:env], *args) { :execute }
  if @verbose
    msg = ([command] + args).compact.join(' ') + ' => ' +
      res.status.to_s + ': ' + res.output
    report("[rconf] " + 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

Options

abort_on_failure(bool)

Whether to raise an exception in case command returns an error (false by default)

env

Hash of environment variables keyed by name

Return

result(CommandResult)

Result of execution (output and exit status)



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rconf/command.rb', line 65

def execute_in_ruby(command, *args)
  report_fatal('Failed to run in ruby context, no ruby specified') unless @ruby_version
  opts = {}; env = {}
  if args[-1].is_a?(Hash)
    opts = args.pop
    env = opts[:env] || {}
  end
  env.merge!({ 'RBENV_VERSION' => @ruby_version })
  opts.merge!({ :env => env })
  args.push(opts)

  execute(command, *args)
end

#execute_linux(command, env, *args) ⇒ Object Also known as: execute_darwin

Execute given command on *nix systems

Parameters

command(String)

Command name

env(Hash)

Hash of environment variables keyed by name

params(Array)

List of parameters to pass to command

Return

result(CommandResult)

Result of execution (output and exit status)



101
102
103
104
105
106
107
108
# File 'lib/rconf/command.rb', line 101

def execute_linux(command, env, *args)
  env ||= {}
  old_envs = env.keys.inject({}) { |o, k| o[k] = ENV[k]; o }
  env.each { |k, v| ENV[k] = v.to_s }
  out = `#{Shellwords.join([command, *args])} 2>&1`
  old_envs.each { |k, v| ENV[k] = v }
  result = CommandResult.new(out, $?.exitstatus)
end

#execute_windows(command, env, *args) ⇒ Object

Execute given command on Windows systems

Parameters

command(String)

Command name

env(Hash)

Hash of environment variables keyed by name

params(Array)

List of parameters to pass to command

Return

result(CommandResult)

Result of execution (output and exit status)



120
121
122
# File 'lib/rconf/command.rb', line 120

def execute_windows(command, env, *args)
  raise 'TBD!'
end

#set_ruby(version) ⇒ Object

Set ruby to be used by ‘execute_in_ruby’

Parameters

version(String)

rbenv ruby version

Return

true

Always return true



131
132
133
134
135
# File 'lib/rconf/command.rb', line 131

def set_ruby(version)
  @ruby_version = version
  File.open('.ruby-version', 'w') { |f| f.puts version }
  true
end

#set_verboseObject

Enable debug output

Return

true

Always return true



141
142
143
# File 'lib/rconf/command.rb', line 141

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)



87
88
89
90
# File 'lib/rconf/command.rb', line 87

def sudo(*args)
  args = args.unshift('/usr/bin/sudo')
  Command.execute(*args)
end