1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
// MIT License
//
// Copyright (c) 2023 Robin Doer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
use anyhow::Result;
use clap::{Args, Subcommand};
use log::debug;
use nuts_archive::Archive;
use std::io::{self, Read};
use std::path::PathBuf;
use crate::archive::append_recursive;
use crate::cli::open_container;
#[derive(Args, Debug)]
// #[clap(group(ArgGroup::new("input").required(true).multiple(false)))]
#[clap(args_conflicts_with_subcommands = true)]
pub struct ArchiveAddArgs {
#[clap(subcommand)]
command: Option<ArchiveAddCommand>,
/// Path to files/directories to be added to the archive. If PATHS contains
/// a directory all entries in the directory are also appended. If no PATHS
/// are specified an empty archive is created.
paths: Vec<PathBuf>,
/// Specifies the name of the container
#[clap(short, long, env = "NUTS_CONTAINER")]
container: String,
}
impl ArchiveAddArgs {
pub fn run(&self) -> Result<()> {
if let Some(command) = self.command.as_ref() {
return command.run();
}
debug!("container: {}", self.container);
let container = open_container(&self.container)?;
let mut archive = Archive::open(container)?;
for path in self.paths.iter() {
append_recursive(&mut archive, path)?;
}
Ok(())
}
}
#[derive(Debug, Subcommand)]
pub enum ArchiveAddCommand {
/// Appends a custom file to the archive.
File(ArchiveAddFileArgs),
/// Appends a custom directory to the archive.
Directory(ArchiveAddDirectoryArgs),
/// Appends a custom symlink to the archive.
Symlink(ArchiveAddSymlinkArgs),
}
impl ArchiveAddCommand {
pub fn run(&self) -> Result<()> {
match self {
Self::File(args) => args.run(),
Self::Directory(args) => args.run(),
Self::Symlink(args) => args.run(),
}
}
}
#[derive(Args, Debug)]
pub struct ArchiveAddFileArgs {
/// Name of the file.
name: String,
/// Specifies the name of the container
#[clap(short, long, env = "NUTS_CONTAINER")]
container: String,
}
impl ArchiveAddFileArgs {
pub fn run(&self) -> Result<()> {
debug!("container: {}", self.container);
debug!("name: {}", self.name);
let container = open_container(&self.container)?;
let mut archive = Archive::open(container)?;
let block_size = archive.as_ref().block_size() as usize;
let mut entry = archive.append_file(&self.name).build()?;
let mut buf = vec![0; block_size];
loop {
let n = io::stdin().read(&mut buf)?;
debug!("{} bytes read from stdin", n);
if n > 0 {
entry.write_all(&buf[..n])?;
} else {
break;
}
}
Ok(())
}
}
#[derive(Args, Debug)]
pub struct ArchiveAddDirectoryArgs {
/// Name of the directory.
name: String,
/// Specifies the name of the container
#[clap(short, long, env = "NUTS_CONTAINER")]
container: String,
}
impl ArchiveAddDirectoryArgs {
pub fn run(&self) -> Result<()> {
debug!("container: {}", self.container);
debug!("name: {}", self.name);
let container = open_container(&self.container)?;
let mut archive = Archive::open(container)?;
archive.append_directory(&self.name).build()?;
Ok(())
}
}
#[derive(Args, Debug)]
pub struct ArchiveAddSymlinkArgs {
/// Name of the symlink.
name: String,
/// Target of the symlink.
target: String,
/// Specifies the name of the container
#[clap(short, long, env = "NUTS_CONTAINER")]
container: String,
}
impl ArchiveAddSymlinkArgs {
pub fn run(&self) -> Result<()> {
debug!("container: {}", self.container);
debug!("name: {}", self.name);
debug!("target: {}", self.target);
let container = open_container(&self.container)?;
let mut archive = Archive::open(container)?;
archive.append_symlink(&self.name, &self.target).build()?;
Ok(())
}
}