Skip to content

Commit

Permalink
Simplified handling of 'no paths given' case
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 1, 2019
1 parent a53c2ac commit ae0182f
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dua-cli"
version = "1.0.0"
version = "1.1.0"
authors = ["Sebastian Thiel <byronimo@gmail.com>"]
edition = "2018"
include = ["src/**/*", "Cargo.toml"]
Expand Down
28 changes: 25 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use structopt::StructOpt;
use dua::{ByteFormat, Color};
use failure::Error;
use failure_tools::ok_or_exit;
use std::{io, io::Write, path::PathBuf, process};
use std::path::PathBuf;
use std::{fs, io, io::Write, process};

mod options;

Expand All @@ -34,7 +35,17 @@ fn run() -> Result<(), Error> {
statistics,
}) => (
statistics,
dua::aggregate(stdout_locked, walk_options, !no_total, !no_sort, input)?,
dua::aggregate(
stdout_locked,
walk_options,
!no_total,
!no_sort,
if input.len() == 0 {
cwd_dirlist()?
} else {
input
},
)?,
),
None => (
false,
Expand All @@ -44,7 +55,7 @@ fn run() -> Result<(), Error> {
true,
true,
if opt.input.len() == 0 {
vec![PathBuf::from(".")]
cwd_dirlist()?
} else {
opt.input
},
Expand All @@ -61,6 +72,17 @@ fn run() -> Result<(), Error> {
Ok(())
}

fn cwd_dirlist() -> Result<Vec<PathBuf>, io::Error> {
let mut v: Vec<_> = fs::read_dir(".")?
.filter_map(|e| {
e.ok()
.and_then(|e| e.path().strip_prefix(".").ok().map(ToOwned::to_owned))
})
.collect();
v.sort();
Ok(v)
}

fn main() {
ok_or_exit(run())
}
4 changes: 2 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct Args {
#[structopt(short = "f", long = "format")]
pub format: Option<ByteFormat>,

/// One or more input files. If unset, we will assume the current directory
/// One or more input files. If unset, we will use all entries in the current working directory.
#[structopt(parse(from_os_str))]
pub input: Vec<PathBuf>,
}
Expand All @@ -60,7 +60,7 @@ pub enum Command {
/// If set, no total column will be computed for multiple inputs
#[structopt(long = "no-total")]
no_total: bool,
/// One or more input files. If unset, we will assume the current directory
/// One or more input files. If unset, we will use all entries in the current working directory.
#[structopt(parse(from_os_str))]
input: Vec<PathBuf>,
},
Expand Down
7 changes: 6 additions & 1 deletion tests/snapshots/success-bytes-binary
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
1.20 MiB .
0.00 B b.empty
256.00 B a
256.00 B c.lnk
666.00 B .hidden.666
1.20 MiB dir
1.20 MiB total
7 changes: 6 additions & 1 deletion tests/snapshots/success-bytes-only
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
1258947 b .
0 b b.empty
256 b a
256 b c.lnk
666 b .hidden.666
1258024 b dir
1259202 b total
7 changes: 6 additions & 1 deletion tests/snapshots/success-no-arguments
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
1.26 MB .
0.00 B b.empty
256.00 B a
256.00 B c.lnk
666.00 B .hidden.666
1.26 MB dir
1.26 MB total
6 changes: 6 additions & 0 deletions tests/snapshots/success-no-arguments-no-sort
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
666.00 B .hidden.666
256.00 B a
0.00 B b.empty
256.00 B c.lnk
1.26 MB dir
1.26 MB total
25 changes: 20 additions & 5 deletions tests/stateless-journey.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ WITH_FAILURE=1
cp -R "$fixtures/sample-01/" .
(with "no arguments"
(with "no given path"
it "produces a human-readable (metric) aggregate of the current directory, without total" && {
WITH_SNAPSHOT="$snapshot/success-no-arguments" \
expect_run ${SUCCESSFULLY} "$exe"
}
(with "no subcommand"
it "produces a human-readable (metric) aggregate of everything within the current directory, with total" && {
WITH_SNAPSHOT="$snapshot/success-no-arguments" \
expect_run ${SUCCESSFULLY} "$exe"
}
)
(with "the aggregate sub-command"
(with "no sorting option"
it "produces a human-readable (metric) aggregate of everything within the current directory, with total" && {
WITH_SNAPSHOT="$snapshot/success-no-arguments" \
expect_run ${SUCCESSFULLY} "$exe" aggregate
}
)
(with "sorting disabled"
it "produces a human-readable (metric) aggregate of everything within the current directory, alphabetically sorted, with total" && {
WITH_SNAPSHOT="$snapshot/success-no-arguments-no-sort" \
expect_run ${SUCCESSFULLY} "$exe" aggregate --no-sort
}
)
)
)
ls
(with "multiple given paths"
(when "specifying the 'aggregate' subcommand"
(with "no option to adjust the total"
Expand Down

0 comments on commit ae0182f

Please sign in to comment.