Skip to content

Commit f01c298

Browse files
committed
add initial version of 'lean-cli' feature toggle, but…
…nested subcommands are not allowed. Maybe this tells me that I want two binaries, one for plumbing, one for everything else.
1 parent f558ce5 commit f01c298

File tree

5 files changed

+152
-51
lines changed

5 files changed

+152
-51
lines changed

Diff for: Cargo.lock

+30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ doctest = false
1717
default = ["fast", "pretty-cli"]
1818
fast = ["git-features/parallel", "git-features/fast-sha1"]
1919
pretty-cli = ["structopt"]
20+
lean-cli = ["argh"]
2021

2122
[dependencies]
23+
anyhow = "1.0.31"
24+
2225
gitoxide-core = { version = "0.1.0", path = "gitoxide-core" }
2326
git-features = { version = "0.1.0", path = "git-features" }
27+
2428
structopt = { version = "0.3.14", optional = true }
25-
anyhow = "1.0.31"
29+
argh = { version = "0.1.3", optional = true }
2630

2731
[profile.release]
2832
overflow-checks = false

Diff for: Makefile

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interactive-developer-environment-in-docker: ## Use docker for all dependencies
1414
##@ Development
1515

1616
target/debug/gio: always
17-
cargo build
17+
cargo build --no-default-features --features pretty-cli
1818

1919
target/release/gio: always
2020
cargo build --release
@@ -29,7 +29,7 @@ profile: target/release/gio ## run callgrind and annotate its output - linux onl
2929
benchmark: target/release/gio ## see how fast things are, powered by hyperfine
3030
hyperfine '$<'
3131

32-
tests: check unit-tests journey-tests ## run all tests, including journey tests
32+
tests: check unit-tests journey-tests journey-tests-lean-cli ## run all tests, including journey tests
3333

3434
check: ## Build all code in suitable configurations
3535
cargo check --all
@@ -43,9 +43,13 @@ unit-tests: ## run all unit tests
4343
continuous-unit-tests: ## run all unit tests whenever something changes
4444
watchexec -w src $(MAKE) unit-tests
4545

46-
journey-tests: target/debug/gio ## run stateless journey tests
46+
journey-tests: target/debug/gio ## run stateless journey tests (pretty-cli)
4747
./tests/stateless-journey.sh $<
4848

49+
journey-tests-lean-cli: always ## run stateless journey tests (lean-cli)
50+
cargo build --no-default-features --features lean-cli
51+
./tests/stateless-journey.sh target/debug/gio
52+
4953
continuous-journey-tests: ## run stateless journey tests whenever something changes
5054
watchexec $(MAKE) journey-tests
5155

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ The top-level command-line interface.
163163
* If disabled, the binary will be visibly smaller.
164164
* **pretty-cli** _(default)_
165165
* Use `clap` + `structopt` to build the prettiest, best documented and most user-friendly CLI at the expense of file size.
166-
* **small-cli** _(mutually exclusive to pretty-cli)_
166+
* **lean-cli** _(mutually exclusive to pretty-cli)_
167167
* Use `argh` to produce a usable binary with decent documentation that is smallest in size.
168168
* If `pretty-cli` is enabled as well, `small-cli` will take precedence, and you pay for building unnecessary dependencies.
169169

Diff for: src/main.rs

+109-46
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,124 @@
11
#![forbid(unsafe_code)]
22

3-
use anyhow::Result;
4-
use gitoxide_core as core;
5-
use std::io::{stderr, stdout};
6-
use structopt::StructOpt;
7-
8-
mod options {
9-
use std::path::PathBuf;
10-
use structopt::clap::AppSettings;
3+
#[cfg(feature = "pretty-cli")]
4+
mod pretty {
5+
use anyhow::Result;
6+
use gitoxide_core as core;
7+
use std::io::{stderr, stdout};
118
use structopt::StructOpt;
129

13-
#[derive(Debug, StructOpt)]
14-
#[structopt(about = "The git, simply swift")]
15-
#[structopt(settings = &[AppSettings::SubcommandRequired,
10+
mod options {
11+
use std::path::PathBuf;
12+
use structopt::clap::AppSettings;
13+
use structopt::StructOpt;
14+
15+
#[derive(Debug, StructOpt)]
16+
#[structopt(about = "The git, simply swift")]
17+
#[structopt(settings = &[AppSettings::SubcommandRequired,
1618
AppSettings::ColoredHelp])]
17-
pub struct Args {
18-
#[structopt(subcommand)]
19-
pub cmd: Subcommands,
19+
pub struct Args {
20+
#[structopt(subcommand)]
21+
pub cmd: Subcommands,
22+
}
23+
24+
/// Low-level commands that are not used every day
25+
#[derive(Debug, StructOpt)]
26+
pub enum Plumbing {
27+
/// Verify the integrity of a pack or index file
28+
#[structopt(setting = AppSettings::ColoredHelp)]
29+
VerifyPack {
30+
/// The '.pack' or '.idx' file whose checksum to validate.
31+
#[structopt(parse(from_os_str))]
32+
path: PathBuf,
33+
},
34+
}
35+
36+
#[derive(Debug, StructOpt)]
37+
pub enum Subcommands {
38+
/// Initialize the repository in the current directory.
39+
#[structopt(alias = "initialize")]
40+
#[structopt(setting = AppSettings::ColoredHelp)]
41+
Init,
42+
43+
#[structopt(alias = "p")]
44+
#[structopt(setting = AppSettings::ColoredHelp)]
45+
Plumbing(Plumbing),
46+
}
2047
}
2148

22-
/// Low-level commands that are not used every day
23-
#[derive(Debug, StructOpt)]
24-
pub enum Plumbing {
25-
/// Verify the integrity of a pack or index file
26-
#[structopt(setting = AppSettings::ColoredHelp)]
27-
VerifyPack {
28-
/// The '.pack' file whose checksum to validate.
29-
///
30-
/// '.pack' files have a 20 byte trailer representing the Sha1 over all the bytes that
31-
/// preceded it.
32-
#[structopt(parse(from_os_str))]
33-
path: PathBuf,
34-
},
49+
pub fn main() -> Result<()> {
50+
use options::*;
51+
let args = Args::from_args();
52+
match args.cmd {
53+
Subcommands::Init => core::init(),
54+
Subcommands::Plumbing(cmd) => match cmd {
55+
Plumbing::VerifyPack { path } => {
56+
core::verify_pack_or_pack_index(path, stdout(), stderr())
57+
}
58+
},
59+
}?;
60+
Ok(())
3561
}
62+
}
3663

37-
#[derive(Debug, StructOpt)]
38-
pub enum Subcommands {
39-
/// Initialize the repository in the current directory.
40-
#[structopt(alias = "initialize")]
41-
#[structopt(setting = AppSettings::ColoredHelp)]
42-
Init,
64+
#[cfg(all(feature = "lean-cli", not(feature = "pretty-cli")))]
65+
mod lean {
66+
use argh::FromArgs;
4367

44-
#[structopt(alias = "p")]
45-
#[structopt(setting = AppSettings::ColoredHelp)]
46-
Plumbing(Plumbing),
68+
#[derive(FromArgs)]
69+
/// A simple calculation tool
70+
struct Args {
71+
#[argh(subcommand)]
72+
subcommand: SubCommands,
4773
}
48-
}
4974

50-
fn main() -> Result<()> {
51-
let args = options::Args::from_args();
52-
match args.cmd {
53-
options::Subcommands::Init => core::init(),
54-
options::Subcommands::Plumbing(cmd) => match cmd {
55-
options::Plumbing::VerifyPack { path } => {
75+
#[derive(FromArgs, PartialEq, Debug)]
76+
#[argh(subcommand)]
77+
enum SubCommands {
78+
Init(Init),
79+
VerifyPack(VerifyPack),
80+
}
81+
82+
/// Initialize the repository in the current directory.
83+
#[derive(FromArgs, PartialEq, Debug)]
84+
#[argh(subcommand, name = "init")]
85+
struct Init {}
86+
87+
/// Initialize the repository in the current directory.
88+
#[derive(FromArgs, PartialEq, Debug)]
89+
#[argh(subcommand, name = "verify-pack")]
90+
struct VerifyPack {
91+
/// the '.pack' or '.idx' file whose checksum to validate.
92+
#[argh(option)]
93+
path: PathBuf,
94+
}
95+
96+
use anyhow::Result;
97+
use gitoxide_core as core;
98+
use std::{
99+
io::{stderr, stdout},
100+
path::PathBuf,
101+
};
102+
103+
pub fn main() -> Result<()> {
104+
let cli: Args = argh::from_env();
105+
match cli.subcommand {
106+
SubCommands::Init(_) => core::init(),
107+
SubCommands::VerifyPack(VerifyPack { path }) => {
56108
core::verify_pack_or_pack_index(path, stdout(), stderr())
57109
}
58-
},
59-
}?;
60-
Ok(())
110+
}
111+
}
112+
}
113+
114+
use anyhow::Result;
115+
116+
#[cfg(feature = "pretty-cli")]
117+
fn main() -> Result<()> {
118+
pretty::main()
119+
}
120+
121+
#[cfg(all(feature = "lean-cli", not(feature = "pretty-cli")))]
122+
fn main() -> Result<()> {
123+
lean::main()
61124
}

0 commit comments

Comments
 (0)