Skip to content

Commit

Permalink
examples/*: rewrite to use future chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
steveej committed Oct 24, 2018
1 parent d1bdb2a commit 64255ef
Showing 1 changed file with 50 additions and 30 deletions.
80 changes: 50 additions & 30 deletions examples/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate dkregistry;
extern crate futures;
extern crate tokio_core;

use futures::stream::Stream;
use futures::prelude::*;
use std::{boxed, error};
use tokio_core::reactor::Core;

Expand Down Expand Up @@ -38,36 +38,56 @@ fn main() {
}

fn run(host: &str, user: Option<String>, passwd: Option<String>, image: &str) -> Result<()> {
let mut tcore = try!(Core::new());
let mut dclient = try!(
dkregistry::v2::Client::configure(&tcore.handle())
.registry(host)
.insecure_registry(false)
.username(user)
.password(passwd)
.build()
);
let mut tcore = Core::new()?;
let client = dkregistry::v2::Client::configure(&tcore.handle())
.registry(host)
.insecure_registry(false)
.username(user)
.password(passwd)
.build()?;

let futcheck = dclient.is_v2_supported();
let supported = try!(tcore.run(futcheck));
if !supported {
return Err("API v2 not supported".into());
}

let fut_token = dclient.login(&[&format!("repository:{}:pull", image)]);
let token_auth = try!(tcore.run(fut_token));
let futures = futures::future::ok::<_, dkregistry::errors::Error>(client)
.and_then(|dclient| {
dclient.is_v2_supported().and_then(|v2_supported| {
if !v2_supported {
Err("API v2 not supported".into())
} else {
Ok(dclient)
}
})
}).and_then(|dclient| {
dclient.is_auth(None).and_then(|is_auth| {
if is_auth {
Err("no login performed, but already authenticated".into())
} else {
Ok(dclient)
}
})
}).and_then(|dclient| {
dclient.login(&[]).and_then(|token| {
dclient
.is_auth(Some(token.token()))
.and_then(move |is_auth| {
if !is_auth {
Err("login failed".into())
} else {
println!("logged in!");
Ok(dclient
.clone()
.set_token(Some(token.token()))
.get_tags(&image, Some(7))
.collect())
}
})
})
}).and_then(|tags_collect| tags_collect);

let futauth = dclient.is_auth(Some(token_auth.token()));
if !try!(tcore.run(futauth)) {
return Err("login failed".into());
match tcore.run(futures) {
Ok(tags) => {
println!("{:?}", tags);
Ok(())
}
Err(e) => Err(Box::new(e)),
_ => Err("Login unsucessful".into()),
}

dclient.set_token(Some(token_auth.token()));

let fut_tags = dclient.get_tags(image, Some(7));
let tags = tcore.run(fut_tags.collect());

println!("{:?}", tags);

return Ok(());
}

0 comments on commit 64255ef

Please sign in to comment.