Skip to content

Commit

Permalink
feat(CLI):handle output json encoding and ostreams
Browse files Browse the repository at this point in the history
* support for encoding response schemas to json
* support for simple downloads (without alt=media)

Fixes #63
  • Loading branch information
Byron committed Apr 14, 2015
1 parent c3a9f1e commit 3f49f50
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
2 changes: 2 additions & 0 deletions etc/api/type-cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ cargo:
- docopt = "= 0.6.59"
- docopt_macros = "= 0.6.59"
- rustc-serialize = "*"
- serde = ">= 0.3.0"
- serde_macros = "*"
6 changes: 3 additions & 3 deletions src/mako/api/lib/schema.mako
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ ${struct};
<%def name="new(s, c)">\
<%
markers = schema_markers(s, c, transitive=True)
traits = ['Clone', 'Debug']
# We always need Serialization support, as others might want to serialize the response, even though we will
# only deserialize it.
traits = ['Clone', 'Debug', 'Serialize']
# default only works for structs, and 'variant' will be an enum
if 'variant' not in s:
traits.insert(0, 'Default')
allow_optionals = True
if REQUEST_MARKER_TRAIT in markers:
traits.append('Serialize')
if RESPONSE_MARKER_TRAIT in markers:
traits.append('Deserialize')
Expand Down
23 changes: 14 additions & 9 deletions src/mako/cli/lib/engine.mako
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::default::Default;
use std::str::FromStr;
use oauth2::{Authenticator, DefaultAuthenticatorDelegate};
use rustc_serialize::json;
struct Engine {
opt: Options,
Expand Down Expand Up @@ -162,18 +163,22 @@ if dry_run {
return None
% else:
% if handle_output:
let ostream = match writer_from_opts(${SOPT + flag_ident(OUTPUT_FLAG)}, &${SOPT + arg_ident(OUT_ARG[1:-1])}) {
Err(cli_err) => {
err.issues.push(cli_err);
return None
},
Ok(bs) => bs,
};
let mut ostream = writer_from_opts(${SOPT + flag_ident(OUTPUT_FLAG)}, &${SOPT + arg_ident(OUT_ARG[1:-1])});
% endif # handle output
match call.${api.terms.action}() {
Err(api_err) => Some(api_err),
Ok(res) => {
println!("DEBUG: {:?}", res);
% if mc.response_schema:
Ok((response, output_schema)) => {
% else:
Ok(mut response) => {
% endif # handle output structure
println!("DEBUG: REMOVE ME {:?}", response);
% if mc.response_schema:
serde::json::to_writer(&mut ostream, &output_schema).unwrap();
% elif mc.m.get('supportsMediaDownload', False):
## Download is the only option - nothing else matters
io::copy(&mut response, &mut ostream).unwrap();
% endif
None
}
}
Expand Down
1 change: 1 addition & 0 deletions src/mako/cli/main.rs.mako
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
extern crate docopt;
extern crate yup_oauth2 as oauth2;
extern crate rustc_serialize;
extern crate serde;
extern crate hyper;
extern crate ${to_extern_crate_name(library_to_crate_name(library_name(name, version), make.depends_on_suffix))} as api;

Expand Down
15 changes: 4 additions & 11 deletions src/rust/cli/cmn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@ use std::default::Default;

// May panic if we can't open the file - this is anticipated, we can't currently communicate this
// kind of error: TODO: fix this architecture :)
pub fn writer_from_opts(flag: bool, arg: &str) -> Result<Box<Write>, CLIError> {
if !flag && arg == "-" {
Ok(Box::new(stdout()))
pub fn writer_from_opts(flag: bool, arg: &str) -> Box<Write> {
if !flag || arg == "-" {
Box::new(stdout())
} else {
match fs::OpenOptions::new().create(true).write(true).open(arg) {
Err(io_err) => {
Err(CLIError::Configuration(
ConfigurationError::IOError((arg.to_string(), io_err))
))
},
Ok(f) => Ok(Box::new(f)),
}
Box::new(fs::OpenOptions::new().create(true).write(true).open(arg).unwrap())
}
}

Expand Down

0 comments on commit 3f49f50

Please sign in to comment.