nuts_tool/cli/container/info.rs
1// MIT License
2//
3// Copyright (c) 2023,2024 Robin Doer
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to
7// deal in the Software without restriction, including without limitation the
8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22
23use anyhow::Result;
24use clap::Args;
25use log::debug;
26use std::cmp;
27
28use crate::cli::open_container;
29use crate::config::ContainerConfig;
30use crate::format::Format;
31use crate::say;
32
33#[derive(Args, Debug)]
34pub struct ContainerInfoArgs {
35 /// Specifies the format of the userdata dump
36 #[clap(short, long, value_parser, default_value = "raw")]
37 format: Format,
38
39 /// Specifies the name of the container
40 #[clap(short, long, env = "NUTS_CONTAINER")]
41 container: String,
42}
43
44impl ContainerInfoArgs {
45 pub fn run(&self) -> Result<()> {
46 debug!("args: {:?}", self);
47
48 let container = open_container(&self.container)?;
49 let container_config = ContainerConfig::load()?;
50 let plugin = container_config.get_plugin(&self.container).unwrap_or("?");
51 let info = container.info()?;
52
53 let key_width = 19;
54 let key_width = info
55 .backend
56 .iter()
57 .fold(key_width, |acc, (key, _)| cmp::max(acc, key.len() + 1));
58
59 say!("{:<key_width$} {}", "plugin:", plugin);
60 say!("{:<key_width$} {}", "revision:", info.revision);
61 say!("{:<key_width$} {}", "cipher:", info.cipher);
62 say!("{:<key_width$} {}", "kdf:", info.kdf.to_string());
63 say!("{:<key_width$} {}", "block size (gross):", info.bsize_gross);
64 say!("{:<key_width$} {}", "block size (net):", info.bsize_net);
65
66 say!("");
67
68 for (key, value) in info.backend {
69 say!("{:<key_width$} {}", format!("{}:", key), value);
70 }
71
72 Ok(())
73 }
74}