Skip to content

Commit

Permalink
Adapt nuts-tool to api changes of nuts-container
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Doer committed Aug 28, 2024
1 parent 6003629 commit faed79f
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 72 deletions.
10 changes: 8 additions & 2 deletions nuts-tool/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use anyhow::Result;
use clap::{crate_version, ArgAction, Parser, Subcommand};
use env_logger::Builder;
use log::LevelFilter;
use nuts_container::{Container, OpenOptionsBuilder};
use nuts_container::{Container, OpenOptions, OpenOptionsBuilder};
use nuts_tool_api::tool::Plugin;
use rpassword::prompt_password;

Expand Down Expand Up @@ -94,7 +94,7 @@ impl Commands {
}
}

fn open_container(name: &str, verbose: u8) -> Result<Container<PluginBackend>> {
fn open_builder(name: &str, verbose: u8) -> Result<(PluginBackendOpenBuilder, OpenOptions)> {
let container_config = ContainerConfig::load()?;
let plugin_config = PluginConfig::load()?;

Expand All @@ -109,6 +109,12 @@ fn open_container(name: &str, verbose: u8) -> Result<Container<PluginBackend>> {
let builder = OpenOptionsBuilder::new().with_password_callback(ask_for_password);
let options = builder.build::<PluginBackend>()?;

Ok((plugin_builder, options))
}

fn open_container(name: &str, verbose: u8) -> Result<Container<PluginBackend>> {
let (plugin_builder, options) = open_builder(name, verbose)?;

Container::open(plugin_builder, options).map_err(|err| err.into())
}

Expand Down
12 changes: 11 additions & 1 deletion nuts-tool/src/cli/archive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright (c) 2023 Robin Doer
// Copyright (c) 2023,2024 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
Expand Down Expand Up @@ -28,12 +28,16 @@ pub mod list;

use anyhow::Result;
use clap::{Args, Subcommand};
use nuts_archive::{Archive, ArchiveFactory};
use nuts_container::Container;

use crate::backend::PluginBackend;
use crate::cli::archive::add::ArchiveAddArgs;
use crate::cli::archive::create::ArchiveCreateArgs;
use crate::cli::archive::get::ArchiveGetArgs;
use crate::cli::archive::info::ArchiveInfoArgs;
use crate::cli::archive::list::ArchiveListArgs;
use crate::cli::open_builder;

#[derive(Debug, Args)]
#[clap(args_conflicts_with_subcommands = true, subcommand_required = true)]
Expand Down Expand Up @@ -79,3 +83,9 @@ impl ArchiveCommand {
}
}
}

fn open_archive(name: &str, verbose: u8) -> Result<Archive<PluginBackend>> {
let (plugin_builder, options) = open_builder(name, verbose)?;

Container::open_service::<_, ArchiveFactory>(plugin_builder, options).map_err(|err| err.into())
}
6 changes: 2 additions & 4 deletions nuts-tool/src/cli/archive/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ use chrono::{DateTime, Local, NaiveDateTime, TimeZone, Utc};
use clap::builder::{TypedValueParser, ValueParserFactory};
use clap::{value_parser, Arg, Args, Command, Subcommand};
use log::debug;
use nuts_archive::Archive;
use std::ffi::OsStr;
use std::path::PathBuf;

use crate::archive::append_recursive;
use crate::cli::archive::add::dir::ArchiveAddDirectoryArgs;
use crate::cli::archive::add::file::ArchiveAddFileArgs;
use crate::cli::archive::add::symlink::ArchiveAddSymlinkArgs;
use crate::cli::open_container;
use crate::cli::archive::open_archive;

const TSTAMP_HELP: &str = "\x1B[1m\x1B[4mTimestamps:\x1B[0m
Expand Down Expand Up @@ -159,8 +158,7 @@ impl ArchiveAddArgs {

debug!("args: {:?}", self);

let container = open_container(&self.container, self.verbose)?;
let mut archive = Archive::open(container)?;
let mut archive = open_archive(&self.container, self.verbose)?;

for path in self.paths.iter() {
append_recursive(&mut archive, path)?;
Expand Down
7 changes: 2 additions & 5 deletions nuts-tool/src/cli/archive/add/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
use anyhow::Result;
use clap::Args;
use log::debug;
use nuts_archive::Archive;

use crate::cli::archive::add::{TimestampArgs, TSTAMP_HELP};
use crate::cli::open_container;
use crate::cli::archive::open_archive;

#[derive(Args, Debug)]
#[clap(after_help(TSTAMP_HELP))]
Expand All @@ -49,9 +48,7 @@ impl ArchiveAddDirectoryArgs {
pub fn run(&self) -> Result<()> {
debug!("args: {:?}", self);

let container = open_container(&self.container, self.verbose)?;
let mut archive = Archive::open(container)?;

let mut archive = open_archive(&self.container, self.verbose)?;
let mut builder = archive.append_directory(&self.name);

if let Some(created) = self.timestamps.created {
Expand Down
7 changes: 2 additions & 5 deletions nuts-tool/src/cli/archive/add/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
use anyhow::Result;
use clap::Args;
use log::debug;
use nuts_archive::Archive;
use std::io::{self, Read};

use crate::cli::archive::add::{TimestampArgs, TSTAMP_HELP};
use crate::cli::open_container;
use crate::cli::archive::open_archive;

#[derive(Args, Debug)]
#[clap(after_help(TSTAMP_HELP))]
Expand All @@ -50,10 +49,8 @@ impl ArchiveAddFileArgs {
pub fn run(&self) -> Result<()> {
debug!("args: {:?}", self);

let container = open_container(&self.container, self.verbose)?;
let mut archive = Archive::open(container)?;
let mut archive = open_archive(&self.container, self.verbose)?;
let block_size = archive.as_ref().block_size() as usize;

let mut builder = archive.append_file(&self.name);

if let Some(created) = self.timestamps.created {
Expand Down
7 changes: 2 additions & 5 deletions nuts-tool/src/cli/archive/add/symlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
use anyhow::Result;
use clap::Args;
use log::debug;
use nuts_archive::Archive;

use crate::cli::archive::add::{TimestampArgs, TSTAMP_HELP};
use crate::cli::open_container;
use crate::cli::archive::open_archive;

#[derive(Args, Debug)]
#[clap(after_help(TSTAMP_HELP))]
Expand All @@ -52,9 +51,7 @@ impl ArchiveAddSymlinkArgs {
pub fn run(&self) -> Result<()> {
debug!("args: {:?}", self);

let container = open_container(&self.container, self.verbose)?;
let mut archive = Archive::open(container)?;

let mut archive = open_archive(&self.container, self.verbose)?;
let mut builder = archive.append_symlink(&self.name, &self.target);

if let Some(created) = self.timestamps.created {
Expand Down
6 changes: 2 additions & 4 deletions nuts-tool/src/cli/archive/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
use anyhow::Result;
use clap::{ArgAction, Args};
use log::debug;
use nuts_archive::Archive;
use std::path::PathBuf;

use crate::archive::append_recursive;
use crate::cli::open_container;
use crate::cli::archive::open_archive;

#[derive(Args, Debug)]
pub struct ArchiveCreateArgs {
Expand All @@ -53,8 +52,7 @@ impl ArchiveCreateArgs {
pub fn run(&self) -> Result<()> {
debug!("args: {:?}", self);

let container = open_container(&self.container, self.verbose)?;
let mut archive = Archive::create(container, self.force)?;
let mut archive = open_archive(&self.container, self.verbose)?;

for path in self.paths.iter() {
append_recursive(&mut archive, path)?;
Expand Down
7 changes: 2 additions & 5 deletions nuts-tool/src/cli/archive/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
use anyhow::{anyhow, Result};
use clap::Args;
use log::{debug, trace};
use nuts_archive::Archive;

use crate::cli::open_container;
use crate::cli::archive::open_archive;
use crate::format::Format;
use crate::say::is_quiet;

Expand All @@ -50,9 +49,7 @@ impl ArchiveGetArgs {
pub fn run(&self) -> Result<()> {
debug!("args: {:?}", self);

let container = open_container(&self.container, self.verbose)?;
let mut archive = Archive::open(container)?;

let mut archive = open_archive(&self.container, self.verbose)?;
let block_size = archive.as_ref().block_size() as usize;

let entry = match archive.lookup(&self.name) {
Expand Down
6 changes: 2 additions & 4 deletions nuts-tool/src/cli/archive/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
use anyhow::Result;
use clap::Args;
use log::debug;
use nuts_archive::Archive;

use crate::cli::open_container;
use crate::cli::archive::open_archive;
use crate::say;
use crate::time::TimeFormat;

Expand Down Expand Up @@ -53,8 +52,7 @@ impl ArchiveInfoArgs {
pub fn run(&self) -> Result<()> {
debug!("args: {:?}", self);

let container = open_container(&self.container, self.verbose)?;
let archive = Archive::open(container)?;
let archive = open_archive(&self.container, self.verbose)?;
let info = archive.info();

let created = self.time_format.format(&info.created, "%c");
Expand Down
7 changes: 3 additions & 4 deletions nuts-tool/src/cli/archive/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
use anyhow::Result;
use clap::{ArgAction, Args};
use log::debug;
use nuts_archive::{Archive, Entry, Group};
use nuts_archive::{Entry, Group};
use std::cmp;
use std::fmt::{self, Write};

use crate::backend::PluginBackend;
use crate::cli::open_container;
use crate::cli::archive::open_archive;
use crate::say;
use crate::time::TimeFormat;

Expand Down Expand Up @@ -169,8 +169,7 @@ impl ArchiveListArgs {
pub fn run(&self) -> Result<()> {
debug!("args: {:?}", self);

let container = open_container(&self.container, self.verbose)?;
let mut archive = Archive::open(container)?;
let mut archive = open_archive(&self.container, self.verbose)?;

let mut entry_opt = archive.first();
let mut ctx_opt = None;
Expand Down
40 changes: 7 additions & 33 deletions nuts-tool/src/cli/container/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,15 @@
use anyhow::Result;
use clap::Args;
use log::debug;
use nuts_container::Container;
use std::cmp;

use crate::backend::PluginBackend;
use crate::cli::open_container;
use crate::config::ContainerConfig;
use crate::format::Format;
use crate::say;
use crate::say::is_quiet;

#[derive(Args, Debug)]
pub struct ContainerInfoArgs {
/// If set, displays the userdata of the container
#[clap(short, long)]
userdata: bool,

/// Specifies the format of the userdata dump
#[clap(short, long, value_parser, default_value = "raw")]
format: Format,
Expand All @@ -52,7 +45,10 @@ pub struct ContainerInfoArgs {
}

impl ContainerInfoArgs {
fn print_info(&self, container: &Container<PluginBackend>) -> Result<()> {
pub fn run(&self) -> Result<()> {
debug!("args: {:?}", self);

let container = open_container(&self.container, self.verbose)?;
let container_config = ContainerConfig::load()?;
let plugin = container_config.get_plugin(&self.container).unwrap_or("?");
let info = container.info()?;
Expand All @@ -63,8 +59,9 @@ impl ContainerInfoArgs {
.iter()
.fold(key_width, |acc, (key, _)| cmp::max(acc, key.len() + 1));

say!("{:<key_width$} {}", "plugin:", plugin,);
say!("{:<key_width$} {}", "cipher:", info.cipher,);
say!("{:<key_width$} {}", "plugin:", plugin);
say!("{:<key_width$} {}", "revision:", info.revision);
say!("{:<key_width$} {}", "cipher:", info.cipher);
say!("{:<key_width$} {}", "kdf:", info.kdf.to_string());
say!("{:<key_width$} {}", "block size (gross):", info.bsize_gross);
say!("{:<key_width$} {}", "block size (net):", info.bsize_net);
Expand All @@ -77,27 +74,4 @@ impl ContainerInfoArgs {

Ok(())
}

fn print_userdata(&self, container: &Container<PluginBackend>) -> Result<()> {
if !is_quiet() {
let mut writer = self.format.create_writer();

writer.print(container.userdata())?;
writer.flush()?;
}

Ok(())
}

pub fn run(&self) -> Result<()> {
debug!("args: {:?}", self);

let container = open_container(&self.container, self.verbose)?;

if self.userdata {
self.print_userdata(&container)
} else {
self.print_info(&container)
}
}
}

0 comments on commit faed79f

Please sign in to comment.