public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Max Carrara" <m.carrara@proxmox.com>
To: "Proxmox Backup Server development discussion"
	<pbs-devel@lists.proxmox.com>
Subject: Re: [pbs-devel] [PATCH vma-to-pbs 08/10] add a fallback for the --fingerprint argument
Date: Mon, 25 Mar 2024 13:35:05 +0100	[thread overview]
Message-ID: <D02U2KRN8I5T.HQOZ2U7OQ25M@proxmox.com> (raw)
In-Reply-To: <20240320141516.213930-9-f.schauer@proxmox.com>

On Wed Mar 20, 2024 at 3:15 PM CET, Filip Schauer wrote:
> Fallback to the PBS_FINGERPRINT environment variable if the
> --fingerprint argument is not specified.
>
> Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
> ---
>  Cargo.toml  | 2 +-
>  src/main.rs | 5 +++--
>  2 files changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/Cargo.toml b/Cargo.toml
> index 9711690..f56e351 100644
> --- a/Cargo.toml
> +++ b/Cargo.toml
> @@ -7,7 +7,7 @@ edition = "2021"
>  [dependencies]
>  anyhow = "1.0"
>  bincode = "1.3"
> -clap = { version = "4.0.32", features = ["cargo"] }
> +clap = { version = "4.0.32", features = ["cargo", "env"] }
>  md5 = "0.7.0"
>  scopeguard = "1.1.0"
>  serde = "1.0"
> diff --git a/src/main.rs b/src/main.rs
> index e0e1076..149c1a6 100644
> --- a/src/main.rs
> +++ b/src/main.rs
> @@ -27,7 +27,7 @@ fn main() -> Result<()> {
>                  .long("fingerprint")
>                  .value_name("FINGERPRINT")
>                  .help("Proxmox Backup Server Fingerprint")
> -                .required(true),
> +                .env("PBS_FINGERPRINT"),
>          )
>          .arg(
>              Arg::new("keyfile")
> @@ -72,9 +72,10 @@ fn main() -> Result<()> {
>  
>      let pbs_repository = matches.get_one::<String>("repository").unwrap().to_string();
>      let vmid = matches.get_one::<String>("vmid").unwrap().to_string();
> +
>      let fingerprint = matches
>          .get_one::<String>("fingerprint")
> -        .unwrap()
> +        .expect("Fingerprint not set. Use $PBS_FINGERPRINT or --fingerprint")
>          .to_string();

Something I hadn't mentioned in my last review for this patch in
particular (but for other parts in main.rs): Please avoid using
`unwrap()` and `expect()` in cases like these - there's no need to
`panic!` here.

Specifically, if the user forgets to provide the fingerprint, the error
message looks like this:

thread 'main' panicked at 'Fingerprint not set. Use $PBS_FINGERPRINT or --fingerprint', src/main.rs:78:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

... which ought not happen - you should only really `panic!` when an
invariant isn't being upheld.

Instead, use `anyhow::Context` [0] here with a `?`.

[0]: https://docs.rs/anyhow/1.0.79/anyhow/trait.Context.html

>  
>      let keyfile = matches.get_one::<String>("keyfile");





  reply	other threads:[~2024-03-25 12:35 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-20 14:15 [pbs-devel] [PATCH v5 vma-to-pbs 00/10] Implement vma-to-pbs tool Filip Schauer
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 01/10] Initial commit Filip Schauer
2024-03-25  9:42   ` Filip Schauer
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 02/10] cargo fmt Filip Schauer
2024-03-25  9:43   ` Filip Schauer
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 03/10] bump proxmox-backup-qemu Filip Schauer
2024-03-25  9:43   ` Filip Schauer
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 04/10] Add the ability to provide credentials via files Filip Schauer
2024-03-25 12:33   ` Max Carrara
2024-03-25 15:26     ` Thomas Lamprecht
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 05/10] bump proxmox-backup-qemu Filip Schauer
2024-03-25 12:33   ` Max Carrara
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 06/10] remove unnecessary "extern crate" declarations Filip Schauer
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 07/10] add support for streaming the VMA file via stdin Filip Schauer
2024-03-25 12:34   ` Max Carrara
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 08/10] add a fallback for the --fingerprint argument Filip Schauer
2024-03-25 12:35   ` Max Carrara [this message]
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 09/10] refactor error handling with anyhow Filip Schauer
2024-03-25 12:35   ` Max Carrara
2024-03-20 14:15 ` [pbs-devel] [PATCH vma-to-pbs 10/10] Makefile: remove reference to unused submodule Filip Schauer
2024-03-25 12:38 ` [pbs-devel] [PATCH v5 vma-to-pbs 00/10] Implement vma-to-pbs tool Max Carrara
2024-03-25 12:52   ` Wolfgang Bumiller
2024-04-03  9:56 ` Filip Schauer

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=D02U2KRN8I5T.HQOZ2U7OQ25M@proxmox.com \
    --to=m.carrara@proxmox.com \
    --cc=pbs-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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal