From 3c7c862ad814d2f46c3dfb8f795ad5d92121c1c8 Mon Sep 17 00:00:00 2001 From: Bert JW Regeer Date: Sun, 2 Oct 2022 14:56:56 -0600 Subject: [PATCH] Add support for exiting with an exit code This allows barnacle to be used as an init container or some other process that simply requires barnacle to render the configuration file but not execute another process. It is mutually exclusive with providing a command to barnacle --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cli.rs | 6 +++++- src/main.rs | 6 ++++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9765442..ec35ffd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,7 +21,7 @@ dependencies = [ [[package]] name = "barnacle" -version = "0.2.0" +version = "0.3.0" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index d9fa4a3..6cbe783 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "barnacle" -version = "0.2.0" +version = "0.3.0" edition = "2021" authors = ["Bert JW Regeer "] license = "ISC" diff --git a/src/cli.rs b/src/cli.rs index 7f9a309..86a0f68 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -13,9 +13,13 @@ pub struct Cli { pub output: PathBuf, /// The command + arguments to execute once template is rendered - #[arg(required(true))] + #[arg(group = "command_or_exit", required(true))] pub command: Vec, + /// Exit with error code, exclusive with a command to exeute + #[arg(short, long, group = "command_or_exit", required(true))] + pub exit: Option, + /// Turn debugging information on #[arg(short, long, action = clap::ArgAction::Count)] pub verbose: u8, diff --git a/src/main.rs b/src/main.rs index d85741e..850a291 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,5 +40,11 @@ fn main() { fs::write(&matches.output, output).expect("Failed to write output file"); + // If we have an exit code, use it + if let Some(exit) = matches.exit { + std::process::exit(exit.into()); + } + + // Otherwise we execute the command provided on the CLI exec(&matches.command); }