Skip to content

Commit

Permalink
feat: add scoop command for the CSAF cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dejanb authored and ctron committed Jul 29, 2024
1 parent d114ad9 commit 0d4d912
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions csaf/csaf-cli/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod metadata;
pub mod parse;
pub mod report;
pub mod scan;
pub mod scoop;
pub mod send;
pub mod sync;

Expand Down
73 changes: 73 additions & 0 deletions csaf/csaf-cli/src/cmd/scoop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use anyhow::anyhow;
use std::path::{Path, PathBuf};
use walker_common::{
cli::client::ClientArguments, compression::decompress, progress::Progress,
scoop::ScooperBuilder,
};
use walker_extras::visitors::{SendArguments, SendVisitor};

/// Walk a local directory (or single file) and send the files to a target without any validation.
#[derive(clap::Args, Debug)]
pub struct Scoop {
/// Delete processes files
#[arg(long, conflicts_with = "processed")]
delete: bool,

/// Directory to move processed files to.
#[arg(long)]
processed: Option<PathBuf>,

/// Directory to move processed files to.
#[arg(long)]
failed: Option<PathBuf>,

#[command(flatten)]
client: ClientArguments,

#[command(flatten)]
send: SendArguments,

#[command(flatten)]
source: SourceArguments,
}

#[derive(Debug, clap::Parser)]
#[command(next_help_heading = "Source")]
struct SourceArguments {
/// Files or directories to upload
#[arg()]
source: Vec<PathBuf>,
}

impl Scoop {
pub async fn run(self, progress: Progress) -> anyhow::Result<()> {
log::debug!("Start processing");

let scooper = ScooperBuilder {
sources: self.source.source,
processed: self.processed,
failed: self.failed,
delete: self.delete,
}
.build()?;

let send: SendVisitor = self.send.into_visitor().await?;

scooper
.process(progress, |path: &Path| {
let send = send.clone();
Box::pin(async move {
let data = tokio::fs::read(&path).await?;
let name = path
.to_str()
.ok_or_else(|| anyhow!("Invalid UTF-8 sequence in path"))?;
let data = decompress(data.into(), name)?;

send.send_advisory(&path.to_string_lossy(), data).await?;

Ok(())
})
})
.await
}
}
4 changes: 3 additions & 1 deletion csaf/csaf-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod common;
use clap::Parser;
use cmd::{
discover::Discover, download::Download, metadata::Metadata, parse::Parse, report::Report,
scan::Scan, send::Send, sync::Sync,
scan::Scan, scoop::Scoop, send::Send, sync::Sync,
};
use std::process::ExitCode;
use walker_common::{cli::log::Logging, progress::Progress, utils::measure::MeasureTime};
Expand All @@ -30,6 +30,7 @@ enum Command {
Report(Report),
Send(Send),
Metadata(Metadata),
Scoop(Scoop),
}

impl Command {
Expand All @@ -43,6 +44,7 @@ impl Command {
Command::Report(cmd) => cmd.run(progress).await,
Command::Send(cmd) => cmd.run(progress).await,
Command::Metadata(cmd) => cmd.run().await,
Command::Scoop(cmd) => cmd.run(progress).await,
}
}
}
Expand Down

0 comments on commit 0d4d912

Please sign in to comment.