diff --git a/src/commands/backup.rs b/src/commands/backup.rs index c435645..b431302 100644 --- a/src/commands/backup.rs +++ b/src/commands/backup.rs @@ -13,8 +13,9 @@ use crate::archiver::{Archiver, Parent}; use crate::backend::{ DecryptFullBackend, DryRunBackend, LocalSource, LocalSourceOptions, ReadSource, }; +use crate::id::Id; use crate::index::IndexBackend; -use crate::repo::{ConfigFile, Id, SnapshotFile, StringList}; +use crate::repo::{ConfigFile, SnapshotFile, StringList}; #[derive(Parser)] pub(super) struct Opts { @@ -44,6 +45,7 @@ pub(super) struct Opts { pub(super) async fn execute( be: &impl DecryptFullBackend, opts: Opts, + config_id: &Id, command: String, ) -> Result<()> { let mut snap = SnapshotFile { @@ -51,7 +53,7 @@ pub(super) async fn execute( ..Default::default() }; - let config: ConfigFile = be.get_file(&Id::default()).await?; + let config: ConfigFile = be.get_file(config_id).await?; let poly = config.poly()?; let be = DryRunBackend::new(be.clone(), opts.dry_run); diff --git a/src/commands/init.rs b/src/commands/init.rs index 92e95d5..8b20b71 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -1,7 +1,7 @@ use std::fs::File; use std::io::BufReader; -use anyhow::{bail, Result}; +use anyhow::Result; use clap::Parser; use rpassword::{prompt_password_stderr, read_password_with_reader}; @@ -21,10 +21,6 @@ pub(super) struct Opts { pub(super) async fn execute(be: &impl WriteBackend, opts: Opts) -> Result<()> { let key = Key::new(); - let ids = be.list(FileType::Config).await?; - if !ids.is_empty() { - bail!("Config file already exists. Aborting.") - } be.create().await?; let key_opts = opts.key_opts; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 4a1cf95..9b55c04 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,9 +1,9 @@ use std::path::PathBuf; -use anyhow::Result; +use anyhow::{bail, Result}; use clap::{Parser, Subcommand}; -use crate::backend::{ChooseBackend, DecryptBackend}; +use crate::backend::{ChooseBackend, DecryptBackend, FileType, ReadBackend}; mod backup; mod cat; @@ -104,17 +104,22 @@ pub async fn execute() -> Result<()> { let be = ChooseBackend::from_url(&args.repository); - let (key, dbe) = match args.command { - Command::Init(opts) => return init::execute(&be, opts).await, - _ => { + let config_ids = be.list(FileType::Config).await?; + + let (cmd, key, dbe, config_id) = match (args.command, config_ids.len()) { + (Command::Init(opts), 0) => return init::execute(&be, opts).await, + (Command::Init(_), _) => bail!("Config file already exists. Aborting."), + (cmd, 1) => { let key = get_key(&be, args.password_file).await?; let dbe = DecryptBackend::new(&be, key.clone()); - (key, dbe) + (cmd, key, dbe, &config_ids[0]) } + (_, 0) => bail!("No config file found. Is there a repo?"), + _ => bail!("More than one config file. Aborting."), }; - match args.command { - Command::Backup(opts) => backup::execute(&dbe, opts, command).await?, + match cmd { + Command::Backup(opts) => backup::execute(&dbe, opts, config_id, command).await?, Command::Cat(opts) => cat::execute(&dbe, opts).await?, Command::Check(opts) => check::execute(&dbe, opts).await?, Command::Diff(opts) => diff::execute(&dbe, opts).await?,