From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id CFF751FF164 for ; Fri, 8 Nov 2024 14:06:21 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 3CA86DFAE; Fri, 8 Nov 2024 14:06:19 +0100 (CET) From: Christoph Heiss To: pve-devel@lists.proxmox.com Date: Fri, 8 Nov 2024 14:05:34 +0100 Message-ID: <20241108130537.1266472-3-c.heiss@proxmox.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241108130537.1266472-1-c.heiss@proxmox.com> References: <20241108130537.1266472-1-c.heiss@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.030 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH installer v2 2/4] fetch-answer: refactor cli argument parsing X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" Clean up the actual parsing a bit to make it more easily extensible, and add a proper help menu. Tested-By: Aaron Lauterer Reviewed-By: Aaron Lauterer Signed-off-by: Christoph Heiss --- 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"), + " + +Commands: + iso Fetch the builtin answer file from the ISO + http Fetch the answer file via HTTP(S) + Additional parameters: [] [] + 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 { } fn settings_from_cli_args(args: &[String]) -> Result { - // 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: {} [] []", - 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