nuts_tool/cli/container/
attach.rs

1// MIT License
2//
3// Copyright (c) 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::{anyhow, ensure, Result};
24use clap::{ArgAction, Args};
25use log::debug;
26use std::os::fd::RawFd;
27use std::path::PathBuf;
28
29use crate::config::{ContainerConfig, PluginConfig};
30
31#[derive(Args, Debug)]
32pub struct ContainerAttachArgs {
33    /// Specifies the name of the container
34    #[clap(short, long, env = "NUTS_CONTAINER")]
35    container: String,
36
37    /// Attaches PLUGIN to CONTAINER
38    plugin: String,
39
40    /// Enforce the operation, even if a plugin is already attached to the
41    /// container
42    #[clap(short, long, action = ArgAction::SetTrue)]
43    force: bool,
44
45    #[clap(long, hide = true)]
46    password_from_fd: Option<RawFd>,
47
48    #[clap(long, hide = true)]
49    password_from_file: Option<PathBuf>,
50}
51
52impl ContainerAttachArgs {
53    pub fn run(&self) -> Result<()> {
54        debug!("container: {}", self.container);
55        debug!("plugin: {}", self.plugin);
56        debug!("force: {}", self.force);
57
58        let mut container_config = ContainerConfig::load()?;
59        let plugin_config = PluginConfig::load()?;
60
61        ensure!(
62            plugin_config.have_plugin(&self.plugin),
63            "no such plugin: {}",
64            self.plugin
65        );
66
67        if !container_config.add_plugin(&self.container, &self.plugin, self.force) {
68            return Err(anyhow!(
69                "you already have a container with the name {}",
70                self.container,
71            ));
72        }
73
74        container_config.save()
75    }
76}