Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub(crate) struct Entry {
pub(crate) hash: Hash,
pub(crate) size: u64,
pub struct Entry {
pub hash: Hash,
pub size: u64,
}
2 changes: 1 addition & 1 deletion src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct Hash(blake3::Hash);
pub struct Hash(blake3::Hash);

impl Hash {
pub(crate) const LEN: usize = blake3::OUT_LEN;
Expand Down
104 changes: 104 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use {
self::{
arguments::Arguments, display_path::DisplayPath, display_secret::DisplaySecret, error::Error,
lint::Lint, lint_group::LintGroup, list::List, manifest::Manifest, metadata::Metadata,
options::Options, owo_colorize_ext::OwoColorizeExt, private_key::PrivateKey,
public_key::PublicKey, relative_path::RelativePath, signature::Signature,
signature_error::SignatureError, style::Style, subcommand::Subcommand, template::Template,
utf8_path_ext::Utf8PathExt,
},
blake3::Hasher,
camino::{Utf8Component, Utf8Path, Utf8PathBuf},
clap::{Parser, ValueEnum},
indicatif::{ProgressBar, ProgressStyle},
lexiclean::Lexiclean,
owo_colors::Styled,
serde::{Deserialize, Deserializer, Serialize, Serializer},
serde_with::{DeserializeFromStr, SerializeDisplay},
snafu::{ensure, ErrorCompat, OptionExt, ResultExt, Snafu},
std::{
array::TryFromSliceError,
backtrace::{Backtrace, BacktraceStatus},
cmp::Ordering,
collections::{BTreeMap, HashMap},
env,
fmt::{self, Display, Formatter},
fs::File,
io::{self, IsTerminal},
path::{Path, PathBuf},
process,
str::{self, FromStr},
},
walkdir::WalkDir,
};

pub use self::{entry::Entry, hash::Hash};

#[cfg(test)]
use assert_fs::TempDir;

mod arguments;
mod display_path;
mod display_secret;
mod entry;
mod error;
mod filesystem;
mod hash;
mod lint;
mod lint_group;
mod list;
mod manifest;
mod metadata;
mod options;
mod owo_colorize_ext;
mod private_key;
mod progress_bar;
mod public_key;
mod relative_path;
mod signature;
mod signature_error;
mod style;
mod subcommand;
mod template;
mod utf8_path_ext;

type Result<T = (), E = Error> = std::result::Result<T, E>;

const MASTER_PRIVATE_KEY: &str = "master.private";
const MASTER_PUBLIC_KEY: &str = "master.public";

fn current_dir() -> Result<Utf8PathBuf> {
Utf8PathBuf::from_path_buf(env::current_dir().context(error::CurrentDir)?)
.map_err(|path| error::PathUnicode { path }.build())
}

fn decode_path(path: &Path) -> Result<&Utf8Path> {
Utf8Path::from_path(path).context(error::PathUnicode { path })
}

pub fn run() {
if let Err(err) = Arguments::parse().run() {
let style = Style::stderr();
eprintln!(
"{}: {}",
"error".style(style.error()),
err.style(style.message()),
);

let causes = err.iter_chain().skip(1).count();

for (i, err) in err.iter_chain().skip(1).enumerate() {
eprintln!(" {}─ {err}", if i < causes - 1 { '├' } else { '└' });
}

if let Some(backtrace) = err.backtrace() {
if backtrace.status() == BacktraceStatus::Captured {
eprintln!();
eprintln!("backtrace:");
eprintln!("{backtrace}");
}
}

process::exit(1);
}
}
101 changes: 1 addition & 100 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,102 +1,3 @@
use {
self::{
arguments::Arguments, display_path::DisplayPath, display_secret::DisplaySecret, entry::Entry,
error::Error, hash::Hash, lint::Lint, lint_group::LintGroup, list::List, manifest::Manifest,
metadata::Metadata, options::Options, owo_colorize_ext::OwoColorizeExt,
private_key::PrivateKey, public_key::PublicKey, relative_path::RelativePath,
signature::Signature, signature_error::SignatureError, style::Style, subcommand::Subcommand,
template::Template, utf8_path_ext::Utf8PathExt,
},
blake3::Hasher,
camino::{Utf8Component, Utf8Path, Utf8PathBuf},
clap::{Parser, ValueEnum},
indicatif::{ProgressBar, ProgressStyle},
lexiclean::Lexiclean,
owo_colors::Styled,
serde::{Deserialize, Deserializer, Serialize, Serializer},
serde_with::{DeserializeFromStr, SerializeDisplay},
snafu::{ensure, ErrorCompat, OptionExt, ResultExt, Snafu},
std::{
array::TryFromSliceError,
backtrace::{Backtrace, BacktraceStatus},
cmp::Ordering,
collections::{BTreeMap, HashMap},
env,
fmt::{self, Display, Formatter},
fs::File,
io::{self, IsTerminal},
path::{Path, PathBuf},
process,
str::{self, FromStr},
},
walkdir::WalkDir,
};

#[cfg(test)]
use assert_fs::TempDir;

mod arguments;
mod display_path;
mod display_secret;
mod entry;
mod error;
mod filesystem;
mod hash;
mod lint;
mod lint_group;
mod list;
mod manifest;
mod metadata;
mod options;
mod owo_colorize_ext;
mod private_key;
mod progress_bar;
mod public_key;
mod relative_path;
mod signature;
mod signature_error;
mod style;
mod subcommand;
mod template;
mod utf8_path_ext;

type Result<T = (), E = Error> = std::result::Result<T, E>;

const MASTER_PRIVATE_KEY: &str = "master.private";
const MASTER_PUBLIC_KEY: &str = "master.public";

fn current_dir() -> Result<Utf8PathBuf> {
Utf8PathBuf::from_path_buf(env::current_dir().context(error::CurrentDir)?)
.map_err(|path| error::PathUnicode { path }.build())
}

fn decode_path(path: &Path) -> Result<&Utf8Path> {
Utf8Path::from_path(path).context(error::PathUnicode { path })
}

fn main() {
if let Err(err) = Arguments::parse().run() {
let style = Style::stderr();
eprintln!(
"{}: {}",
"error".style(style.error()),
err.style(style.message()),
);

let causes = err.iter_chain().skip(1).count();

for (i, err) in err.iter_chain().skip(1).enumerate() {
eprintln!(" {}─ {err}", if i < causes - 1 { '├' } else { '└' });
}

if let Some(backtrace) = err.backtrace() {
if backtrace.status() == BacktraceStatus::Captured {
eprintln!();
eprintln!("backtrace:");
eprintln!("{backtrace}");
}
}

process::exit(1);
}
filepack::run();
}
2 changes: 1 addition & 1 deletion src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Manifest {
Ok((path, manifest))
}

pub(crate) fn store(&self, path: &Utf8Path) -> Result<()> {
pub(crate) fn save(&self, path: &Utf8Path) -> Result<()> {
filesystem::write(path, format!("{}\n", serde_json::to_string(self).unwrap()))
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub(crate) struct Metadata {
impl Metadata {
pub(crate) const FILENAME: &'static str = "metadata.json";

pub(crate) fn store(&self, path: &Utf8Path) -> Result<()> {
pub(crate) fn save(&self, path: &Utf8Path) -> Result<()> {
filesystem::write(path, format!("{}\n", serde_json::to_string(self).unwrap()))
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Create {
self.force || !filesystem::exists(&path)?,
error::MetadataAlreadyExists { path: &path },
}
Metadata::from(template).store(&path)?;
Metadata::from(template).save(&path)?;
}

let cleaned_manifest = current_dir.join(&manifest_path).lexiclean();
Expand Down Expand Up @@ -185,7 +185,7 @@ impl Create {
manifest.signatures.insert(public_key, signature);
}

manifest.store(&manifest_path)?;
manifest.save(&manifest_path)?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Sign {

manifest.signatures.insert(public_key, signature);

manifest.store(&path)?;
manifest.save(&path)?;

Ok(())
}
Expand Down
9 changes: 2 additions & 7 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
fixture::{FileTouch, FileWriteBin, FileWriteStr, PathChild, PathCreateDir},
TempDir,
},
filepack::Entry,
predicates::str::RegexPredicate,
serde::{Deserialize, Serialize},
std::{collections::BTreeMap, fs, path::Path, str},
Expand Down Expand Up @@ -33,18 +34,12 @@ struct Manifest {
signatures: BTreeMap<String, String>,
}

#[derive(Deserialize, Serialize)]
pub(crate) struct Entry {
pub(crate) hash: String,
pub(crate) size: u64,
}

impl Manifest {
fn load(path: &Path) -> Self {
serde_json::from_str(&fs::read_to_string(path).unwrap()).unwrap()
}

fn store(&self, path: &Path) {
fn save(&self, path: &Path) {
fs::write(path, serde_json::to_string(self).unwrap()).unwrap();
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn existing_signatures_must_be_valid() {
"0".repeat(128),
);

manifest.store(&dir.child("foo/filepack.json"));
manifest.save(&dir.child("foo/filepack.json"));

Command::cargo_bin("filepack")
.unwrap()
Expand Down
6 changes: 3 additions & 3 deletions tests/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ fn weak_signature_public_key() {

manifest.signatures.insert("0".repeat(64), "0".repeat(128));

manifest.store(&dir.child("filepack.json"));
manifest.save(&dir.child("filepack.json"));

Command::cargo_bin("filepack")
.unwrap()
Expand Down Expand Up @@ -535,7 +535,7 @@ fn malformed_signature_error() {
"7f1420cdc898f9370fd196b9e8e5606a7992fab5144fc1873d91b8c65ef5db6b".into(),
);

manifest.store(&path);
manifest.save(&path);

Command::cargo_bin("filepack")
.unwrap()
Expand Down Expand Up @@ -581,7 +581,7 @@ fn valid_signature_for_wrong_pubkey_error() {
foo,
);

manifest.store(&dir.child("foo/filepack.json"));
manifest.save(&dir.child("foo/filepack.json"));

Command::cargo_bin("filepack")
.unwrap()
Expand Down