From 5017d258f48c945a02eaffa2bbc4cbcbf3fb0324 Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Fri, 22 Jul 2022 17:19:46 +0200 Subject: [PATCH] init: Add config options --- src/commands/config.rs | 4 ++-- src/commands/init.rs | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/commands/config.rs b/src/commands/config.rs index 666b3e2..8e63758 100644 --- a/src/commands/config.rs +++ b/src/commands/config.rs @@ -31,11 +31,11 @@ pub(super) async fn execute( pub(super) struct ConfigOpts { /// set compression level, 0 equals no compression #[clap(long, value_name = "LEVEL")] - set_compression: Option, + pub set_compression: Option, /// set repository version #[clap(long, value_name = "VERSION")] - set_version: Option, + pub set_version: Option, } impl ConfigOpts { diff --git a/src/commands/init.rs b/src/commands/init.rs index 984cfec..bae7421 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -5,6 +5,7 @@ use anyhow::{bail, Result}; use clap::Parser; use rpassword::{prompt_password, read_password_from_bufread}; +use super::config::ConfigOpts; use super::key::AddOpts; use crate::backend::{DecryptBackend, DecryptWriteBackend, FileType, WriteBackend}; use crate::chunker; @@ -16,6 +17,9 @@ use crate::repo::{ConfigFile, KeyFile}; pub(super) struct Opts { #[clap(flatten)] key_opts: AddOpts, + + #[clap(flatten)] + config_opts: ConfigOpts, } pub(super) async fn execute( @@ -28,6 +32,17 @@ pub(super) async fn execute( bail!("Config file already exists. Aborting."); } + // Create config first to allow catching errors from here without writing anything + let repo_id = Id::random(); + let chunker_poly = chunker::random_poly()?; + let version = match opts.config_opts.set_version { + None => 2, + Some(_) => 1, // will be changed later + }; + let mut config = ConfigFile::new(version, repo_id, chunker_poly); + opts.config_opts.apply(&mut config)?; + + // generate key let key = Key::new(); let key_opts = opts.key_opts; @@ -56,10 +71,7 @@ pub(super) async fn execute( } println!("key {} successfully added.", id); - let repo_id = Id::random(); - let chunker_poly = chunker::random_poly()?; - let mut config = ConfigFile::new(2, repo_id, chunker_poly); - + // save config let dbe = DecryptBackend::new(be, key.clone()); dbe.save_file(&config).await?;