Skip to content

Commit 0dd854b

Browse files
committed
more human readable error messages
1 parent 778eef5 commit 0dd854b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/crypt.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use aes_gcm_siv::{
1010
};
1111
use anyhow::{anyhow, Context, Result};
1212
use colored::Colorize;
13+
use die_exit::die;
1314
use futures_util::{stream::FuturesOrdered, StreamExt};
1415
use log::{debug, info};
1516
use sha3::{Digest, Sha3_224};
@@ -150,7 +151,7 @@ pub async fn encrypt_file(file: impl AsRef<Path>, repo: &Repo) -> anyhow::Result
150151
.with_context(|| format!("{:?}", file))?;
151152
let (compressed, new_file) = try_compress(&bytes, new_file, repo.conf.zstd_level)?;
152153
let (encrypted, new_file) =
153-
encrypt_change_path(repo.get_key()?.as_bytes(), &compressed, new_file)?;
154+
encrypt_change_path(repo.get_key().as_bytes(), &compressed, new_file)?;
154155
compio::fs::write(&new_file, encrypted).await.0?;
155156
compio::fs::remove_file(file).await?;
156157
debug!("Encrypted filename: {:?}", new_file);
@@ -168,7 +169,7 @@ pub async fn decrypt_file(file: impl AsRef<Path>, repo: &Repo) -> anyhow::Result
168169
.await
169170
.with_context(|| format!("{:?}", file.as_ref()))?;
170171
let (decrypted, new_file) =
171-
try_decrypt_change_path(repo.get_key()?.as_bytes(), &bytes, new_file)?;
172+
try_decrypt_change_path(repo.get_key().as_bytes(), &bytes, new_file)?;
172173
let (decompressed, new_file) = try_decompress(&decrypted, new_file)?;
173174
compio::fs::write(&new_file, decompressed).await.0?;
174175
compio::fs::remove_file(&file).await?;
@@ -177,7 +178,11 @@ pub async fn decrypt_file(file: impl AsRef<Path>, repo: &Repo) -> anyhow::Result
177178
}
178179

179180
pub async fn encrypt_repo(repo: &Repo) -> anyhow::Result<()> {
181+
assert!(!repo.get_key().is_empty(), "Key must not be empty");
180182
let patterns = &repo.conf.crypt_list;
183+
if patterns.is_empty() {
184+
die!("No file to encrypt, please exec `git-se add <FILE>` first.");
185+
}
181186
repo.add_all()?;
182187
let mut encrypt_futures = repo
183188
.ls_files_absolute_with_given_patterns(
@@ -199,6 +204,7 @@ pub async fn encrypt_repo(repo: &Repo) -> anyhow::Result<()> {
199204
}
200205

201206
pub async fn decrypt_repo(repo: &Repo) -> anyhow::Result<()> {
207+
assert!(!repo.get_key().is_empty(), "Key must not be empty");
202208
let dot_pattern = String::from("*.") + ENCRYPTED_EXTENSION;
203209
let mut decrypt_futures = repo
204210
.ls_files_absolute_with_given_patterns(&[dot_pattern.as_str()])?

src/repo.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::path::{Path, PathBuf};
22

33
use anyhow::{anyhow, Result};
44
use assert2::assert;
5+
use die_exit::Die;
56
use log::debug;
67
use tap::Tap;
78

@@ -55,8 +56,9 @@ impl Repo {
5556
.collect();
5657
Ok(files_zip?.into_iter().flatten().collect())
5758
}
58-
pub fn get_key(&self) -> Result<String> {
59+
pub fn get_key(&self) -> String {
5960
self.get_config("key")
61+
.die("Key not found, please exec `git-se set key <KEY>` first.")
6062
}
6163
}
6264

0 commit comments

Comments
 (0)