Class: Balmora::Shell
- Inherits:
-
Object
show all
- Defined in:
- lib/balmora/shell.rb
Defined Under Namespace
Classes: Error, Expression
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(logger, dry, home = nil) ⇒ Shell
Returns a new instance of Shell.
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/balmora/shell.rb', line 33
def initialize(logger, dry, home = nil)
@logger = logger
@dry = dry
@run = ::Object.method(:'`')
@system = ::Object.method(:system)
@status = Proc.new() { $?.exitstatus }
@home = Dir.home()
@user_id ||= `id -un; id -gn`.strip().split("\n").join(':')
@sudo_stack = []
end
|
Instance Attribute Details
#exec ⇒ Object
Returns the value of attribute exec.
26
27
28
|
# File 'lib/balmora/shell.rb', line 26
def exec
@exec
end
|
#home ⇒ Object
Returns the value of attribute home.
27
28
29
|
# File 'lib/balmora/shell.rb', line 27
def home
@home
end
|
#status ⇒ Object
Returns the value of attribute status.
26
27
28
|
# File 'lib/balmora/shell.rb', line 26
def status
@status
end
|
#user_id ⇒ Object
Returns the value of attribute user_id.
27
28
29
|
# File 'lib/balmora/shell.rb', line 27
def user_id
@user_id
end
|
Class Method Details
.factory(state) ⇒ Object
29
30
31
|
# File 'lib/balmora/shell.rb', line 29
def self.factory(state)
return self.new(state.logger, state.options[:dry])
end
|
Instance Method Details
#expand(path) ⇒ Object
115
116
117
118
119
120
121
|
# File 'lib/balmora/shell.rb', line 115
def expand(path)
if path.start_with?('~') || path.start_with?('/')
return ::File.expand_path(path)
end
return path
end
|
#expression(command) ⇒ Object
45
46
47
|
# File 'lib/balmora/shell.rb', line 45
def expression(command)
Expression.new(command)
end
|
#run(command, options = {}) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/balmora/shell.rb', line 61
def run(command, options = {})
command = sudo() + command
shell_command =
command.
collect() { |part|
if part.instance_of?(Expression)
next part.command
end
next Shellwords.escape(part)
}.
join(' ')
dry = false
if options[:change] == true && @dry
dry = true
shell_command = "dry: #{shell_command}"
end
if options[:verbose] != false
@logger.info(shell_command)
else
@logger.debug(shell_command)
end
if dry
return 0, ''
end
method = @run
if options[:system] == true
method = @system
end
result = method.call(shell_command)
status = @status.call()
if options[:raise] == true && status != 0
raise Error.new("Failed to execute command: #{shell_command}",
status)
end
return status, result
end
|
#run!(command, options = {}) ⇒ Object
107
108
109
|
# File 'lib/balmora/shell.rb', line 107
def run!(command, options = {})
return run(command, options.merge(raise: true))[1]
end
|
#sudo ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/balmora/shell.rb', line 49
def sudo()
if @sudo_stack.length == 0
return []
end
if @sudo_stack[-1] == true
return ['sudo']
else
return ['sudo', '--user', @sudo_stack[-1]]
end
end
|
#sudo!(sudo) ⇒ Object
123
124
125
126
127
128
|
# File 'lib/balmora/shell.rb', line 123
def sudo!(sudo)
@sudo_stack.push(sudo)
yield
ensure
@sudo_stack.pop()
end
|
#system(command, options = {}) ⇒ Object
111
112
113
|
# File 'lib/balmora/shell.rb', line 111
def system(command, options = {})
return run(command, options.merge(raise: true, system: true))[1]
end
|