From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer v2 2/4] fetch-answer: refactor cli argument parsing
Date: Fri, 8 Nov 2024 14:05:34 +0100 [thread overview]
Message-ID: <20241108130537.1266472-3-c.heiss@proxmox.com> (raw)
In-Reply-To: <20241108130537.1266472-1-c.heiss@proxmox.com>
Clean up the actual parsing a bit to make it more easily extensible, and
add a proper help menu.
Tested-By: Aaron Lauterer <a.lauterer@proxmox.com>
Reviewed-By: Aaron Lauterer <a.lauterer@proxmox.com>
Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
Changes v1 -> v2:
* no changes
proxmox-fetch-answer/src/main.rs | 40 +++++++++++++++++++++++++-------
1 file changed, 31 insertions(+), 9 deletions(-)
diff --git a/proxmox-fetch-answer/src/main.rs b/proxmox-fetch-answer/src/main.rs
index 86c3270..753ab29 100644
--- a/proxmox-fetch-answer/src/main.rs
+++ b/proxmox-fetch-answer/src/main.rs
@@ -16,6 +16,22 @@ mod fetch_plugins;
static LOGGER: AutoInstLogger = AutoInstLogger;
static AUTOINST_MODE_FILE: &str = "/cdrom/auto-installer-mode.toml";
+const CLI_USAGE_HELPTEXT: &str = concat!(
+ "Usage: ",
+ env!("CARGO_BIN_NAME"),
+ " <command> <additional parameters..>
+
+Commands:
+ iso Fetch the builtin answer file from the ISO
+ http Fetch the answer file via HTTP(S)
+ Additional parameters: [<http-url>] [<tls-cert-fingerprint>]
+ partition Fetch the answer file from a mountable partition
+
+Options:
+ -h, --help Print this help menu
+"
+);
+
pub fn init_log() -> Result<()> {
AutoInstLogger::init("/tmp/fetch_answer.log")?;
log::set_logger(&LOGGER)
@@ -46,21 +62,27 @@ fn fetch_answer(install_settings: &AutoInstSettings) -> Result<String> {
}
fn settings_from_cli_args(args: &[String]) -> Result<AutoInstSettings> {
- // TODO: this was done in a bit of a hurry, needs tidying up
let mode = match args[1].to_lowercase().as_str() {
"iso" => FetchAnswerFrom::Iso,
"http" => FetchAnswerFrom::Http,
"partition" => FetchAnswerFrom::Partition,
- "-h" | "--help" => bail!(
- "usage: {} <http|iso|partition> [<http-url>] [<tls-cert-fingerprint>]",
- args[0]
- ),
+ "-h" | "--help" => {
+ eprintln!("{}", CLI_USAGE_HELPTEXT);
+ bail!("invalid usage");
+ }
_ => bail!("failed to parse fetch-from argument, not one of 'http', 'iso', or 'partition'"),
};
- if args.len() > 4 {
- } else if args.len() > 2 && mode != FetchAnswerFrom::Http {
- bail!("only 'http' fetch-from mode supports additional url and cert-fingerprint mode");
- }
+
+ match mode {
+ FetchAnswerFrom::Iso | FetchAnswerFrom::Partition if args.len() > 2 => {
+ bail!("'iso' and 'partition' modes do not take any additional arguments")
+ }
+ FetchAnswerFrom::Http if args.len() > 4 => {
+ bail!("'http' mode takes at most 2 additional arguments")
+ }
+ _ => {}
+ };
+
Ok(AutoInstSettings {
mode,
partition_label: "proxmox-ais".to_owned(),
--
2.47.0
_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
next prev parent reply other threads:[~2024-11-08 13:06 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-08 13:05 [pve-devel] [PATCH installer v2 0/4] auto-install-assistant: allow specifying 'fetch-from' partition label Christoph Heiss
2024-11-08 13:05 ` [pve-devel] [PATCH installer v2 1/4] auto-install-assistant: add new parameter to specify " Christoph Heiss
2024-11-08 13:05 ` Christoph Heiss [this message]
2024-11-08 13:05 ` [pve-devel] [PATCH installer v2 3/4] fetch-answer: partition: also search for exact-matching " Christoph Heiss
2024-11-08 13:05 ` [pve-devel] [PATCH installer v2 4/4] fetch-answer: use partition label from fetch config instead of hardcoded Christoph Heiss
2024-11-10 18:23 ` [pve-devel] applied-series: [PATCH installer v2 0/4] auto-install-assistant: allow specifying 'fetch-from' partition label Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241108130537.1266472-3-c.heiss@proxmox.com \
--to=c.heiss@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox