Skip to content

Commit

Permalink
add user output
Browse files Browse the repository at this point in the history
  • Loading branch information
lxl66566 committed May 4, 2024
1 parent 268c124 commit 4003459
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test.txt crypt=1
test.txt crypt=1
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "Encrypt/decrypt files in git repo using one password"
homepage = "https://github.com/lxl66566/git-simple-encrypt"
repository = "https://github.com/lxl66566/git-simple-encrypt"
license = "MIT"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
readme = "./README.md"
categories = ["cryptography"]
Expand Down
18 changes: 13 additions & 5 deletions src/crypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use aes_gcm_siv::{
Aes128GcmSiv, Nonce,
};
use anyhow::{anyhow, Context, Result};
#[cfg(any(test, debug_assertions))]
use colored::Colorize;
use log::{debug, info};
use same_file::is_same_file;
Expand Down Expand Up @@ -139,30 +138,39 @@ pub async fn encrypt_file(file: impl AsRef<Path>) -> anyhow::Result<PathBuf> {
);
return Ok(new_file);
}
info!("Encrypting file: {:?}", file);
if file.extension() == Some(ENCRYPTED_EXTENSION.as_ref()) {
println!(
"{}",
"Warning: file has been encrypted, do not encrypt.".yellow()
)
}
println!("Encrypting file: `{}`", format!("{:?}", file).green());
let bytes = compio::fs::read(file)
.await
.with_context(|| format!("{:?}", file))?;
let (compressed, new_file) = try_compress(&bytes, new_file)?;
let (encrypted, new_file) = encrypt_change_path(CONFIG.key.as_bytes(), &compressed, new_file)?;
compio::fs::write(&new_file, encrypted).await.0?;
debug!("Encrypted filename: {:?}", new_file);
compio::fs::remove_file(file).await?;
debug!("Encrypted filename: {:?}", new_file);
Ok(new_file)
}

/// decrypt file, and unlink it.
pub async fn decrypt_file(file: impl AsRef<Path>) -> anyhow::Result<PathBuf> {
info!("Decrypting file: {:?}", file.as_ref());
println!(
"Decrypting file: `{}`",
format!("{:?}", file.as_ref()).green()
);
let new_file = file.as_ref().to_owned();
let bytes = compio::fs::read(&file)
.await
.with_context(|| format!("{:?}", file.as_ref()))?;
let (decrypted, new_file) = try_decrypt_change_path(CONFIG.key.as_bytes(), &bytes, new_file)?;
let (decompressed, new_file) = try_decompress(&decrypted, new_file)?;
debug!("Decrypted filename: {:?}", new_file);
compio::fs::write(&new_file, decompressed).await.0?;
compio::fs::remove_file(&file).await?;
debug!("Decrypted filename: {:?}", new_file);
Ok(new_file)
}

Expand Down
1 change: 0 additions & 1 deletion src/git_command/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{
};

use anyhow::Ok;
use colored::Colorize;
use die_exit::DieWith;
use git2::{AttrCheckFlags, IndexAddOption, Repository};
use log::debug;
Expand Down
1 change: 1 addition & 0 deletions src/git_command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub async fn decrypt_repo() -> anyhow::Result<()> {
)
}
}
add_all()?;
Ok(())
}

Expand Down
29 changes: 29 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![feature(lazy_cell)]
#![feature(vec_pop_if)]
#![feature(let_chains)]
#![warn(clippy::nursery, clippy::cargo)]
#![allow(clippy::multiple_crate_versions)]

mod cli;
mod crypt;
mod git_command;
mod utils;

use anyhow::{Ok, Result};
use git_command::{
add_crypt_attributes, config, decrypt_repo, encrypt_repo, remove_crypt_attributes,
};

pub use crate::cli::{SubCommand, CLI};

pub async fn run(command: &SubCommand) -> Result<()> {
match command {
SubCommand::Encrypt => encrypt_repo().await?,
SubCommand::Decrypt => decrypt_repo().await?,
SubCommand::SetKey { key } => config::set_key(key)?,
SubCommand::Add { path } => add_crypt_attributes(path)?,
SubCommand::Remove { path } => remove_crypt_attributes(path)?,
SubCommand::Set { field, value } => config::set(field.name(), value)?,
}
Ok(())
}
28 changes: 3 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
#![feature(lazy_cell)]
#![feature(vec_pop_if)]
#![feature(let_chains)]
#![warn(clippy::nursery, clippy::cargo)]

mod cli;
mod crypt;
mod git_command;
mod utils;

use anyhow::{Ok, Result};
use cli::{SubCommand, CLI};
use git_command::{
add_crypt_attributes, config, decrypt_repo, encrypt_repo, remove_crypt_attributes,
};
use git_simple_encrypt::{run, CLI};

#[compio::main]
async fn main() -> Result<()> {
async fn main() -> anyhow::Result<()> {
env_logger::init();
match &CLI.command {
SubCommand::Encrypt => encrypt_repo().await?,
SubCommand::Decrypt => decrypt_repo().await?,
SubCommand::SetKey { key } => config::set_key(key)?,
SubCommand::Add { path } => add_crypt_attributes(path)?,
SubCommand::Remove { path } => remove_crypt_attributes(path)?,
SubCommand::Set { field, value } => config::set(field.name(), value)?,
}
Ok(())
run(&CLI.command).await
}

0 comments on commit 4003459

Please sign in to comment.