Skip to content

Commit bd001d2

Browse files
committed
refactor: migrate to clap2man and consolidate CLI metadata in src/cli.rs
1 parent 4ad9880 commit bd001d2

7 files changed

Lines changed: 85 additions & 73 deletions

File tree

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ tracing = { version = "0.1.44", features = ["log"] }
3232
url = "2.5.8"
3333

3434
[build-dependencies]
35+
clap = { version = "4.6.0", features = ["derive"] }
36+
clap2man = "0.1.1"
3537
color-eyre = "0.6.3"
3638
man = "0.3.0"
3739

build.rs

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,33 @@
22
// This file is subject to the terms and conditions defined in
33
// file 'LICENSE', which is part of this source code package.
44

5-
use color_eyre::{Result, eyre::eyre};
6-
use man::prelude::*;
5+
use clap::CommandFactory;
6+
use color_eyre::{eyre::eyre, eyre::Context, Result};
77
use std::env;
88
use std::fs::{self, File};
99
use std::io::Write;
1010
use std::path;
1111

12+
include!("src/cli.rs");
13+
1214
fn generate_man_page<P: AsRef<path::Path>>(outdir: P) -> Result<()> {
1315
let outdir = outdir.as_ref();
1416
let man_path = outdir.join("github-workflows-update.1");
15-
let manpage = Manual::new("github-workflows-update")
16-
.about("Check github workflows for actions that can be updated")
17-
.author(Author::new("Leandro Lisboa Penz").email("[email protected]"))
18-
.flag(
19-
Flag::new()
20-
.short("-n")
21-
.long("--dry-run")
22-
.help("Don't update the workflows, just print what would be done"),
23-
)
24-
.option(
25-
Opt::new("output-format")
26-
.short("-f")
27-
.long("--output-format")
28-
.default_value("standard")
29-
.help(
30-
"Output format for the outdated action messages; \
31-
one of \"standard\" or \"github-warning\"",
32-
),
33-
)
34-
.flag(
35-
Flag::new()
36-
.long("--error-on-outdated")
37-
.help("Return error if any outdated actions are found"),
38-
)
39-
.flag(
40-
Flag::new()
41-
.short("-h")
42-
.long("--help")
43-
.help("Prints help information"),
44-
)
45-
.flag(
46-
Flag::new()
47-
.short("-V")
48-
.long("--version")
49-
.help("Prints version information"),
50-
)
51-
.arg(Arg::new("COMMAND"))
52-
.arg(Arg::new("[ ARGS ]"))
53-
.description(
54-
"github-workflows-update reads all github workflow and checks the latest
55-
available versions of all github actions and workflow dispatches used, showing
56-
which ones can be updated and optionally updating them automatically.",
57-
)
17+
let cmd = Cli::command();
18+
let manual: man::Manual = clap2man::Manual::try_from(&cmd)
19+
.wrap_err("error converting clap command to manual")?
20+
.into();
21+
let manpage = manual
5822
.example(
59-
Example::new()
23+
man::prelude::Example::new()
6024
.text(
6125
"Update all actions used in all github workflows \
6226
under the current repository",
6327
)
6428
.command("github-workflows-update"),
6529
)
6630
.example(
67-
Example::new()
31+
man::prelude::Example::new()
6832
.text("Show outdated actions without updating them")
6933
.command("github-workflows-update -n"),
7034
)

src/cli.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (C) 2022 Leandro Lisboa Penz <[email protected]>
2+
// This file is subject to the terms and conditions defined in
3+
// file 'LICENSE', which is part of this source code package.
4+
5+
use clap::Parser;
6+
use clap::ValueEnum;
7+
8+
#[derive(Parser, Debug)]
9+
#[command(
10+
author,
11+
version,
12+
about = "Check github workflows for actions that can be updated",
13+
long_about = "github-workflows-update reads all github workflow and checks the latest
14+
available versions of all github actions and workflow dispatches used, showing
15+
which ones can be updated and optionally updating them automatically."
16+
)]
17+
pub struct Cli {
18+
/// Don't update the workflows, just print what would be done
19+
#[clap(short = 'n', long = "dry-run")]
20+
pub dryrun: bool,
21+
/// Output format for the outdated action messages
22+
#[clap(short = 'f', long, value_enum, value_parser)]
23+
pub output_format: Option<OutputFormat>,
24+
/// Return error if any outdated actions are found
25+
#[clap(long)]
26+
pub error_on_outdated: bool,
27+
}
28+
29+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Default, Debug)]
30+
pub enum OutputFormat {
31+
#[default]
32+
Standard,
33+
/// Generate messages as github action warnings
34+
GithubWarning,
35+
}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::*;
40+
use clap::CommandFactory;
41+
42+
#[test]
43+
fn test_cli() {
44+
Cli::command().debug_assert();
45+
}
46+
47+
#[test]
48+
fn test_parse_args() {
49+
let args =
50+
Cli::parse_from(&["test", "-n", "-f", "github-warning", "--error-on-outdated"]);
51+
assert!(args.dryrun);
52+
assert_eq!(args.output_format, Some(OutputFormat::GithubWarning));
53+
assert!(args.error_on_outdated);
54+
}
55+
}

src/cmd.rs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,13 @@ use tracing::Level;
1212
use tracing::event;
1313

1414
use clap::Parser;
15-
use clap::ValueEnum;
16-
15+
use crate::cli::Cli;
16+
use crate::cli::OutputFormat;
1717
use crate::proxy;
1818

19-
#[derive(Parser, Debug)]
20-
#[command(author, version, about, long_about = None)]
21-
pub struct Args {
22-
/// Don't update the workflows, just print what would be done
23-
#[clap(short = 'n', long = "dry-run")]
24-
pub dryrun: bool,
25-
/// Output format for the outdated action messages
26-
#[clap(short = 'f', long, value_enum, value_parser)]
27-
pub output_format: Option<OutputFormat>,
28-
/// Return error if any outdated actions are found
29-
#[clap(long)]
30-
pub error_on_outdated: bool,
31-
}
32-
33-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Default, Debug)]
34-
pub enum OutputFormat {
35-
#[default]
36-
Standard,
37-
/// Generate messages as github action warnings
38-
GithubWarning,
39-
}
40-
4119
#[tokio::main]
4220
pub async fn main() -> Result<()> {
43-
let args = Args::parse();
21+
let args = Cli::parse();
4422
env_logger::init();
4523
let proxy_server = proxy::Server::new();
4624
let futures = ReadDirStream::new(tokio::fs::read_dir(".github/workflows").await?)

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! - [`proxy`]: a proxy [`proxy::Server`] that makes async
2020
//! requests and caches the results.
2121
22+
pub mod cli;
2223
pub mod cmd;
2324
pub mod error;
2425
pub mod processor;

src/processor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use tracing::Level;
1010
use tracing::event;
1111
use tracing::instrument;
1212

13-
use crate::cmd::OutputFormat;
13+
use crate::cli::OutputFormat;
1414
use crate::proxy;
1515
use crate::workflow::Workflow;
1616

0 commit comments

Comments
 (0)