From 3963910e1e7ee42ecda63af200f857d6c547c355 Mon Sep 17 00:00:00 2001 From: Lynne Date: Mon, 15 Feb 2021 00:25:32 +1000 Subject: [PATCH] write header/footer with fif version, sh shebang, etc. --- src/formats.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/formats.rs b/src/formats.rs index 47d86d7..71c628b 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -1,14 +1,16 @@ use std::io::{self, Write}; -use crate::Findings; -use crate::scanerror::ScanError; -use std::path::PathBuf; -use snailquote::escape; - #[cfg(unix)] use std::os::unix::ffi::OsStrExt; - #[cfg(windows)] use std::os::windows::ffi::OsStrExt; +use std::path::PathBuf; + +use snailquote::escape; + +use crate::Findings; +use crate::scanerror::ScanError; + +const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); type Entries = [Result]; @@ -32,9 +34,13 @@ pub trait Format { fn no_known_extension(&self, f: &mut W, path: &PathBuf) -> io::Result<()>; fn unreadable(&self, f: &mut W, path: &PathBuf) -> io::Result<()>; fn unknown_type(&self, f: &mut W, path: &PathBuf) -> io::Result<()>; + fn header(&self, entries: &Entries, f: &mut W) -> io::Result<()>; + fn footer(&self, entries: &Entries, f: &mut W) -> io::Result<()>; fn write_all(&self, entries: &Entries, f: &mut W) -> io::Result<()> { // TODO: clean this up - it's horrifying + self.header(entries, f)?; + for entry in entries { match entry { Ok(finding) => { @@ -64,7 +70,8 @@ pub trait Format { } } } - Ok(()) + + self.footer(entries, f) } } @@ -101,4 +108,12 @@ impl Format for Script { write_pathbuf(f, path)?; write!(f, "\n") } + + fn header(&self, _: &Entries, f: &mut W) -> io::Result<()> { + write!(f, "#!/usr/bin/env sh\n\nGenerated by fif {}.\n\n", VERSION.unwrap_or("???")) + } + + fn footer(&self, _: &Entries, f: &mut W) -> io::Result<()> { + writeln!(f, "\necho Done.") + } } \ No newline at end of file