Skip to content

Commit

Permalink
Use set-url to start ID creation process if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Feb 22, 2021
1 parent fca7a5b commit d5efe62
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions cargo-crev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
doc = "See [user documentation module](./doc/user/index.html)."
)]
#![cfg_attr(feature = "documentation", feature(external_doc))]
use crev_data::UnlockedId;
use crev_data::proof::ContentExt;
use crate::prelude::*;
use crev_data::proof::ContentExt;
use crev_data::UnlockedId;
use crev_lib::id::LockedId;

use crev_lib::{self, local::Local};
Expand Down Expand Up @@ -225,12 +225,15 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
if !args.url.starts_with("https://") {
bail!("URL must be https://");
}
let local = Local::auto_open()?;
let locked_id = local.read_current_locked_id()?;
let pub_id = locked_id.to_public_id().id.clone();
local.change_locked_id_url(locked_id, &args.url, args.use_https_push)?;
local.save_current_id(&pub_id)?;
local.fetch_trusted(opts::TrustDistanceParams::default().into(), None)?;
match current_id_set_url(&args.url, args.use_https_push) {
Err(crev_lib::Error::CurrentIDNotSet)
| Err(crev_lib::Error::IDNotSpecifiedAndCurrentIDNotSet)
| Err(crev_lib::Error::UserConfigNotInitialized) => {
eprintln!("set-url requires a CrevID set up, so we'll set up one now.");
generate_new_id_interactively(Some(&args.url), args.use_https_push, false)?
}
res => res?,
}
}
opts::Id::Export(args) => {
let local = Local::auto_open()?;
Expand Down Expand Up @@ -583,6 +586,16 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
Ok(CommandExitStatus::Success)
}

fn current_id_set_url(url: &str, use_https_push: bool) -> Result<(), crev_lib::Error> {
let local = Local::auto_open()?;
let locked_id = local.read_current_locked_id()?;
let pub_id = locked_id.to_public_id().id.clone();
local.change_locked_id_url(locked_id, url, use_https_push)?;
local.save_current_id(&pub_id)?;
local.fetch_trusted(opts::TrustDistanceParams::default().into(), None)?;
Ok(())
}

/// Interactive process of setting up a new CrevID
fn generate_new_id_interactively(url: Option<&str>, use_https_push: bool, allow_no_url: bool) -> Result<()> {
// Avoid creating new CrevID if it's not necessary
Expand Down

0 comments on commit d5efe62

Please sign in to comment.