Skip to content

Commit

Permalink
feat(CLI): infrastructure for call and dry-run
Browse files Browse the repository at this point in the history
Now we are able to cleanly handle our arguments on a per-method basis.
The generated code won't clutter our design as we put the details into
their own methods.

Fixes #59
  • Loading branch information
Byron committed Apr 14, 2015
1 parent 8afc76a commit d6919f1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/mako/cli/lib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,16 @@ def is_request_value_property(mc, p):
def mangle_subcommand(name):
return util.camel_to_under(name).replace('_', '-').replace('.', '-')

def ident(name):
return mangle_subcommand(name).replace('-', '_')

# return the identifier of a command for the given name, suitable to address the command field in the docopt structure
def cmd_ident(name):
return 'cmd_' + mangle_subcommand(name).replace('-', '_')
return 'cmd_' + ident(name)

# Returns identifier for method dealing with options for the given resource-method pair
def call_method_ident(resource, method):
return '_%s_%s' % (ident(resource), ident(method))

# transform the resource name into a suitable filename to contain the markdown documentation for it
def subcommand_md_filename(resource, method):
Expand Down
25 changes: 18 additions & 7 deletions src/mako/cli/lib/engine.mako
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from util import hub_type
from cli import (mangle_subcommand, new_method_context, PARAM_FLAG, STRUCT_FLAG, UPLOAD_FLAG, OUTPUT_FLAG, VALUE_ARG,
CONFIG_DIR, SCOPE_FLAG, is_request_value_property, FIELD_SEP, docopt_mode, FILE_ARG, MIME_ARG, OUT_ARG,
cmd_ident)
cmd_ident, call_method_ident)
v_arg = '<%s>' % VALUE_ARG
%>\
Expand All @@ -24,8 +24,18 @@ struct Engine {
impl Engine {
% for resource in sorted(c.rta_map.keys()):
% for method in sorted(c.rta_map[resource]):
fn ${call_method_ident(resource, method)}(&self, dry_run: bool, err: &mut InvalidOptionsError) -> Option<api::Error> {
${self._method_call_impl(c, resource, method)}\
}
% endfor # each method
% endfor
fn _doit(&self, dry_run: bool) -> (Option<api::Error>, Option<InvalidOptionsError>) {
let mut err = InvalidOptionsError::new();
let mut call_result: Option<api::Error> = None;
let mut err_opt: Option<InvalidOptionsError> = None;
## RESOURCE LOOP: check for set primary subcommand
% for resource in sorted(c.rta_map.keys()):
Expand All @@ -43,7 +53,7 @@ self.opt.${cmd_ident(resource)} {
else if \
% endif
self.opt.${cmd_ident(method)} {
call_result = self.${call_method_ident(resource, method)}(dry_run, &mut err);
}\
% endfor # each method
else {
Expand All @@ -57,13 +67,10 @@ self.opt.${cmd_ident(method)} {
if dry_run {
if err.issues.len() > 0 {
(None, Some(err))
} else {
(None, None)
err_opt = Some(err);
}
} else {
unreachable!();
}
(call_result, err_opt)
}
// Please note that this call will fail if any part of the opt can't be handled
Expand Down Expand Up @@ -104,4 +111,8 @@ self.opt.${cmd_ident(method)} {
self._doit(false).0
}
}
</%def>

<%def name="_method_call_impl(c, resource, method)">\
None
</%def>

0 comments on commit d6919f1

Please sign in to comment.