From c765759d1c6140c21f5e48ca2e8ff7afc428a20f Mon Sep 17 00:00:00 2001 From: Hendrik Maus Date: Tue, 29 Mar 2022 09:12:57 +0200 Subject: [PATCH 1/6] refactor: rename clap's entry point struct this prepares the code for the implementation of sub-commands --- release-operator/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/release-operator/src/main.rs b/release-operator/src/main.rs index baccd625e..ef551ce2e 100644 --- a/release-operator/src/main.rs +++ b/release-operator/src/main.rs @@ -8,7 +8,7 @@ use clap::Parser; #[derive(Parser, Debug)] #[clap(version)] -struct Cmd { +struct Cli { /// Commit sha to work on #[clap(short, long, env = "GITHUB_SHA")] sha: String, @@ -28,10 +28,10 @@ fn main() -> anyhow::Result<()> { let start = std::time::Instant::now(); log::trace!("starting release-operator process"); - let args = Cmd::parse(); + let cli = Cli::parse(); log::debug!("got arguments: {args:#?}"); - Release::new(args.sha, args.label).detect()?; + Release::new(cli.sha, cli.label).detect()?; log::trace!( "finished release-operator process, took {:?}", From cfd1ac9690a769151d347f52f62f46f115783c07 Mon Sep 17 00:00:00 2001 From: Hendrik Maus Date: Tue, 29 Mar 2022 09:30:38 +0200 Subject: [PATCH 2/6] feat: move release detection into sub-command --- .github/workflows/cd.yml | 2 +- Cargo.lock | 2 +- release-operator/Cargo.toml | 2 +- release-operator/README.md | 4 ++-- release-operator/src/main.rs | 26 +++++++++++++++++++++----- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index c80ce8cf5..9db3545e9 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -104,7 +104,7 @@ jobs: RUST_LOG: info run: | # Run release operator - cargo run + cargo run -- detect - name: Binaries | Download if: ${{ steps.release.outputs.release-detected == 'true' }} diff --git a/Cargo.lock b/Cargo.lock index 310377c92..c46e11930 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1892,7 +1892,7 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" [[package]] name = "release-operator" -version = "0.1.0" +version = "0.2.0" dependencies = [ "anyhow", "clap", diff --git a/release-operator/Cargo.toml b/release-operator/Cargo.toml index c34653cb0..6c258a789 100644 --- a/release-operator/Cargo.toml +++ b/release-operator/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "release-operator" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/release-operator/README.md b/release-operator/README.md index 0f8335d3d..87e883efb 100644 --- a/release-operator/README.md +++ b/release-operator/README.md @@ -54,7 +54,7 @@ As seen above, the release operator requires the maintainer to: ```shell # release-operator/ -cargo run -- --sha --label +cargo run -- detect --sha --label ``` Where `` can be set using `GITHUB_SHA` (present by default in GitHub Actions), and `` can be set using `RELEASE_LABEL` (defaults to `autorelease`). @@ -94,7 +94,7 @@ To embed the operator in a workflow, include these steps _before_ anything relat RELEASE_LABEL: autorelease run: | # release operator - cargo run + cargo run -- detect # Subsequent steps can use: # if: ${{ steps.release.outputs.release-detected == 'true' }} diff --git a/release-operator/src/main.rs b/release-operator/src/main.rs index ef551ce2e..71ad0d1b4 100644 --- a/release-operator/src/main.rs +++ b/release-operator/src/main.rs @@ -4,11 +4,23 @@ mod release; use crate::github::{Actions, GitHub}; use crate::release::Release; -use clap::Parser; +use clap::{Args, Parser, Subcommand}; -#[derive(Parser, Debug)] -#[clap(version)] +#[derive(Parser)] +#[clap(version, propagate_version = true)] struct Cli { + #[clap(subcommand)] + command: Commands, +} + +#[derive(Subcommand)] +enum Commands { + /// Detect a release and set respective outputs + Detect(DetectArgs), +} + +#[derive(Args, Debug)] +struct DetectArgs { /// Commit sha to work on #[clap(short, long, env = "GITHUB_SHA")] sha: String, @@ -29,9 +41,13 @@ fn main() -> anyhow::Result<()> { log::trace!("starting release-operator process"); let cli = Cli::parse(); - log::debug!("got arguments: {args:#?}"); - Release::new(cli.sha, cli.label).detect()?; + match &cli.command { + Commands::Detect(opts) => { + log::debug!("got arguments: {opts:#?}"); + Release::new(opts.sha.to_owned(), opts.label.to_owned()).detect()?; + } + } log::trace!( "finished release-operator process, took {:?}", From 991f19da5bef6c5d0bceb17ed1d8d9abc61c3d0c Mon Sep 17 00:00:00 2001 From: Hendrik Maus Date: Tue, 29 Mar 2022 09:34:12 +0200 Subject: [PATCH 3/6] docs: rephrase command description --- release-operator/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release-operator/src/main.rs b/release-operator/src/main.rs index 71ad0d1b4..477621da0 100644 --- a/release-operator/src/main.rs +++ b/release-operator/src/main.rs @@ -15,7 +15,7 @@ struct Cli { #[derive(Subcommand)] enum Commands { - /// Detect a release and set respective outputs + /// Detect a release and sets respective Action outputs Detect(DetectArgs), } From 89a51bdf4f6a4c0dad35c6e5b478c78799a3a059 Mon Sep 17 00:00:00 2001 From: Hendrik Maus Date: Tue, 29 Mar 2022 09:34:55 +0200 Subject: [PATCH 4/6] refactor: call input values arguments (args) the trait on the respective struct is also called Args --- release-operator/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/release-operator/src/main.rs b/release-operator/src/main.rs index 477621da0..18da2c73c 100644 --- a/release-operator/src/main.rs +++ b/release-operator/src/main.rs @@ -43,9 +43,9 @@ fn main() -> anyhow::Result<()> { let cli = Cli::parse(); match &cli.command { - Commands::Detect(opts) => { - log::debug!("got arguments: {opts:#?}"); - Release::new(opts.sha.to_owned(), opts.label.to_owned()).detect()?; + Commands::Detect(args) => { + log::debug!("got arguments: {args:#?}"); + Release::new(args.sha.to_owned(), args.label.to_owned()).detect()?; } } From 87ca80c3d62099a8804d62f28bdaf49687b1d17c Mon Sep 17 00:00:00 2001 From: Hendrik Maus Date: Tue, 29 Mar 2022 09:39:10 +0200 Subject: [PATCH 5/6] docs: fix command comment --- release-operator/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release-operator/src/main.rs b/release-operator/src/main.rs index 18da2c73c..c96ae5268 100644 --- a/release-operator/src/main.rs +++ b/release-operator/src/main.rs @@ -15,7 +15,7 @@ struct Cli { #[derive(Subcommand)] enum Commands { - /// Detect a release and sets respective Action outputs + /// Detect a release and set respective Action outputs Detect(DetectArgs), } From cdbd6dcddd2f528c079315c01a2aa03fd9645695 Mon Sep 17 00:00:00 2001 From: Hendrik Maus Date: Tue, 29 Mar 2022 09:55:05 +0200 Subject: [PATCH 6/6] refactor: run cargo fmt --- release-operator/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/release-operator/src/main.rs b/release-operator/src/main.rs index c96ae5268..2041969a6 100644 --- a/release-operator/src/main.rs +++ b/release-operator/src/main.rs @@ -45,7 +45,8 @@ fn main() -> anyhow::Result<()> { match &cli.command { Commands::Detect(args) => { log::debug!("got arguments: {args:#?}"); - Release::new(args.sha.to_owned(), args.label.to_owned()).detect()?; + Release::new(args.sha.to_owned(), args.label.to_owned()) + .detect()?; } }