Skip to content

Commit

Permalink
Use exitcodes instead of exit method in tq binary entrypoint (#14)
Browse files Browse the repository at this point in the history
* Use `std::process::ExitCode` rather than `std::process::exit` in `tq` binary `main` function
* Reformat `serde` dependency in `Cargo.toml`
  • Loading branch information
vcfxb authored Aug 8, 2024
1 parent 17f22b1 commit 8c66294
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ toml = "0.8"
clap = { version = "4.5", features = ["derive", "usage", "help"] }
thiserror = "1.0.61"
colored = { version = "2.1.0", optional = true }
serde_json = { version = "1.0.120", features = [

[dependencies.serde_json]
version = "1.0.120"
features = [
"indexmap",
"preserve_order",
"raw_value",
"unbounded_depth",
], optional = true }
]
optional = true

[features]
default = ["json"]
color = ["colored"]
json = ["dep:serde_json"]


[lib]
name = "tq"
path = "src/lib.rs"
Expand All @@ -36,12 +39,10 @@ doctest = false

[[bin]]
name = "tq"
path = "src/main.rs"
test = false

[[bin]]
name = "tomlq"
path = "src/legacy.rs"
test = false

[package.metadata.binstall]
Expand Down
2 changes: 2 additions & 0 deletions src/legacy.rs → src/bin/tomlq.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Legacy tomlq binary.

use clap::Parser;
#[cfg(feature = "color")]
use colored::Colorize;
Expand Down
14 changes: 7 additions & 7 deletions src/main.rs → src/bin/tq.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
use clap::Parser;
use std::process::exit;
use std::process::ExitCode;
use tq::OutputType;

fn main() {
fn main() -> ExitCode {
let app = tq::Cli::parse();

let toml_file = tq::load_toml_from_file(&app.file);

let Ok(toml_file) = toml_file else {
let e = toml_file.unwrap_err();
eprintln!("{}", e);
exit(-1);
return ExitCode::FAILURE;
};

let x = tq::extract_pattern(&toml_file, &app.pattern);

exit(match x {
match x {
Ok(needle) => {
match app.output {
OutputType::Toml => println!("{}", format!("{}", needle).trim_matches('"')),
#[cfg(feature = "json")]
OutputType::Json => println!("{}", serde_json::to_string(&needle).unwrap()),
}

0
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("{}", e);
-1
ExitCode::FAILURE
}
});
}
}

0 comments on commit 8c66294

Please sign in to comment.