Skip to content

Commit

Permalink
update clap
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Nov 13, 2023
1 parent 02321cb commit 49e421b
Show file tree
Hide file tree
Showing 8 changed files with 572 additions and 202 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
os:
- ubuntu-latest
toolchain:
- 1.63
- "1.70"
target:
- x86_64-unknown-linux-gnu
- x86_64-unknown-linux-musl
Expand Down Expand Up @@ -100,7 +100,7 @@ jobs:
- macos-latest
- windows-latest
toolchain:
- 1.63
- "1.70"
features:
-
- --features json
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
os:
- ubuntu-latest
toolchain:
- 1.63
- "1.70"
target:
- x86_64-unknown-linux-gnu
- x86_64-unknown-linux-musl
Expand Down Expand Up @@ -116,7 +116,7 @@ jobs:
- macos-latest
- windows-latest
toolchain:
- 1.63
- "1.70"
features:
-
- --features json
Expand Down
17 changes: 11 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "wait-service"
version = "0.2.8"
version = "0.3.0"
authors = ["Magic Len <len@magiclen.org>"]
edition = "2021"
rust-version = "1.63"
rust-version = "1.70"
repository = "https://github.com/magiclen/wait-service"
homepage = "https://magiclen.org/wait-service"
keywords = ["wait-for-it", "tcp", "unix", "socket", "uds"]
Expand All @@ -19,19 +19,24 @@ panic = "abort"
strip = true

[dependencies]
clap = "3.2.23"
clap = { version = "4", features = ["derive"] }
concat-with = "0.2"
terminal_size = "0.2"
terminal_size = "0.3"

anyhow = "1"

once_cell = "1.9"
path-absolutize = { version = "3.0.11", features = ["once_cell_cache"] }

tokio = { version = "1.12", features = ["rt", "time", "net", "sync", "macros"] }

dnsclient = { version = "0.1.16", default-features = false, features = ["async-tokio"] }

serde_json = { version = "1", optional = true }
serde = { version = "1", features = ["derive"], optional = true }
serde_json = { version = "1", optional = true }

[dependencies.path-absolutize]
version = "3"
features = ["once_cell_cache"]

[features]
json = ["serde", "serde_json"]
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ Wait Service is a pure rust program to test and wait on the availability of mult

```
EXAMPLES:
wait-service --tcp localhost:27017 --tcp localhost:27018 -t 5 -- npm start # Wait for localhost:27017 and localhost:27018 (max 5 seconds) and then run `npm start`
wait-service --tcp localhost:27017 --uds /var/run/app.sock -t 0 -- npm start # Wait for localhost:27017 and /var/run/app.sock (forever) and then run `npm start`
wait-service --uds /var/run/app.sock --json /path/to/json -- npm start # Wait for /var/run/app.sock and other services defined in the json file (max 60 seconds) and then run `npm start`
USAGE:
wait-service [OPTIONS] <COMMAND>...
ARGS:
<COMMAND>... Command to execute after service is available
OPTIONS:
-t, --timeout <TIMEOUT> Set the timeout in seconds, zero for no timeout [default: 60]
--tcp <TCP>... Test and wait on the availability of TCP services
--uds <UDS>... Test and wait on the availability of UDS services [aliases: unix]
-h, --help Print help information
-V, --version Print version information
wait-service --tcp localhost:27017 --tcp localhost:27018 -t 5 -- npm start # Wait for localhost:27017 and localhost:27018 (max 5 seconds) and then run `npm start`
wait-service --tcp localhost:27017 --uds /var/run/app.sock -t 0 -- npm start # Wait for localhost:27017 and /var/run/app.sock (forever) and then run `npm start`
wait-service --uds /var/run/app.sock --json /path/to/json -- npm start # Wait for /var/run/app.sock and other services defined in the json file (max 60 seconds) and then run `npm start`
Usage: wait-service [OPTIONS] -- <COMMAND>...
Arguments:
<COMMAND>... Command to execute after service is available
Options:
-t, --timeout <TIMEOUT> Set the timeout in seconds, zero for no timeout [default: 60]
--tcp <TCP>... Test and wait on the availability of TCP services
--uds <UDS>... Test and wait on the availability of UDS services [aliases: unix]
--json <JSON>... Test and wait on the availability of TCP or UDS services
-h, --help Print help
-V, --version Print version
```

## The Config File
Expand Down
80 changes: 80 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#[cfg(any(unix, feature = "json"))]
use std::path::PathBuf;

use clap::{CommandFactory, FromArgMatches, Parser};
use concat_with::concat_line;
use terminal_size::terminal_size;

const APP_NAME: &str = "wait-service";
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
const CARGO_PKG_AUTHORS: &str = env!("CARGO_PKG_AUTHORS");

const AFTER_HELP: &str = "Enjoy it! https://magiclen.org";

const APP_ABOUT: &str = concat!(
"Wait Service is a pure rust program to test and wait on the availability of multiple \
services\n\nEXAMPLES:\n",
concat_line!(prefix "wait-service ",
"--tcp localhost:27017 --tcp localhost:27018 -t 5 -- npm start # Wait for localhost:27017 and localhost:27018 (max 5 seconds) and then run `npm start`",
"--tcp localhost:27017 --uds /var/run/app.sock -t 0 -- npm start # Wait for localhost:27017 and /var/run/app.sock (forever) and then run `npm start`",
"--uds /var/run/app.sock --json /path/to/json -- npm start # Wait for /var/run/app.sock and other services defined in the json file (max 60 seconds) and then run `npm start`",
)
);

#[derive(Debug, Parser)]
#[command(name = APP_NAME)]
#[command(term_width = terminal_size().map(|(width, _)| width.0 as usize).unwrap_or(0))]
#[command(version = CARGO_PKG_VERSION)]
#[command(author = CARGO_PKG_AUTHORS)]
#[command(after_help = AFTER_HELP)]
pub struct CLIArgs {
#[arg(short, long)]
#[arg(default_value = "60")]
#[arg(help = "Set the timeout in seconds, zero for no timeout")]
pub timeout: u64,

#[arg(required = true)]
#[arg(last = true)]
#[arg(value_hint = clap::ValueHint::CommandWithArguments)]
#[arg(help = "Command to execute after service is available")]
pub command: Vec<String>,

#[arg(long)]
#[arg(num_args = 1..)]
#[cfg_attr(unix, cfg_attr(feature = "json", arg(required_unless_present_any = ["uds", "json"], required_unless_present = "uds")), cfg_attr(feature = "json", arg(required_unless_present = "json")))]
#[arg(help = "Test and wait on the availability of TCP services")]
pub tcp: Vec<String>,

#[cfg(unix)]
#[arg(long, visible_alias = "unix")]
#[arg(num_args = 1..)]
#[cfg_attr(feature = "json", arg(required_unless_present_any = ["tcp", "json"]), arg(required_unless_present = "tcp"))]
#[arg(value_hint = clap::ValueHint::FilePath)]
#[arg(help = "Test and wait on the availability of UDS services")]
pub uds: Vec<PathBuf>,

#[cfg(feature = "json")]
#[arg(long)]
#[arg(num_args = 1..)]
#[cfg_attr(unix, arg(required_unless_present_any = ["tcp", "uds"]), arg(required_unless_present = "tcp"))]
#[arg(value_hint = clap::ValueHint::FilePath)]
#[arg(help = "Test and wait on the availability of TCP or UDS services")]
pub json: Vec<PathBuf>,
}

pub fn get_args() -> CLIArgs {
let args = CLIArgs::command();

let about = format!("{APP_NAME} {CARGO_PKG_VERSION}\n{CARGO_PKG_AUTHORS}\n{APP_ABOUT}");

let args = args.about(about);

let matches = args.get_matches();

match CLIArgs::from_arg_matches(&matches) {
Ok(args) => args,
Err(err) => {
err.exit();
},
}
}
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
//! # Wait Service
//! Wait Service is a pure rust program to test and wait on the availability of multiple services.
/*!
# Wait Service
Wait Service is a pure rust program to test and wait on the availability of multiple services.
*/
Loading

0 comments on commit 49e421b

Please sign in to comment.