Skip to content

Commit 25faee6

Browse files
committed
update clap
1 parent 02321cb commit 25faee6

File tree

7 files changed

+185
-203
lines changed

7 files changed

+185
-203
lines changed

.github/workflows/ci-version.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ jobs:
7070
os:
7171
- ubuntu-latest
7272
toolchain:
73-
- 1.63
73+
- "1.70"
7474
target:
7575
- x86_64-unknown-linux-gnu
7676
- x86_64-unknown-linux-musl
@@ -100,7 +100,7 @@ jobs:
100100
- macos-latest
101101
- windows-latest
102102
toolchain:
103-
- 1.63
103+
- "1.70"
104104
features:
105105
-
106106
- --features json

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
os:
8787
- ubuntu-latest
8888
toolchain:
89-
- 1.63
89+
- "1.70"
9090
target:
9191
- x86_64-unknown-linux-gnu
9292
- x86_64-unknown-linux-musl
@@ -116,7 +116,7 @@ jobs:
116116
- macos-latest
117117
- windows-latest
118118
toolchain:
119-
- 1.63
119+
- "1.70"
120120
features:
121121
-
122122
- --features json

Cargo.toml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "wait-service"
3-
version = "0.2.8"
3+
version = "0.3.0"
44
authors = ["Magic Len <len@magiclen.org>"]
55
edition = "2021"
6-
rust-version = "1.63"
6+
rust-version = "1.70"
77
repository = "https://github.com/magiclen/wait-service"
88
homepage = "https://magiclen.org/wait-service"
99
keywords = ["wait-for-it", "tcp", "unix", "socket", "uds"]
@@ -19,19 +19,24 @@ panic = "abort"
1919
strip = true
2020

2121
[dependencies]
22-
clap = "3.2.23"
22+
clap = { version = "4", features = ["derive"] }
2323
concat-with = "0.2"
24-
terminal_size = "0.2"
24+
terminal_size = "0.3"
25+
26+
anyhow = "1"
2527

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

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

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

33-
serde_json = { version = "1", optional = true }
3434
serde = { version = "1", features = ["derive"], optional = true }
35+
serde_json = { version = "1", optional = true }
36+
37+
[dependencies.path-absolutize]
38+
version = "3"
39+
features = ["once_cell_cache"]
3540

3641
[features]
3742
json = ["serde", "serde_json"]

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ Wait Service is a pure rust program to test and wait on the availability of mult
99

1010
```
1111
EXAMPLES:
12-
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`
13-
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`
14-
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`
15-
16-
USAGE:
17-
wait-service [OPTIONS] <COMMAND>...
18-
19-
ARGS:
20-
<COMMAND>... Command to execute after service is available
21-
22-
OPTIONS:
23-
-t, --timeout <TIMEOUT> Set the timeout in seconds, zero for no timeout [default: 60]
24-
--tcp <TCP>... Test and wait on the availability of TCP services
25-
--uds <UDS>... Test and wait on the availability of UDS services [aliases: unix]
26-
-h, --help Print help information
27-
-V, --version Print version information
12+
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`
13+
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`
14+
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`
15+
16+
Usage: wait-service [OPTIONS] -- <COMMAND>...
17+
18+
Arguments:
19+
<COMMAND>... Command to execute after service is available
20+
21+
Options:
22+
-t, --timeout <TIMEOUT> Set the timeout in seconds, zero for no timeout [default: 60]
23+
--tcp <TCP>... Test and wait on the availability of TCP services
24+
--uds <UDS>... Test and wait on the availability of UDS services [aliases: unix]
25+
--json <JSON>... Test and wait on the availability of TCP or UDS services
26+
-h, --help Print help
27+
-V, --version Print version
2828
```
2929

3030
## The Config File

src/cli.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#[cfg(any(unix, feature = "json"))]
2+
use std::path::PathBuf;
3+
4+
use clap::{CommandFactory, FromArgMatches, Parser};
5+
use concat_with::concat_line;
6+
use terminal_size::terminal_size;
7+
8+
const APP_NAME: &str = "wait-service";
9+
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
10+
const CARGO_PKG_AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
11+
12+
const AFTER_HELP: &str = "Enjoy it! https://magiclen.org";
13+
14+
const APP_ABOUT: &str = concat!(
15+
"Wait Service is a pure rust program to test and wait on the availability of multiple \
16+
services\n\nEXAMPLES:\n",
17+
concat_line!(prefix "wait-service ",
18+
"--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`",
19+
"--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`",
20+
"--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`",
21+
)
22+
);
23+
24+
#[derive(Debug, Parser)]
25+
#[command(name = APP_NAME)]
26+
#[command(term_width = terminal_size().map(|(width, _)| width.0 as usize).unwrap_or(0))]
27+
#[command(version = CARGO_PKG_VERSION)]
28+
#[command(author = CARGO_PKG_AUTHORS)]
29+
#[command(after_help = AFTER_HELP)]
30+
pub struct CLIArgs {
31+
#[arg(short, long)]
32+
#[arg(default_value = "60")]
33+
#[arg(help = "Set the timeout in seconds, zero for no timeout")]
34+
pub timeout: u64,
35+
36+
#[arg(required = true)]
37+
#[arg(last = true)]
38+
#[arg(value_hint = clap::ValueHint::CommandWithArguments)]
39+
#[arg(help = "Command to execute after service is available")]
40+
pub command: Vec<String>,
41+
42+
#[arg(long)]
43+
#[arg(num_args = 1..)]
44+
#[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")))]
45+
#[arg(help = "Test and wait on the availability of TCP services")]
46+
pub tcp: Vec<String>,
47+
48+
#[cfg(unix)]
49+
#[arg(long, visible_alias = "unix")]
50+
#[arg(num_args = 1..)]
51+
#[cfg_attr(feature = "json", arg(required_unless_present_any = ["tcp", "json"]), arg(required_unless_present = "tcp"))]
52+
#[arg(value_hint = clap::ValueHint::FilePath)]
53+
#[arg(help = "Test and wait on the availability of UDS services")]
54+
pub uds: Vec<PathBuf>,
55+
56+
#[cfg(feature = "json")]
57+
#[arg(long)]
58+
#[arg(num_args = 1..)]
59+
#[cfg_attr(unix, arg(required_unless_present_any = ["tcp", "uds"]), arg(required_unless_present = "tcp"))]
60+
#[arg(value_hint = clap::ValueHint::FilePath)]
61+
#[arg(help = "Test and wait on the availability of TCP or UDS services")]
62+
pub json: Vec<PathBuf>,
63+
}
64+
65+
pub fn get_args() -> CLIArgs {
66+
let args = CLIArgs::command();
67+
68+
let about = format!("{APP_NAME} {CARGO_PKG_VERSION}\n{CARGO_PKG_AUTHORS}\n{APP_ABOUT}");
69+
70+
let args = args.about(about);
71+
72+
let matches = args.get_matches();
73+
74+
match CLIArgs::from_arg_matches(&matches) {
75+
Ok(args) => args,
76+
Err(err) => {
77+
err.exit();
78+
},
79+
}
80+
}

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
//! # Wait Service
2-
//! Wait Service is a pure rust program to test and wait on the availability of multiple services.
1+
/*!
2+
# Wait Service
3+
4+
Wait Service is a pure rust program to test and wait on the availability of multiple services.
5+
*/

0 commit comments

Comments
 (0)