nuts_tool/cli/container/delete.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 nuts_tool_api::container_dir_for;
27use std::fs;
28
29use crate::cli::{open_container, prompt_yes_no};
30use crate::config::ContainerConfig;
31use crate::{say, say_warn};
32
33#[derive(Args, Debug)]
34pub struct ContainerDeleteArgs {
35 /// Specifies the name of the container
36 #[clap(short, long, env = "NUTS_CONTAINER")]
37 container: String,
38
39 /// Say yes, don't prompt for deletion
40 #[clap(short, long, action = ArgAction::SetTrue)]
41 yes: bool,
42
43 /// Enforces the deletion. Removes the container without connecting to it.
44 /// Note that depending on the backend, data may remain.
45 #[clap(short, long, action = ArgAction::SetTrue)]
46 force: bool,
47}
48
49impl ContainerDeleteArgs {
50 pub fn run(&self) -> Result<()> {
51 debug!("args: {:?}", self);
52
53 if !prompt_yes_no("Do you really want to delete the container?", self.yes)? {
54 say!("aborted");
55 return Ok(());
56 }
57
58 let path = container_dir_for(&self.container)?;
59 let mut container_config = ContainerConfig::load()?;
60
61 debug!("container: {}", self.container);
62 debug!("path: {}", path.display());
63
64 if !container_config.remove_plugin(&self.container) {
65 say_warn!("container {} not configured", self.container);
66 }
67
68 if !self.force {
69 let container = open_container(&self.container)?;
70 container.delete();
71 }
72
73 if path.exists() {
74 fs::remove_dir_all(path)?;
75 }
76
77 container_config.save()?;
78
79 Ok(())
80 }
81}