Skip to content

Commit

Permalink
Merge pull request crev-dev#346 from ffranr/remove_stdout_from_crev_lib
Browse files Browse the repository at this point in the history
Move all stdout and stderr from Local.generate_id to cargo-crev.
  • Loading branch information
dpc committed May 10, 2020
2 parents dc0011d + 488cc9e commit c6c7b66
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 63 deletions.
44 changes: 33 additions & 11 deletions cargo-crev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crev_common::convert::OptionDeref;
use crev_lib::{self, local::Local};
use std::{
collections::{HashMap, HashSet},
io::BufRead,
io::{self, BufRead},
path::PathBuf,
};
use structopt::StructOpt;
Expand Down Expand Up @@ -180,20 +180,42 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
match command {
opts::Command::Id(args) => match args {
opts::Id::New(args) => {
let local = Local::auto_create_or_open()?;
let res = local.generate_id_interactively(
args.url,
args.github_username,
args.use_https_push,
);
if res.is_err() {
eprintln!(
"Visit https://github.com/crev-dev/crev/wiki/Proof-Repository for help."
let url = match (args.url, args.github_username) {
(Some(url), None) => url,
(None, Some(username)) => {
format!("https://github.com/{}/crev-proofs", username)
}
_ => bail!("Must provide either a github username or url, but not both."),
};
if !url.starts_with("https://") {
bail!("URL must start with 'https://'");
}

fn read_new_passphrase() -> io::Result<String> {
println!("CrevID will be protected by a passphrase.");
println!(
"There's no way to recover your CrevID if you forget your passphrase."
);
crev_common::read_new_passphrase()
}
let local = Local::auto_create_or_open()?;
let res = local
.generate_id(&url, args.use_https_push, read_new_passphrase)
.map_err(|e| {
eprintln!("To create your proof repository, fork the template:");
eprintln!("https://github.com/crev-dev/crev-proofs/fork");
eprintln!(
"For help visit: https://github.com/crev-dev/crev/wiki/Proof-Repository"
);
eprintln!();
e
})?;
println!("Your CrevID was created and will be printed below in an encrypted form.");
println!("Make sure to back it up on another device, to prevent losing it.");
println!("{}", res);

let local = crev_lib::Local::auto_open()?;
let _ = ensure_known_owners_list_exists(&local);
res?;
}
opts::Id::Switch(args) => {
let local = Local::auto_open()?;
Expand Down
60 changes: 8 additions & 52 deletions crev-lib/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ impl Local {

self.ensure_proofs_root_exists()?;

debug_assert!(git_https_url.starts_with("https://"));
match git2::Repository::clone(git_https_url, &proof_dir) {
Ok(repo) => {
eprintln!("{} cloned to {}", git_https_url, proof_dir.display());
Expand Down Expand Up @@ -970,65 +971,20 @@ impl Local {
/// The callback should provide a passphrase
pub fn generate_id(
&self,
url: Option<String>,
github_username: Option<String>,
url: &str,
use_https_push: bool,
read_new_passphrase: impl FnOnce() -> std::io::Result<String>,
) -> Result<()> {
let url = match (url, github_username) {
(Some(url), None) => url,
(None, Some(username)) => format!("https://github.com/{}/crev-proofs", username),
(Some(_), Some(_)) => bail!("Can't provide both username and url"),
(None, None) => bail!("Must provide github username or url"),
};

if !url.starts_with("https://") {
bail!("URL must start with 'https://");
}

self.clone_proof_dir_from_git(&url, use_https_push)
.map_err(|e| {
eprintln!("To create your proof repository, fork the template:");
eprintln!("https://github.com/crev-dev/crev-proofs/fork");
eprintln!();
e
})?;

eprintln!("CrevID will be protected by a passphrase.");
eprintln!("There's no way to recover your CrevID if you forget your passphrase.");
let passphrase = read_new_passphrase()?;
) -> Result<id::LockedId> {
self.clone_proof_dir_from_git(&url, use_https_push)?;

let id = crev_data::id::OwnId::generate(crev_data::Url::new_git(url));
let locked = id::LockedId::from_own_id(&id, &passphrase)?;
let passphrase = read_new_passphrase()?;
let locked_id = id::LockedId::from_own_id(&id, &passphrase)?;

self.save_locked_id(&locked)?;
self.save_locked_id(&locked_id)?;
self.save_current_id(id.as_ref())?;

eprintln!("");
eprintln!("Your CrevID was created and will be printed below in an encrypted form.");
eprintln!("Make sure to back it up on another device, to prevent losing it.");

eprintln!("");
println!("{}", locked);

self.init_repo_readme_using_template()?;

Ok(())
}

/// Same as generate_id, but asks for a passphrase interactively
pub fn generate_id_interactively(
&self,
url: Option<String>,
github_username: Option<String>,
use_https_push: bool,
) -> Result<()> {
self.generate_id(
url,
github_username,
use_https_push,
crev_common::read_new_passphrase,
)
Ok(locked_id)
}

pub fn switch_id(&self, id_str: &str) -> Result<()> {
Expand Down

0 comments on commit c6c7b66

Please sign in to comment.