Skip to content

Commit

Permalink
Merge branch 'master' into issue-32
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospb19 committed Oct 31, 2021
2 parents 7c82f2b + a02eb45 commit 95d38e9
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 200 deletions.
43 changes: 42 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,27 @@ jobs:
- name: Install dependencies for musl libc
run: |
sudo apt-get update
sudo apt-get install musl-tools
sudo apt-get install help2man musl-tools
- name: Run cargo build
uses: actions-rs/cargo@v1
with:
command: build
args: --release --target x86_64-unknown-linux-musl
env:
GEN_COMPLETIONS: 1

- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --target x86_64-unknown-linux-musl

- name: Build man page and find completions
run: |
help2man target/x86_64-unknown-linux-musl/release/ouch > ouch.1
cp -r target/x86_64-unknown-linux-musl/release/build/ouch-*/out/completions .
- name: Strip binary
run: strip target/x86_64-unknown-linux-musl/release/ouch

Expand All @@ -127,6 +134,17 @@ jobs:
name: 'ouch-x86_64-linux-musl'
path: target/x86_64-unknown-linux-musl/release/ouch

- name: Upload completions
uses: actions/upload-artifact@v2
with:
name: completions
path: completions

- name: Upload man page
uses: actions/upload-artifact@v2
with:
name: ouch.1
path: ouch.1

x86_64_glibc:
name: Ubuntu 20.04 (glibc)
Expand Down Expand Up @@ -274,3 +292,26 @@ jobs:
# with:
# name: 'ouch-x86_64-pc-windows-gnu'
# path: target\x86_64-pc-windows-gnu\release\ouch.exe

fmt:
name: Check sourcecode format
runs-on: ubuntu-latest

steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
target: x86_64-unknown-linux-musl
components: rustfmt
override: true

- name: Check format with cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
10 changes: 5 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Feel free to open an issue anytime you wish to ask a question, suggest a feature

# Requirements

1. Be kind, considerate and respectfull.
2. If editing .rs files, run `rustfmt` on them before commiting.
1. Be nice to other people.
2. If editing the Rust source code, remember to run `rustfmt` (otherwise, CI will warn you the code was not properly formatted).

Note that we are using `unstable` features of `rustfmt`, so you will need to change your toolchain to nightly.
Note: we are using `unstable` features of `rustfmt`! Nightly toolchain is required (will likely be installed automatically, cause the toolchain was specified in the project root).

# Suggestions

1. Ask for some guidance before solving an error if you feel like it.
2. If editing Rust code, run `clippy` before commiting.
1. If you wish to, you can ask for some guidance before solving an issue.
2. Run `cargo clippy` too.
17 changes: 17 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ description = "A command-line utility for easily compressing and decompressing f
[dependencies]
clap = "=3.0.0-beta.5" # Keep it pinned while in beta!
atty = "0.2.14"
fs-err = "2.6.0"
once_cell = "1.8.0"
walkdir = "2.3.2"
bzip2 = "0.4.3"
Expand All @@ -25,6 +26,10 @@ zip = { version = "0.5.13", default-features = false, features = ["defl
flate2 = { version = "1.0.22", default-features = false, features = ["zlib"] }
zstd = { version = "0.9.0", default-features = false, features = ["thin"] }

[build-dependencies]
clap = "=3.0.0-beta.5"
clap_generate = "=3.0.0-beta.5"

[dev-dependencies]
tempfile = "3.2.0"
infer = "0.5.0"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ ouch decompress a.zip b.tar.gz c.tar
ouch d a.zip
```

You can redirect the decompression results to another folder with the `-o/--output` flag.
You can redirect the decompression results to another folder with the `-d/--dir` flag.

```sh
# Decompress 'summer_vacation.zip' inside of new folder 'pictures'
ouch decompress summer_vacation.zip -o pictures
ouch decompress summer_vacation.zip -d pictures
```

### Compressing
Expand Down
22 changes: 22 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use clap::{ArgEnum, IntoApp};
use clap_generate::{generate_to, Shell};

use std::{env, fs::create_dir_all, path::Path};

include!("src/opts.rs");

fn main() {
println!("cargo:rerun-if-env-changed=GEN_COMPLETIONS");

if env::var_os("GEN_COMPLETIONS") != Some("1".into()) {
return;
}

let out = &Path::new(&env::var_os("OUT_DIR").unwrap()).join("completions");
create_dir_all(out).unwrap();
let app = &mut Opts::into_app();

for shell in Shell::value_variants() {
generate_to(*shell, app, "ouch", out).unwrap();
}
}
14 changes: 11 additions & 3 deletions src/archive/tar.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
//! Contains Tar-specific building and unpacking functions

use std::{
env, fs,
env,
io::prelude::*,
path::{Path, PathBuf},
};

use fs_err as fs;
use tar;
use walkdir::WalkDir;

use crate::{
error::FinalError,
info,
utils::{self, Bytes, QuestionPolicy},
utils::{self, Bytes},
QuestionPolicy,
};

/// Unpacks the archive given by `archive` into the folder given by `into`.
Expand Down Expand Up @@ -64,7 +67,12 @@ where
builder.append_dir(path, path)?;
} else {
let mut file = fs::File::open(path)?;
builder.append_file(path, &mut file)?;
builder.append_file(path, file.file_mut()).map_err(|err| {
FinalError::with_title("Could not create archive")
.detail("Unexpected error while trying to read file")
.detail(format!("Error: {}.", err))
.into_owned()
})?;
}
}
env::set_current_dir(previous_location)?;
Expand Down
10 changes: 7 additions & 3 deletions src/archive/zip.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
//! Contains Zip-specific building and unpacking functions

use std::{
env, fs,
env,
io::{self, prelude::*},
path::{Path, PathBuf},
};

use fs_err as fs;

use walkdir::WalkDir;
use zip::{self, read::ZipFile, ZipArchive};

use crate::{
info,
utils::{self, dir_is_empty, strip_cur_dir, Bytes, QuestionPolicy},
utils::{self, dir_is_empty, strip_cur_dir, Bytes},
QuestionPolicy,
};

use self::utf8::get_invalid_utf8_paths;
Expand Down Expand Up @@ -127,10 +130,11 @@ fn check_for_comments(file: &ZipFile) {

#[cfg(unix)]
fn __unix_set_permissions(file_path: &Path, file: &ZipFile) -> crate::Result<()> {
use std::fs::Permissions;
use std::os::unix::fs::PermissionsExt;

if let Some(mode) = file.unix_mode() {
fs::set_permissions(file_path, fs::Permissions::from_mode(mode))?;
fs::set_permissions(file_path, Permissions::from_mode(mode))?;
}

Ok(())
Expand Down
52 changes: 4 additions & 48 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,10 @@ use std::{
vec::Vec,
};

use clap::{Parser, ValueHint};
use clap::Parser;
use fs_err as fs;

pub use crate::utils::QuestionPolicy;
use crate::Error;

/// Command line options
#[derive(Parser, Debug)]
#[clap(version, about)]
pub struct Opts {
/// Skip overwrite questions positively.
#[clap(short, long, conflicts_with = "no")]
pub yes: bool,

/// Skip overwrite questions negatively.
#[clap(short, long)]
pub no: bool,

/// Action to take
#[clap(subcommand)]
pub cmd: Subcommand,
}

/// Actions to take
#[derive(Parser, PartialEq, Eq, Debug)]
pub enum Subcommand {
/// Compress files. Alias: c
#[clap(alias = "c")]
Compress {
/// Files to be compressed
#[clap(required = true, min_values = 1)]
files: Vec<PathBuf>,

/// The resulting file. Its extensions specify how the files will be compressed and they need to be supported
#[clap(required = true, value_hint = ValueHint::FilePath)]
output: PathBuf,
},
/// Decompress files. Alias: d
#[clap(alias = "d")]
Decompress {
/// Files to be decompressed
#[clap(required = true, min_values = 1)]
files: Vec<PathBuf>,

/// Decompress files in a directory other than the current
#[clap(short, long, value_hint = ValueHint::DirPath)]
output: Option<PathBuf>,
},
}
use crate::{Error, Opts, QuestionPolicy, Subcommand};

impl Opts {
/// A helper method that calls `clap::Parser::parse` and then translates relative paths to absolute.
Expand All @@ -76,7 +32,7 @@ impl Opts {
}

fn canonicalize(path: impl AsRef<Path>) -> crate::Result<PathBuf> {
match std::fs::canonicalize(&path.as_ref()) {
match fs::canonicalize(&path.as_ref()) {
Ok(abs_path) => Ok(abs_path),
Err(io_err) => {
if !path.as_ref().exists() {
Expand Down
Loading

0 comments on commit 95d38e9

Please sign in to comment.