Class: Superhosting::Cli::Base
Direct Known Subclasses
Cmd::AdminAdd, Cmd::AdminContainerAdd, Cmd::AdminContainerDelete, Cmd::AdminContainerList, Cmd::AdminDelete, Cmd::AdminList, Cmd::AdminPasswd, Cmd::ContainerAdd, Cmd::ContainerAdminAdd, Cmd::ContainerAdminDelete, Cmd::ContainerAdminList, Cmd::ContainerChange, Cmd::ContainerDelete, Cmd::ContainerInspect, Cmd::ContainerList, Cmd::ContainerReconfigure, Cmd::ContainerRename, Cmd::ContainerRestore, Cmd::ContainerSave, Cmd::ContainerUpdate, Cmd::ModelList, Cmd::ModelReconfigure, Cmd::ModelTree, Cmd::ModelUpdate, Cmd::MuxReconfigure, Cmd::MysqlDbAdd, Cmd::MysqlDbDelete, Cmd::MysqlDbDump, Cmd::MysqlDbSql, Cmd::MysqlGrant, Cmd::MysqlUserAdd, Cmd::MysqlUserDelete, Cmd::Repair, Cmd::SiteAdd, Cmd::SiteAliasAdd, Cmd::SiteAliasDelete, Cmd::SiteContainer, Cmd::SiteDelete, Cmd::SiteInspect, Cmd::SiteList, Cmd::SiteName, Cmd::SiteReconfigure, Cmd::SiteRename, Cmd::UserAdd, Cmd::UserChange, Cmd::UserDelete, Cmd::UserList, Cmd::UserPasswd
Constant Summary
collapse
- COMMANDS_MODULE =
Cmd
- CONTROLLERS_MODULE =
Superhosting::Controller
- CONTROLLER_BASE_OPTIONS =
[:dry_run, :debug]
Class Method Summary
collapse
Instance Method Summary
collapse
i18n_initialize
__debug, __dry_run, __dry_run=, __logger, __logger=, debug, debug_block, debug_operation, indent, indent=, indent_reset, indent_step, indent_step_back, info, storage, t, with_dry_run, with_indent, with_logger
Constructor Details
#initialize(argv, node) ⇒ Base
Returns a new instance of Base.
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/superhosting/cli/base.rb', line 36
def initialize(argv, node)
self.class.options.merge!(Base::options)
super()
begin
@pos_args = parse_options(argv)
rescue OptionParser::InvalidOption => e
raise NetStatus::Exception, error: :input_error, code: :invalid_cli_option, data: { message: e.message }
end
@node = node
@node_class = node.values.first
@logger = Logger.new(STDOUT)
@logger.level = (config[:debug] or config[:dry_run] or config[:verbose]) ? Logger::DEBUG : Logger::INFO
@logger.formatter = proc {|severity, datetime, progname, msg| sprintf("%s\n", msg.to_s) }
self.__logger = @logger
self.help if config[:help] or self.class == Base
end
|
Class Method Details
.get_cmd_and_node(args) ⇒ Object
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/superhosting/cli/base.rb', line 208
def get_cmd_and_node(args)
def positional_arguments(args)
args.select { |arg| arg =~ /^([[:alnum:]\_\-]+)$/ }
end
args = positional_arguments(args)
node = @@commands_hierarchy
path = []
key = ''
cmd = nil
while arg = args.shift and cmd.nil?
res = node.keys.select { |k| k.start_with? arg }
case res.count
when 1
key = res.first
cmd = node[key] if node[key].is_a? Class
when 0
break
else
raise Error::AmbiguousCommand.new(path: path, commands: res)
end
path << key
node = node[key]
end
cmd ||= self
node = { key => node }
[cmd, node]
end
|
.get_splited_class_name ⇒ Object
200
201
202
|
# File 'lib/superhosting/cli/base.rb', line 200
def get_splited_class_name
self.split_toggle_case_name(self.name.split('::').last)
end
|
.has_required_param? ⇒ Boolean
181
182
183
|
# File 'lib/superhosting/cli/base.rb', line 181
def has_required_param?
false
end
|
.prepend ⇒ Object
163
164
165
166
167
|
# File 'lib/superhosting/cli/base.rb', line 163
def prepend
set_commands_hierarchy
set_banners
i18n_initialize
end
|
.set_banners(node = @@commands_hierarchy, path = []) ⇒ Object
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/superhosting/cli/base.rb', line 169
def set_banners(node=@@commands_hierarchy, path=[])
node.each do |k,v|
path_ = path.dup
path_ << k
if v.is_a? Hash
set_banners(v, path_)
else
v.banner("sx #{path_.join(' ')}#{" <#{path.last}>" if v.has_required_param?}#{' (options)' unless v.options.empty?}")
end
end
end
|
.set_commands_hierarchy ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/superhosting/cli/base.rb', line 185
def set_commands_hierarchy
def get_commands
COMMANDS_MODULE.constants.select {|c| Class === COMMANDS_MODULE.const_get(c) }.sort
end
@@commands_hierarchy = get_commands.sort_by {|k1, k2| split_toggle_case_name(k1).one? ? 0 : 1 }.inject({}) do |h,k|
node = h
parts = split_toggle_case_name(k)
parts.each do |cmd|
node = (node[cmd] ||= (cmd == parts.last) ? COMMANDS_MODULE.const_get(k) : {})
end
h
end
end
|
.split_toggle_case_name(klass) ⇒ Object
204
205
206
|
# File 'lib/superhosting/cli/base.rb', line 204
def split_toggle_case_name(klass)
klass.to_s.gsub(/([[:lower:]])([[:upper:]])/, '\1 \2').split(' ').map(&:downcase)
end
|
.start(args) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/superhosting/cli/base.rb', line 151
def start(args)
def clear_args(args, cmd)
split_toggle_case_name(cmd).length.times{ args.shift }
args
end
self.prepend
cmd, node = get_cmd_and_node(args)
args = clear_args(args, cmd)
cmd.new(args, node).run
end
|
Instance Method Details
#action ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/superhosting/cli/base.rb', line 93
def action
method = get_controller
opts = {}
method.parameters.each do |req, name|
if req.to_s.start_with? 'key'
opt = config[name]
self.help unless (opt = @pos_args.shift) if name == :name
opts.merge!(name => opt)
end
end
self.help unless @pos_args.empty?
method.parameters.empty? ? method.call : method.call(**opts)
end
|
#clear_args(args, cmd) ⇒ Object
152
153
154
155
|
# File 'lib/superhosting/cli/base.rb', line 152
def clear_args(args, cmd)
split_toggle_case_name(cmd).length.times{ args.shift }
args
end
|
#get_childs_banners(node) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/superhosting/cli/base.rb', line 58
def get_childs_banners(node)
if node.is_a? Hash
node.map do |k,v|
if v.is_a? Hash
get_childs_banners(node[k])
else
v.banner
end
end.join("\n")
else
node.banner
end
end
|
#get_commands ⇒ Object
186
187
188
|
# File 'lib/superhosting/cli/base.rb', line 186
def get_commands
COMMANDS_MODULE.constants.select {|c| Class === COMMANDS_MODULE.const_get(c) }.sort
end
|
#get_controller ⇒ Object
108
109
110
111
112
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
143
144
145
146
147
148
|
# File 'lib/superhosting/cli/base.rb', line 108
def get_controller
def get_subcontroller_option
key = :"#{self.class.get_splited_class_name.first}_name"
config[key] unless config[key].nil?
end
def get_method(m_name, node)
params = node.instance_method(:initialize).parameters
opts = {}
params.each do |req, name|
if req.to_s.start_with? 'key'
if name == :name
opt = get_subcontroller_option
elsif config.key? name
opt = config[name]
end
opts.merge!(name => opt) unless opt.nil?
end
end
CONTROLLER_BASE_OPTIONS.each { |opt| opts.merge!(opt => config[opt]) unless config[opt].nil? }
opts.merge!(logger: self.__logger)
node.new(**opts).method(m_name)
end
names = self.class.get_splited_class_name
node = names.one? ? CONTROLLERS_MODULE::Base : CONTROLLERS_MODULE
names.each do |n|
c_name = n.capitalize.to_sym
m_name = n.to_sym
if node.respond_to? :constants and node.constants.include? c_name
node = node.const_get(c_name)
elsif node.respond_to? :instance_methods and node.instance_methods(false).include? m_name
return get_method(m_name, node)
end
end
raise NetStatus::Exception, { message: 'Method doesn\'t found' }
end
|
#get_method(m_name, node) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/superhosting/cli/base.rb', line 114
def get_method(m_name, node)
params = node.instance_method(:initialize).parameters
opts = {}
params.each do |req, name|
if req.to_s.start_with? 'key'
if name == :name
opt = get_subcontroller_option
elsif config.key? name
opt = config[name]
end
opts.merge!(name => opt) unless opt.nil?
end
end
CONTROLLER_BASE_OPTIONS.each { |opt| opts.merge!(opt => config[opt]) unless config[opt].nil? }
opts.merge!(logger: self.__logger)
node.new(**opts).method(m_name)
end
|
#get_subcontroller_option ⇒ Object
109
110
111
112
|
# File 'lib/superhosting/cli/base.rb', line 109
def get_subcontroller_option
key = :"#{self.class.get_splited_class_name.first}_name"
config[key] unless config[key].nil?
end
|
#help ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/superhosting/cli/base.rb', line 57
def help
def get_childs_banners(node)
if node.is_a? Hash
node.map do |k,v|
if v.is_a? Hash
get_childs_banners(node[k])
else
v.banner
end
end.join("\n")
else
node.banner
end
end
self.info("#{opt_parser.to_s}\n#{get_childs_banners(@node) if self.class == Base}")
exit 1
end
|
#positional_arguments(args) ⇒ Object
209
210
211
|
# File 'lib/superhosting/cli/base.rb', line 209
def positional_arguments(args)
args.select { |arg| arg =~ /^([[:alnum:]\_\-]+)$/ }
end
|
#run ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/superhosting/cli/base.rb', line 77
def run
begin
net_status = action
net_status ||= {}
raise Error::Controller, net_status unless net_status[:error].nil?
self.debug('Done!')
unless (data = net_status[:data]).nil?
@node_class.after_action(data, config) if @node_class.respond_to? :after_action
end
rescue NetStatus::Exception => e
raise Error::Controller, e.net_status
end
end
|