Class: Harrison::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/harrison/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(arg_opts = [], opts = {}) ⇒ Base
Returns a new instance of Base.
6
7
8
9
10
11
12
13
14
|
# File 'lib/harrison/base.rb', line 6
def initialize(arg_opts=[], opts={})
self.class.option_helper(:user)
@arg_opts = arg_opts
@arg_opts << [ :debug, "Output debug messages.", :type => :boolean, :default => false ]
@options = opts
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Find config from Harrison.config if it’s not on this class.
27
28
29
30
31
32
33
|
# File 'lib/harrison/base.rb', line 27
def method_missing(meth, *args, &block)
if Harrison.config.respond_to?(meth)
Harrison.config.send(meth, *args, &block)
else
super
end
end
|
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
4
5
6
|
# File 'lib/harrison/base.rb', line 4
def options
@options
end
|
Class Method Details
.option_helper(option) ⇒ Object
16
17
18
19
20
21
22
23
24
|
# File 'lib/harrison/base.rb', line 16
def self.option_helper(option)
send :define_method, option do
@options[option]
end
send :define_method, "#{option}=" do |val|
@options[option] = val
end
end
|
Instance Method Details
#close ⇒ Object
88
89
90
|
# File 'lib/harrison/base.rb', line 88
def close
ssh.close if @ssh
end
|
#download(remote_path, local_path) ⇒ Object
80
81
82
|
# File 'lib/harrison/base.rb', line 80
def download(remote_path, local_path)
ssh.download(remote_path, local_path)
end
|
#exec(cmd) ⇒ Object
35
36
37
38
39
40
41
42
43
|
# File 'lib/harrison/base.rb', line 35
def exec(cmd)
result = `#{cmd}`
if ($?.success? && result)
result.strip
else
throw :failure, true
end
end
|
#parse(args) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/harrison/base.rb', line 55
def parse(args)
opt_parser = Trollop::Parser.new
@arg_opts.each do |arg_opt|
opt_parser.opt(*arg_opt)
end
@options.merge!(Trollop::with_standard_exception_handling(opt_parser) do
opt_parser.parse(args)
end)
Harrison.send(:remove_const, "DEBUG") if Harrison.const_defined?("DEBUG")
Harrison.const_set("DEBUG", @options[:debug])
end
|
#remote_exec(cmd) ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'lib/harrison/base.rb', line 45
def remote_exec(cmd)
result = ssh.exec(cmd)
if result
result.strip
else
throw :failure, true
end
end
|
#run(&block) ⇒ Object
70
71
72
73
74
75
76
77
78
|
# File 'lib/harrison/base.rb', line 70
def run(&block)
if block_given?
@run_block = block
else
@run_block && @run_block.call(self)
end
end
|
#upload(local_path, remote_path) ⇒ Object
84
85
86
|
# File 'lib/harrison/base.rb', line 84
def upload(local_path, remote_path)
ssh.upload(local_path, remote_path)
end
|