Skip to content

Commit

Permalink
Up-lift generator command to top-level
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX committed Dec 9, 2023
1 parent ae34e49 commit 6de8240
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 155 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ cargo +nightly install ftzz
Generate a reproducibly random tree in the current directory with *approximately* 1 million files:

```console
$ ftzz g ./simple -n 1M
$ ftzz ./simple -n 1M
About 1,000,000 files will be generated in approximately 1,000 directories distributed across a tree of maximum depth 5 where each directory contains approximately 4 other directories.
Created 1,002,953 files across 1,193 directories.

Expand All @@ -31,7 +31,7 @@ Created 1,002,953 files across 1,193 directories.
Generate *exactly* 1 million files:

```console
$ ftzz g ./exact -en 1M
$ ftzz ./exact -en 1M
Exactly 1,000,000 files will be generated in approximately 1,000 directories distributed across a tree of maximum depth 5 where each directory contains approximately 4 other directories.
Created 1,000,000 files across 1,193 directories.

Expand All @@ -40,7 +40,7 @@ Created 1,000,000 files across 1,193 directories.
Generate ~10_000 files with ~1 MB of random data spread across them:

```console
$ ftzz g ./with_data -n 10K -b 1M
$ ftzz ./with_data -n 10K -b 1M
About 10,000 files will be generated in approximately 1,000 directories distributed across a tree of maximum depth 5 where each directory contains approximately 4 other directories. Each file will contain approximately 100 bytes of random data.
Created 9,419 files (929.4 KB) across 1,138 directories.

Expand All @@ -51,11 +51,11 @@ structure given the same inputs. To generate variations on a structure with the
change the starting seed:

```console
$ ftzz g ./unseeded -n 100
$ ftzz ./unseeded -n 100
About 100 files will be generated in approximately 100 directories distributed across a tree of maximum depth 5 where each directory contains approximately 3 other directories.
Created 32 files across 214 directories.

$ ftzz g ./seeded -n 100 --seed 42 # Or $RANDOM
$ ftzz ./seeded -n 100 --seed 42 # Or $RANDOM
About 100 files will be generated in approximately 100 directories distributed across a tree of maximum depth 5 where each directory contains approximately 3 other directories.
Created 78 files across 59 directories.

Expand All @@ -64,7 +64,7 @@ Created 78 files across 59 directories.
Other parameters can be found in the built-in docs:

```console
$ ftzz help generate
$ ftzz --help
Generate a random directory hierarchy with some number of files

A pseudo-random directory hierarchy will be generated (seeded by this command's input parameters)
Expand All @@ -78,7 +78,7 @@ named `n` and directories are named `n.dir` for a given natural number `n`.
By default, generated files are empty, but random data can be used as the file contents with the
`total-bytes` option.

Usage: ftzz[EXE] generate [OPTIONS] --files <NUM_FILES> <ROOT_DIR>
Usage: ftzz[EXE] [OPTIONS] --files <NUM_FILES> <ROOT_DIR>

Arguments:
<ROOT_DIR>
Expand Down Expand Up @@ -142,4 +142,7 @@ Options:
-v, --verbose...
More output per occurrence

-V, --version
Print version

```
41 changes: 2 additions & 39 deletions command-reference-short.golden
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
A random file and directory generator

Usage: ftzz [OPTIONS] <COMMAND>

Commands:
generate Generate a random directory hierarchy with some number of files
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help (use `--help` for more detail)
-q, --quiet... Less output per occurrence
-v, --verbose... More output per occurrence
-V, --version Print version

---

Generate a random directory hierarchy with some number of files

Usage: ftzz generate [OPTIONS] --files <NUM_FILES> <ROOT_DIR>
Usage: ftzz [OPTIONS] --files <NUM_FILES> <ROOT_DIR>

Arguments:
<ROOT_DIR> The directory in which to generate files
Expand All @@ -37,25 +21,4 @@ Options:
-h, --help Print help (use `--help` for more detail)
-q, --quiet... Less output per occurrence
-v, --verbose... More output per occurrence

---

Print this message or the help of the given subcommand(s)

Usage: ftzz help [COMMAND]

Commands:
generate Generate a random directory hierarchy with some number of files
help Print this message or the help of the given subcommand(s)

---

Generate a random directory hierarchy with some number of files

Usage: ftzz help generate

---

Print this message or the help of the given subcommand(s)

Usage: ftzz help help
-V, --version Print version
48 changes: 3 additions & 45 deletions command-reference.golden
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
A random file and directory generator

Usage: ftzz [OPTIONS] <COMMAND>

Commands:
generate Generate a random directory hierarchy with some number of files
help Print this message or the help of the given subcommand(s)

Options:
-h, --help
Print help (use `-h` for a summary)

-q, --quiet...
Less output per occurrence

-v, --verbose...
More output per occurrence

-V, --version
Print version

---

Generate a random directory hierarchy with some number of files

A pseudo-random directory hierarchy will be generated (seeded by this command's input parameters)
Expand All @@ -34,7 +11,7 @@ named `n` and directories are named `n.dir` for a given natural number `n`.
By default, generated files are empty, but random data can be used as the file contents with the
`total-bytes` option.

Usage: ftzz generate [OPTIONS] --files <NUM_FILES> <ROOT_DIR>
Usage: ftzz [OPTIONS] --files <NUM_FILES> <ROOT_DIR>

Arguments:
<ROOT_DIR>
Expand Down Expand Up @@ -98,24 +75,5 @@ Options:
-v, --verbose...
More output per occurrence

---

Print this message or the help of the given subcommand(s)

Usage: ftzz help [COMMAND]

Commands:
generate Generate a random directory hierarchy with some number of files
help Print this message or the help of the given subcommand(s)

---

Generate a random directory hierarchy with some number of files

Usage: ftzz help generate

---

Print this message or the help of the given subcommand(s)

Usage: ftzz help help
-V, --version
Print version
52 changes: 22 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,37 @@ use std::{
process::{ExitCode, Termination},
};

use clap::{builder::ArgPredicate, ArgAction, Args, Parser, Subcommand, ValueHint};
use clap::{builder::ArgPredicate, ArgAction, Args, Parser, ValueHint};
use clap_num::si_number;
use clap_verbosity_flag::Verbosity;
use error_stack::ResultExt;
use ftzz::{Generator, NumFilesWithRatio, NumFilesWithRatioError};
use io_adapters::WriteExtension;
use paste::paste;

/// A random file and directory generator
/// Generate a random directory hierarchy with some number of files
///
/// A pseudo-random directory hierarchy will be generated (seeded by this
/// command's input parameters) containing approximately the target
/// number of files. The exact configuration of files and directories in
/// the hierarchy is probabilistically determined to mostly match the
/// specified parameters.
///
/// Generated files and directories are named using monotonically increasing
/// numbers, where files are named `n` and directories are named `n.dir`
/// for a given natural number `n`.
///
/// By default, generated files are empty, but random data can be used as
/// the file contents with the `total-bytes` option.
#[derive(Parser, Debug)]
#[command(version, author = "Alex Saveau (@SUPERCILEX)")]
#[command(infer_subcommands = true, infer_long_args = true)]
#[command(disable_help_flag = true)]
#[command(max_term_width = 100)]
#[cfg_attr(test, command(help_expected = true))]
struct Ftzz {
#[command(subcommand)]
cmd: Cmd,
#[command(flatten)]
options: Generate,

#[command(flatten)]
#[command(next_display_order = None)]
Expand All @@ -38,25 +51,6 @@ struct Ftzz {
help: Option<bool>,
}

#[derive(Subcommand, Debug)]
enum Cmd {
/// Generate a random directory hierarchy with some number of files
///
/// A pseudo-random directory hierarchy will be generated (seeded by this
/// command's input parameters) containing approximately the target
/// number of files. The exact configuration of files and directories in
/// the hierarchy is probabilistically determined to mostly match the
/// specified parameters.
///
/// Generated files and directories are named using monotonically increasing
/// numbers, where files are named `n` and directories are named `n.dir`
/// for a given natural number `n`.
///
/// By default, generated files are empty, but random data can be used as
/// the file contents with the `total-bytes` option.
Generate(Generate),
}

#[derive(Args, Debug)]
#[command(arg_required_else_help = true)]
struct Generate {
Expand Down Expand Up @@ -240,18 +234,16 @@ fn main() -> ExitCode {

fn ftzz(
Ftzz {
cmd,
options,
verbose: _,
help: _,
}: Ftzz,
) -> error_stack::Result<(), CliError> {
let mut stdout = stdout();
match cmd {
Cmd::Generate(options) => Generator::try_from(options)
.change_context(CliError::InvalidArgs)?
.generate(&mut stdout.write_adapter())
.change_context(CliError::Generator),
}
Generator::try_from(options)
.change_context(CliError::InvalidArgs)?
.generate(&mut stdout.write_adapter())
.change_context(CliError::Generator)
}

fn num_files_parser(s: &str) -> Result<NonZeroU64, String> {
Expand Down
Loading

0 comments on commit 6de8240

Please sign in to comment.