Skip to content

Commit

Permalink
autocomplete support for pol_export
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Starke committed Sep 8, 2023
1 parent 2814724 commit ef6a78b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
22 changes: 22 additions & 0 deletions src/bin/pol_export/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use clap::Parser;
use dfir_toolkit::common::HasVerboseFlag;
use log::LevelFilter;


#[derive(Parser, Debug)]
#[clap(name=env!("CARGO_BIN_NAME"), author, version, about, long_about = None)]
pub (crate) struct Cli {
/// Name of the file to read
#[clap()]
pub (crate) polfile: String,

#[clap(flatten)]
pub (crate) verbose: clap_verbosity_flag::Verbosity,

}

impl HasVerboseFlag for Cli {
fn log_level_filter(&self) -> LevelFilter {
self.verbose.log_level_filter()
}
}
40 changes: 8 additions & 32 deletions src/bin/pol_export/main.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,21 @@
use std::{
fs::File,
io::{Read, stdout},
io::{stdout, Read},
};

use anyhow::{anyhow, Result};
use binread::{BinReaderExt, BinResult};
use clap::Parser;
use cli::Cli;
use dfir_toolkit::common::FancyParser;

mod cli;
mod policy_file_entry;

use csv::Writer;
use policy_file_entry::*;
use simplelog::{TermLogger, Config};


#[derive(Parser, Debug)]
#[clap(name=env!("CARGO_BIN_NAME"), author, version, about, long_about = None)]
struct Args {
/// Name of the file to read
#[clap()]
polfile: String,

#[clap(flatten)]
pub (crate) verbose: clap_verbosity_flag::Verbosity,

/// print help in markdown format
#[arg(long, hide = true, exclusive=true)]
pub markdown_help: bool,
}

fn main() -> Result<()> {
if std::env::args().any(|a| &a == "--markdown-help") {
clap_markdown::print_help_markdown::<Args>();
return Ok(());
}
let args = Args::parse();
let _ = TermLogger::init(
args.verbose.log_level_filter(),
Config::default(),
simplelog::TerminalMode::Stderr,
simplelog::ColorChoice::Auto);
let args = Cli::parse_cli();

let mut polfile = File::open(args.polfile)?;

Expand All @@ -56,13 +32,13 @@ fn main() -> Result<()> {
}

let mut wtr = Writer::from_writer(stdout());

loop {
let entry_result: BinResult<PolicyFileEntry> = polfile.read_le();
match entry_result {
Ok(entry) => {
wtr.serialize(entry)?;
},
}
Err(why) => match why {
binread::Error::Io(why) if why.kind() == std::io::ErrorKind::OutOfMemory => break,
binread::Error::Io(why) if why.kind() == std::io::ErrorKind::BrokenPipe => break,
Expand All @@ -72,7 +48,7 @@ fn main() -> Result<()> {
//continue;
break;
}
}
},
}
}
Ok(())
Expand Down

0 comments on commit ef6a78b

Please sign in to comment.