nuts_tool/cli/container/list.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::{ArgAction, Args};
25use log::debug;
26use std::os::fd::RawFd;
27use std::path::PathBuf;
28
29use crate::config::{ContainerConfig, PluginConfig};
30use crate::{say, say_warn};
31
32#[derive(Args, Debug)]
33pub struct ContainerListArgs {
34 /// Display all container (even with broken configuration)
35 #[clap(short, long, action = ArgAction::SetTrue)]
36 all: bool,
37
38 #[clap(long, hide = true)]
39 password_from_fd: Option<RawFd>,
40
41 #[clap(long, hide = true)]
42 password_from_file: Option<PathBuf>,
43}
44
45impl ContainerListArgs {
46 pub fn run(&self) -> Result<()> {
47 debug!("all: {}", self.all);
48
49 let container_config = ContainerConfig::load()?;
50 let plugin_config = PluginConfig::load()?;
51
52 for name in container_config.list_container() {
53 let ok = container_config
54 .get_plugin(name)
55 .map(|p| plugin_config.have_plugin(p))
56 .unwrap_or(false);
57
58 if ok && self.all {
59 say!(" {}", name);
60 } else if ok && !self.all {
61 say!("{}", name);
62 } else if self.all {
63 say_warn!("! {}", name);
64 }
65 }
66
67 Ok(())
68 }
69}