Fixes for CLI code

This doesn't work completely yet because yup_hyper_mock is pulling in old crates: it needs to be updated or removed, but I'm not sure that I have enough context to do it.
This commit is contained in:
Daniel Rodgers-Pryor
2021-02-05 00:23:04 +11:00
parent b8a66bc666
commit 0236e24291
3 changed files with 29 additions and 26 deletions

View File

@@ -179,11 +179,11 @@ ${util.crate_name()} = "*"
# This project intentionally uses an old version of Hyper. See
# https://github.com/Byron/google-apis-rs/issues/173 for more
# information.
hyper = "^0.10"
hyper-rustls = "^0.6"
hyper = "^0.14"
hyper-rustls = "^0.22"
serde = "^1.0"
serde_json = "^1.0"
yup-oauth2 = "^1.0"
yup-oauth2 = "^5.0"
```
${'##'} A complete example
@@ -249,7 +249,7 @@ Arguments will always be copied or cloned into the builder, to make them indepen
###############################################################################################
<%def name="test_hub(hub_type, comments=True)">\
use std::default::Default;
use oauth2::{Authenticator, DefaultAuthenticatorDelegate, ApplicationSecret, MemoryStorage};
use oauth2;
use ${util.library_name()}::${hub_type};
% if comments:
@@ -264,9 +264,10 @@ let secret: ApplicationSecret = Default::default();
// what's going on. You probably want to bring in your own `TokenStorage` to persist tokens and
// retrieve them from storage.
% endif
let auth = Authenticator::new(&secret, DefaultAuthenticatorDelegate,
hyper::Client::with_connector(hyper::net::HttpsConnector::new(hyper_rustls::TlsClient::new())),
<MemoryStorage as Default>::default(), None);
let auth = yup_oauth2::InstalledFlowAuthenticator::builder(
secret,
yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect,
).build().await.unwrap();
let mut hub = ${hub_type}::new(hyper::Client::with_connector(hyper::net::HttpsConnector::new(hyper_rustls::TlsClient::new())), auth);\
</%def>

View File

@@ -31,14 +31,13 @@
<%
hub_type_name = 'api::' + hub_type(c.schemas, util.canonical_name())
%>\
use client::{InvalidOptionsError, CLIError, JsonTokenStorage, arg_from_str, writer_from_opts, parse_kv_arg,
use client::{InvalidOptionsError, CLIError, arg_from_str, writer_from_opts, parse_kv_arg,
input_file_from_opts, input_mime_from_opts, FieldCursor, FieldError, CallType, UploadProtocol,
calltype_from_str, remove_json_null_values, ComplexType, JsonType, JsonTypeInfo};
use std::default::Default;
use std::str::FromStr;
use oauth2::{Authenticator, DefaultAuthenticatorDelegate, FlowType};
use serde_json as json;
use clap::ArgMatches;
@@ -49,7 +48,8 @@ enum DoitError {
struct Engine<'n> {
opt: ArgMatches<'n>,
hub: ${hub_type_name}<hyper::Client<hyper_rustls::HttpsConnector<hyper::client::connect::HttpConnector>, hyper::body::Body>, Authenticator<DefaultAuthenticatorDelegate, JsonTokenStorage, hyper::Client<hyper_rustls::HttpsConnector<hyper::client::connect::HttpConnector>, hyper::body::Body>>>,
hub: ${hub_type_name}<hyper::Client<hyper_rustls::HttpsConnector<hyper::client::connect::HttpConnector>, hyper::body::Body>
>,
gp: ${"Vec<&'static str>"},
gpm: Vec<(&'static str, &'static str)>,
}
@@ -58,14 +58,14 @@ struct Engine<'n> {
impl<'n> Engine<'n> {
% for resource in sorted(c.rta_map.keys()):
% for method in sorted(c.rta_map[resource]):
fn ${call_method_ident(resource, method)}(&self, opt: &ArgMatches<'n>, dry_run: bool, err: &mut InvalidOptionsError)
async fn ${call_method_ident(resource, method)}(&self, opt: &ArgMatches<'n>, dry_run: bool, err: &mut InvalidOptionsError)
-> Result<(), DoitError> {
${self._method_call_impl(c, resource, method) | indent_all_but_first_by(2)}
}
% endfor # each method
% endfor
fn _doit(&self, dry_run: bool) -> Result<Result<(), DoitError>, Option<InvalidOptionsError>> {
async fn _doit(&self, dry_run: bool) -> Result<Result<(), DoitError>, Option<InvalidOptionsError>> {
let mut err = InvalidOptionsError::new();
let mut call_result: Result<(), DoitError> = Ok(());
let mut err_opt: Option<InvalidOptionsError> = None;
@@ -76,7 +76,7 @@ impl<'n> Engine<'n> {
match opt.subcommand() {
% for method in sorted(c.rta_map[resource]):
("${mangle_subcommand(method)}", Some(opt)) => {
call_result = self.${call_method_ident(resource, method)}(opt, dry_run, &mut err);
call_result = self.${call_method_ident(resource, method)}(opt, dry_run, &mut err).await;
},
% endfor # each method
_ => {
@@ -103,7 +103,7 @@ impl<'n> Engine<'n> {
}
// Please note that this call will fail if any part of the opt can't be handled
fn new(opt: ArgMatches<'n>) -> Result<Engine<'n>, InvalidOptionsError> {
async fn new(opt: ArgMatches<'n>) -> Result<Engine<'n>, InvalidOptionsError> {
let (config_dir, secret) = {
let config_dir = match client::assure_config_dir_exists(opt.value_of("${CONFIG_DIR_ARG}").unwrap_or("${CONFIG_DIR}")) {
Err(e) => return Err(InvalidOptionsError::single(e, 3)),
@@ -117,12 +117,10 @@ impl<'n> Engine<'n> {
}
};
let auth = Authenticator::new( &secret, DefaultAuthenticatorDelegate,
${self._debug_client(DEBUG_AUTH_FLAG) | indent_all_but_first_by(10)},
JsonTokenStorage {
program_name: "${util.program_name()}",
db_dir: config_dir.clone(),
}, Some(FlowType::InstalledRedirect(54324)));
let auth = yup_oauth2::InstalledFlowAuthenticator::builder(
secret,
yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect,
).persist_tokens_to_disk(format!("{}/${util.program_name()}", config_dir)).build().await.unwrap();
let client =
${self._debug_client(DEBUG_FLAG) | indent_all_but_first_by(3)};
@@ -138,15 +136,15 @@ impl<'n> Engine<'n> {
]
};
match engine._doit(true) {
match engine._doit(true).await {
Err(Some(err)) => Err(err),
Err(None) => Ok(engine),
Ok(_) => unreachable!(),
}
}
fn doit(&self) -> Result<(), DoitError> {
match self._doit(false) {
async fn doit(&self) -> Result<(), DoitError> {
match self._doit(false).await {
Ok(res) => res,
Err(_) => unreachable!(),
}
@@ -306,7 +304,7 @@ if dry_run {
% endfor
CallType::Standard => unreachable!()
% else:
CallType::Standard => call.${api.terms.action}(),
CallType::Standard => call.${api.terms.action}().await,
_ => unreachable!()
% endif
} {

View File

@@ -14,6 +14,9 @@
</%block>
#![allow(unused_variables, unused_imports, dead_code, unused_mut)]
#[macro_use]
extern crate tokio;
#[macro_use]
extern crate clap;
extern crate yup_oauth2 as oauth2;
@@ -36,7 +39,8 @@ mod client;
${engine.new(c)}\
fn main() {
#[tokio::main]
async fn main() {
let mut exit_status = 0i32;
${argparse.new(c) | indent_all_but_first_by(1)}\
let matches = app.get_matches();
@@ -48,7 +52,7 @@ fn main() {
writeln!(io::stderr(), "{}", err).ok();
},
Ok(engine) => {
if let Err(doit_err) = engine.doit() {
if let Err(doit_err) = engine.doit().await {
exit_status = 1;
match doit_err {
DoitError::IoError(path, err) => {