nuts_tool/cli/archive/migrate.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, Result};
24use clap::{ArgAction, Args};
25use log::debug;
26use nuts_container::LATEST_REVISION;
27use std::cmp::Ordering;
28
29use crate::cli::archive::open_archive;
30use crate::cli::error::ExitOnly;
31use crate::cli::prompt_yes_no;
32use crate::say;
33
34#[derive(Args, Debug)]
35pub struct ArchiveMigrateArgs {
36 /// Instead of executing the migration, simply check whether one is
37 /// necessary. The exit code is 1 if a migration can be carried out,
38 /// otherwise 0
39 #[clap(long, action = ArgAction::SetTrue)]
40 verify: bool,
41
42 /// Say yes, don't prompt for migration
43 #[clap(short, long, action = ArgAction::SetTrue)]
44 yes: bool,
45
46 /// Specifies the name of the container
47 #[clap(short, long, env = "NUTS_CONTAINER")]
48 container: String,
49}
50
51impl ArchiveMigrateArgs {
52 pub fn run(&self) -> Result<()> {
53 debug!("args: {:?}", self);
54
55 let archive = open_archive(&self.container, false)?;
56 let info = archive.as_ref().info()?;
57
58 match info.revision.cmp(&LATEST_REVISION) {
59 Ordering::Equal => {
60 say!(
61 "container revision: {}, no migration necessary",
62 info.revision
63 );
64
65 Ok(())
66 }
67 Ordering::Less => {
68 say!(
69 "container revision: {}, migration required to revision {}",
70 info.revision,
71 LATEST_REVISION
72 );
73
74 if self.verify {
75 Err(ExitOnly::new(1).into())
76 } else if prompt_yes_no("Do you really want to start the migration?", self.yes)? {
77 open_archive(&self.container, true).map(|_| ())
78 } else {
79 say!("aborted");
80 Ok(())
81 }
82 }
83 Ordering::Greater => Err(anyhow!(
84 "invalid container revision {}, cannot be greater than {}",
85 info.revision,
86 LATEST_REVISION
87 )),
88 }
89 }
90}