nuts_tool/cli/
archive.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
23pub mod add;
24pub mod create;
25pub mod get;
26pub mod info;
27pub mod list;
28pub mod migrate;
29
30use anyhow::Result;
31use clap::{Args, Subcommand};
32use nuts_archive::{Archive, ArchiveFactory};
33use nuts_container::Container;
34
35use crate::backend::PluginBackend;
36use crate::cli::archive::add::ArchiveAddArgs;
37use crate::cli::archive::create::ArchiveCreateArgs;
38use crate::cli::archive::get::ArchiveGetArgs;
39use crate::cli::archive::info::ArchiveInfoArgs;
40use crate::cli::archive::list::ArchiveListArgs;
41use crate::cli::archive::migrate::ArchiveMigrateArgs;
42use crate::cli::open_container;
43
44#[derive(Debug, Args)]
45#[clap(args_conflicts_with_subcommands = true, subcommand_required = true)]
46pub struct ArchiveArgs {
47    #[clap(subcommand)]
48    command: Option<ArchiveCommand>,
49}
50
51impl ArchiveArgs {
52    pub fn run(&self) -> Result<()> {
53        self.command
54            .as_ref()
55            .map_or(Ok(()), |command| command.run())
56    }
57}
58
59#[derive(Debug, Subcommand)]
60pub enum ArchiveCommand {
61    /// Adds a new entry at the end of the archive
62    Add(ArchiveAddArgs),
63
64    /// Creates a new archive
65    Create(ArchiveCreateArgs),
66
67    /// Retrieve the content of an entry
68    Get(ArchiveGetArgs),
69
70    /// Prints information about the archive
71    Info(ArchiveInfoArgs),
72
73    /// Lists the content of the archive
74    List(ArchiveListArgs),
75
76    /// Performs migration tasks
77    Migrate(ArchiveMigrateArgs),
78}
79
80impl ArchiveCommand {
81    pub fn run(&self) -> Result<()> {
82        match self {
83            Self::Add(args) => args.run(),
84            Self::Create(args) => args.run(),
85            Self::Get(args) => args.run(),
86            Self::Info(args) => args.run(),
87            Self::List(args) => args.run(),
88            Self::Migrate(args) => args.run(),
89        }
90    }
91}
92
93fn open_archive(name: &str, migrate: bool) -> Result<Archive<PluginBackend>> {
94    let container = open_container(name)?;
95
96    Container::open_service::<ArchiveFactory>(container, migrate).map_err(|err| err.into())
97}