Skip to content

Commit

Permalink
Fix decoder tests and do rustfmt (#4)
Browse files Browse the repository at this point in the history
* Update decoder tests to match new builder params

* rustfmt all of the things

* Provide more control over output location
  • Loading branch information
pirogoeth authored Sep 11, 2018
1 parent 687881b commit f3896fc
Show file tree
Hide file tree
Showing 16 changed files with 330 additions and 107 deletions.
77 changes: 77 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ log = "0.4.0"
quicli = "0.3.0"
rand = "0.5.5"
sodiumoxide = "0.1.0"
spinners = "1.0.0"
structopt = "0.2.10"
tar = "0.4.16"

Expand Down
65 changes: 51 additions & 14 deletions src/bin/sneakercopy.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
#![recursion_limit = "1024"]
#![feature(try_from)]

#[macro_use] extern crate quicli;
#[macro_use]
extern crate quicli;
extern crate sneakercopy;
extern crate sodiumoxide;
#[macro_use] extern crate structopt;
extern crate structopt;

use quicli::prelude::*;
use std::path::PathBuf;

use sneakercopy::{
*,
errors::*,
tarbox,
};
use sneakercopy::{errors::*, tarbox, *};

#[derive(Debug, StructOpt)]
#[structopt(raw(setting = "structopt::clap::AppSettings::ColoredHelp"))]
Expand All @@ -31,6 +28,21 @@ enum Subcommand {
Seal {
#[structopt(help = "File/folder path to archive", parse(from_os_str))]
path: PathBuf,

#[structopt(
short = "o",
long = "output",
help = "Optional output location",
parse(from_os_str)
)]
output: Option<PathBuf>,

#[structopt(
short = "f",
long = "force",
help = "Force overwriting of output"
)]
force: bool,
},

#[structopt(name = "unseal", about = "Unseal an encrypted archive")]
Expand All @@ -41,7 +53,12 @@ enum Subcommand {
#[structopt(help = "Password used for encryption")]
password: String,

#[structopt(short = "C", long = "extract-to", help = "Directory to extract archive to", parse(from_os_str))]
#[structopt(
short = "C",
long = "extract-to",
help = "Directory to extract archive to",
parse(from_os_str)
)]
dest: Option<PathBuf>,
},
}
Expand All @@ -66,8 +83,16 @@ main!(|args: Cli, log_level: verbosity| {
fn entrypoint(args: Cli) -> sneakercopy::errors::Result<()> {
let action = &args.subcmd;
match action {
Subcommand::Seal{ path } => seal_subcmd(&args, path)?,
Subcommand::Unseal{ path, password, dest } => unseal_subcmd(&args, path, dest, password)?,
Subcommand::Seal {
path,
output,
force,
} => seal_subcmd(&args, path, output, force)?,
Subcommand::Unseal {
path,
password,
dest,
} => unseal_subcmd(&args, path, dest, password)?,
}

Ok(())
Expand All @@ -84,14 +109,26 @@ fn check_path(path: &PathBuf) -> sneakercopy::errors::Result<()> {
Ok(())
}

fn seal_subcmd(_args: &Cli, path: &PathBuf) -> sneakercopy::errors::Result<()> {
fn seal_subcmd(
_args: &Cli,
path: &PathBuf,
output: &Option<PathBuf>,
force: &bool,
) -> sneakercopy::errors::Result<()> {
check_path(&path)?;
let secret = seal_path(&path)?;
println!("secret: {}", secret.password());

let secret = seal_path(&path, &output, *force)?;
println!("\nsecret: {}", secret.password());

Ok(())
}

fn unseal_subcmd(_args: &Cli, path: &PathBuf, dest: &Option<PathBuf>, password: &String) -> sneakercopy::errors::Result<()> {
fn unseal_subcmd(
_args: &Cli,
path: &PathBuf,
dest: &Option<PathBuf>,
password: &String,
) -> sneakercopy::errors::Result<()> {
check_path(&path)?;

let sb = tarbox::TarboxSecretBuilder::new();
Expand Down
2 changes: 1 addition & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ macro_rules! builder {
)*
}
};
}
}
10 changes: 7 additions & 3 deletions src/crypt.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use sodiumoxide::crypto::secretbox;

use super::{ BufResult, errors, tarbox };
use super::{errors, tarbox, BufResult};

pub fn encrypt_buffer(buf: &Vec<u8>, secret: &tarbox::TarboxSecret) -> BufResult {
Ok(secretbox::seal(buf.as_slice(), &secret.nonce(), &secret.key()))
Ok(secretbox::seal(
buf.as_slice(),
&secret.nonce(),
&secret.key(),
))
}

pub fn decrypt_buffer(buf: &Vec<u8>, secret: &tarbox::TarboxSecret) -> BufResult {
secretbox::open(buf.as_slice(), &secret.nonce(), &secret.key())
.or_else(|_| { bail!(errors::ErrorKind::SecretBoxOpenFail) })
.or_else(|_| bail!(errors::ErrorKind::SecretBoxOpenFail))
}
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ error_chain! {
display("could not open secretbox"),
}
}
}
}
8 changes: 4 additions & 4 deletions src/flate.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use libflate::gzip::{ Decoder, Encoder };
use std::io::{ Read, Write };
use libflate::gzip::{Decoder, Encoder};
use std::io::{Read, Write};
use std::vec::Vec;

use super::{ BufResult };
use super::BufResult;

pub fn compress_buffer(buf: &Vec<u8>) -> BufResult {
let mut compressor = Encoder::new(Vec::new())?;
Expand All @@ -29,4 +29,4 @@ pub fn inflate_buffer(buf: &Vec<u8>) -> BufResult {

// Finish the inflation stream
Ok(outbuf)
}
}
Loading

0 comments on commit f3896fc

Please sign in to comment.