summaryrefslogtreecommitdiff
path: root/angelsharkcli
diff options
context:
space:
mode:
authorCarpenter, Adam (CORP) <Adam.Carpenter@adp.com>2021-10-12 14:07:10 -0400
committerCarpenter, Adam (CORP) <Adam.Carpenter@adp.com>2021-10-12 14:07:10 -0400
commit2f67f82a64f4eabfbe758655099f48d8afa07fc3 (patch)
tree8aaad4e02f09635930b13db9851b3696ac345343 /angelsharkcli
parent154367ad2f598b99ca9ccdec175eddcdfa09ef76 (diff)
downloadaltruistic-angelshark-2f67f82a64f4eabfbe758655099f48d8afa07fc3.tar.xz
altruistic-angelshark-2f67f82a64f4eabfbe758655099f48d8afa07fc3.zip
feat: init upload
Diffstat (limited to 'angelsharkcli')
-rw-r--r--angelsharkcli/Cargo.toml22
-rw-r--r--angelsharkcli/src/main.rs165
2 files changed, 187 insertions, 0 deletions
diff --git a/angelsharkcli/Cargo.toml b/angelsharkcli/Cargo.toml
new file mode 100644
index 0000000..5e399f8
--- /dev/null
+++ b/angelsharkcli/Cargo.toml
@@ -0,0 +1,22 @@
+[package]
+name = "angelsharkcli"
+version = "0.1.0"
+edition = "2018"
+authors = ["Adam T. Carpenter <adam.carpenter@adp.com>"]
+description = "A command-line interface into one or more Communication Managers"
+
+[dependencies.libangelshark]
+path = "../libangelshark"
+
+[dependencies.csv]
+version = "1"
+
+[dependencies.serde_json]
+version = "1"
+
+[dependencies.clap]
+version = "2"
+default-features = false
+
+[dependencies.anyhow]
+version = "1"
diff --git a/angelsharkcli/src/main.rs b/angelsharkcli/src/main.rs
new file mode 100644
index 0000000..9d1ac36
--- /dev/null
+++ b/angelsharkcli/src/main.rs
@@ -0,0 +1,165 @@
+use anyhow::{anyhow, Context, Error, Result};
+use clap::{App, Arg, ArgMatches, SubCommand};
+use csv::{QuoteStyle, WriterBuilder};
+use libangelshark::{Acm, AcmRunner, Message, ParallelIterator};
+use std::{
+ fs::File,
+ io::{stdin, stdout, BufWriter, Write},
+};
+
+fn main() -> Result<()> {
+ // Parse arguments.
+ let args = parse_args();
+
+ // Collect logins.
+ let path = args.value_of("config").unwrap_or("./asa.cfg");
+ let logins_file =
+ File::open(path).with_context(|| format!("Failed to open logins file: {}", path))?;
+ let acms = Acm::from_logins(logins_file).with_context(|| "Failed to parse logins.")?;
+ let inputs = Message::from_input(stdin()).with_context(|| "Failed to read input.")?;
+
+ match args.subcommand() {
+ ("test", _) => {
+ // Echo the parsed logins and input.
+ println!("{:?}", acms);
+ println!("{:?}", inputs);
+ }
+ ("man", _) => {
+ // Print manual pages for the given input.
+ AcmRunner::new(acms, inputs)
+ .manuals()
+ .for_each(|(name, output)| match output {
+ Err(e) => eprintln!(
+ "{}",
+ anyhow!(e).context(format!("angelsharkcli: manual ({})", name))
+ ),
+ Ok(o) => println!("{}", o),
+ });
+ }
+ ("print", Some(args)) => {
+ // Run the input and print the output.
+ let format = args.value_of("format").unwrap_or("tsv");
+ let header_row = args.is_present("header_row");
+ let prefix = args.value_of("prefix").unwrap_or_default();
+ let to_file = args.is_present("to_file");
+
+ AcmRunner::new(acms, inputs)
+ .run()
+ .filter_map(|(name, output)| match output {
+ Err(e) => {
+ eprintln!("angelsharkcli: runner ({}): {}", name, e);
+ None
+ }
+ Ok(messages) => Some((name, messages)),
+ })
+ .try_for_each(|(name, outputs)| {
+ for (command, datas) in outputs.into_iter().filter_map(|message| {
+ if message.command == "logoff" {
+ None
+ } else if let Some(error) = &message.error {
+ eprintln!("angelsharkcli: ossi ({}): {}", name, error);
+ None
+ } else if let Some(datas) = message.datas {
+ if header_row {
+ Some((
+ message.command,
+ vec![message.fields.unwrap_or_default()]
+ .into_iter()
+ .chain(datas.into_iter())
+ .collect(),
+ ))
+ } else {
+ Some((message.command, datas))
+ }
+ } else {
+ None
+ }
+ }) {
+ let writer: BufWriter<Box<dyn Write>> = BufWriter::new(if to_file {
+ let filename = format!(
+ "./{}angelshark -- {} -- {}.{}",
+ prefix, name, command, format
+ );
+ let file = File::create(&filename).with_context(|| {
+ format!("Failed to create output file: {}", filename)
+ })?;
+ Box::new(file)
+ } else {
+ Box::new(stdout())
+ });
+
+ match format {
+ "json" => {
+ serde_json::to_writer_pretty(writer, &datas)
+ .with_context(|| "Failed to write JSON.")?;
+ }
+ "csv" => {
+ let mut writer = WriterBuilder::new()
+ .quote_style(QuoteStyle::Always)
+ .from_writer(writer);
+
+ for data in datas {
+ writer
+ .write_record(&data)
+ .with_context(|| "Failed to write CSV.")?;
+ }
+ }
+ _ => {
+ let mut writer = WriterBuilder::new()
+ .delimiter(b'\t')
+ .quote_style(QuoteStyle::Never)
+ .from_writer(writer);
+
+ for data in datas {
+ writer
+ .write_record(&data)
+ .with_context(|| "Failed to write TSV.")?;
+ }
+ }
+ }
+ }
+
+ Result::<(), Error>::Ok(())
+ })?;
+ }
+ _ => {
+ // Just run the input and print any errors encountered.
+ AcmRunner::new(acms, inputs)
+ .run()
+ .for_each(|(name, output)| match output {
+ Err(e) => {
+ eprintln!(
+ "{}",
+ anyhow!(e).context(format!("angelsharkcli: runner ({})", name))
+ );
+ }
+ Ok(o) => {
+ for msg in o {
+ if let Some(e) = msg.error {
+ eprintln!(
+ "{}",
+ anyhow!(e)
+ .context(format!("angelsharkcli: ossi ({})", name))
+ );
+ }
+ }
+ }
+ });
+ }
+ }
+
+ Ok(())
+}
+
+fn parse_args() -> ArgMatches<'static> {
+ let app = App::new("Altruistic Angelshark CLI")
+ .author(env!("CARGO_PKG_AUTHORS"))
+ .about(env!("CARGO_PKG_DESCRIPTION"))
+ .version(env!("CARGO_PKG_VERSION"))
+ .long_about("\nReads STDIN and parses all lines as commands to be fed to one or more ACMs. When it reaches EOF, it stops parsing and starts executing the command(s) on the ACM(s). What it does with the output can be configured with subcommands and flags. If you like keeping your commands in a file, consider using the `<` to read it on STDIN. The default behavior is to run commands but print no output (for quick changes). Errors are printed on STDERR.")
+ .arg(Arg::with_name("config").long("login-file").short("l").default_value("./asa.cfg").help("Set ACM login configuration file"))
+ .subcommand(SubCommand::with_name("test").about("Prints parsed logins and inputs but does not run anything").long_about("Does not execute commands entered, instead prints out the ACM logins and inputs it read (useful for debugging)"))
+ .subcommand(SubCommand::with_name("man").about("Prints command manual pages via `ossim` term").long_about("Reads commands on STDIN and prints their SAT manual pages on STDOUT"))
+ .subcommand(SubCommand::with_name("print").about("Prints command output to STDOUT (or files) in a useful format").long_about("Runs commands on input and writes *their data entries* to STDOUT in variety of formats (and optionally to files)").arg(Arg::with_name("prefix").long("prefix").short("p").takes_value(true).requires("to_file").help("Prepend a prefix to all output filenames")).arg(Arg::with_name("to_file").short("t").long("to-file").help("Write output to separate files instead of STDOUT")).arg(Arg::with_name("header_row").short("h").long("header-row").help("Prepend header entry of hexadecimal field addresses to output")).arg(Arg::with_name("format").short("f").long("format").possible_values(&["csv", "json", "tsv"]).default_value("tsv").help("Format data should be printed in")));
+ app.get_matches_safe().unwrap_or_else(|e| e.exit())
+}