diff options
author | Justus Winter <justus@pep-project.org> | 2017-12-21 16:31:44 +0100 |
---|---|---|
committer | Justus Winter <justus@pep-project.org> | 2017-12-21 18:02:35 +0100 |
commit | b6fae6ab962f0061f18158d04c4ef6a7d06498cf (patch) | |
tree | 3702a1234e4e25e06f682681c7ea799ef49131eb /tool | |
parent | f2e693585e65d8c4873f5bbef8fd32c93e7a2d9e (diff) |
tool: New crate.
- A command line tool to interact with Sequoia. Useful for
debugging and development.
Diffstat (limited to 'tool')
-rw-r--r-- | tool/Cargo.toml | 14 | ||||
-rw-r--r-- | tool/Makefile | 13 | ||||
-rw-r--r-- | tool/make-usage.sh | 41 | ||||
-rw-r--r-- | tool/src/main.rs | 100 | ||||
-rw-r--r-- | tool/src/usage.rs | 78 |
5 files changed, 246 insertions, 0 deletions
diff --git a/tool/Cargo.toml b/tool/Cargo.toml new file mode 100644 index 00000000..c1db4614 --- /dev/null +++ b/tool/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "sequoia-tool" +version = "0.1.0" +authors = ["Justus Winter <justus@pep-project.org>"] + +[dependencies] +openpgp = { path = "../openpgp" } +sequoia-core = { path = "../core" } +sequoia-net = { path = "../net" } +clap = "2.27.1" + +[[bin]] +name = "sq" +path = "src/usage.rs" diff --git a/tool/Makefile b/tool/Makefile new file mode 100644 index 00000000..517f705e --- /dev/null +++ b/tool/Makefile @@ -0,0 +1,13 @@ +CARGO ?= cargo +SQ ?= ../target/debug/sq + +all: sq src/usage.rs + +sq: ../target/debug/sq + +.PHONY: ../target/debug/sq +../target/debug/sq: + $(CARGO) build + +src/usage.rs: make-usage.sh $(SQ) + sh make-usage.sh $(SQ) >$@ diff --git a/tool/make-usage.sh b/tool/make-usage.sh new file mode 100644 index 00000000..bc150a75 --- /dev/null +++ b/tool/make-usage.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +quote() { + sed 's@^@//! @' | sed 's/ $//' +} + +begin_code() { + printf '```text\n' +} + +end_code() { + printf '```\n' +} + +( + printf "A command-line frontend for Sequoia. + +# Usage + +" + begin_code + sq --help + end_code + + sq --help | + sed -n '/^SUBCOMMANDS:/,$p' | + tail -n+2 | + while read command desc + do + if [ "$command" = help ]; then + continue + fi + + printf "\n## Subcommand $command\n\n" + begin_code + sq $command --help + end_code + done +) | quote + +printf '\ninclude!("main.rs");\n' diff --git a/tool/src/main.rs b/tool/src/main.rs new file mode 100644 index 00000000..2ac138e8 --- /dev/null +++ b/tool/src/main.rs @@ -0,0 +1,100 @@ +/// A command-line frontend for Sequoia. + +extern crate clap; + +use clap::{Arg, App, SubCommand, AppSettings}; +use std::fs::File; +use std::io; + +extern crate openpgp; +extern crate sequoia_core; +extern crate sequoia_net; + +use openpgp::armor; + +fn open_or_stdin(f: Option<&str>) -> Box<io::Read> { + match f { + Some(f) => Box::new(File::open(f).unwrap()), + None => Box::new(io::stdin()), + } +} + +fn create_or_stdout(f: Option<&str>) -> Box<io::Write> { + match f { + Some(f) => Box::new(File::create(f).unwrap()), + None => Box::new(io::stdout()), + } +} + +fn main() { + let matches = App::new("sq") + .version("0.1.0") + .about("Sequoia is an implementation of OpenPGP. This is a command-line frontend.") + .setting(AppSettings::ArgRequiredElseHelp) + .subcommand(SubCommand::with_name("enarmor") + .about("Applies ASCII Armor to a file") + .arg(Arg::with_name("input").value_name("FILE") + .long("input") + .short("i") + .help("Sets the input file to use")) + .arg(Arg::with_name("output").value_name("FILE") + .long("output") + .short("o") + .help("Sets the output file to use"))) + .subcommand(SubCommand::with_name("dearmor") + .about("Removes ASCII Armor from a file") + .arg(Arg::with_name("input").value_name("FILE") + .long("input") + .short("i") + .help("Sets the input file to use")) + .arg(Arg::with_name("output").value_name("FILE") + .long("output") + .short("o") + .help("Sets the output file to use"))) + .subcommand(SubCommand::with_name("dump") + .about("Lists OpenPGP packets") + .arg(Arg::with_name("input").value_name("FILE") + .long("input") + .short("i") + .help("Sets the input file to use")) + .arg(Arg::with_name("output").value_name("FILE") + .long("output") + .short("o") + .help("Sets the output file to use")) + .arg(Arg::with_name("dearmor") + .long("dearmor") + .short("A") + .help("Remove ASCII Armor from input"))) + .get_matches(); + + match matches.subcommand() { + ("enarmor", Some(m)) => { + let mut input = open_or_stdin(m.value_of("input")); + let mut output = create_or_stdout(m.value_of("output")); + let mut filter = armor::Writer::new(&mut output, armor::Kind::File); + io::copy(&mut input, &mut filter).unwrap(); + }, + ("dearmor", Some(m)) => { + let mut input = open_or_stdin(m.value_of("input")); + let mut output = create_or_stdout(m.value_of("output")); + let mut filter = armor::Reader::new(&mut input, armor::Kind::Any); + io::copy(&mut filter, &mut output).unwrap(); + }, + ("dump", Some(m)) => { + let mut input = open_or_stdin(m.value_of("input")); + let mut output = create_or_stdout(m.value_of("output")); + let input = if m.is_present("dearmor") { + Box::new(armor::Reader::new(&mut input, armor::Kind::Any)) + } else { + input + }; + let m = openpgp::Message::from_reader(input); + for p in m.iter() { + writeln!(output, "{:?}", p).unwrap(); + } + }, + _ => { + unreachable!(); + }, + } +} diff --git a/tool/src/usage.rs b/tool/src/usage.rs new file mode 100644 index 00000000..3ce13d3b --- /dev/null +++ b/tool/src/usage.rs @@ -0,0 +1,78 @@ +//! A command-line frontend for Sequoia. +//! +//! # Usage +//! +//! ```text +//! sq 0.1.0 +//! Sequoia is an implementation of OpenPGP. This is a command-line frontend. +//! +//! USAGE: +//! sq [SUBCOMMAND] +//! +//! FLAGS: +//! -h, --help Prints help information +//! -V, --version Prints version information +//! +//! SUBCOMMANDS: +//! dearmor Removes ASCII Armor from a file +//! dump Lists OpenPGP packets +//! enarmor Applies ASCII Armor to a file +//! help Prints this message or the help of the given subcommand(s) +//! ``` +//! +//! ## Subcommand dearmor +//! +//! ```text +//! sq-dearmor +//! Removes ASCII Armor from a file +//! +//! USAGE: +//! sq dearmor [OPTIONS] +//! +//! FLAGS: +//! -h, --help Prints help information +//! -V, --version Prints version information +//! +//! OPTIONS: +//! -i, --input <FILE> Sets the input file to use +//! -o, --output <FILE> Sets the output file to use +//! ``` +//! +//! ## Subcommand dump +//! +//! ```text +//! sq-dump +//! Lists OpenPGP packets +//! +//! USAGE: +//! sq dump [FLAGS] [OPTIONS] +//! +//! FLAGS: +//! -A, --dearmor Remove ASCII Armor from input +//! -h, --help Prints help information +//! -V, --version Prints version information +//! +//! OPTIONS: +//! -i, --input <FILE> Sets the input file to use +//! -o, --output <FILE> Sets the output file to use +//! ``` +//! +//! ## Subcommand enarmor +//! +//! ```text +//! sq-enarmor +//! Applies ASCII Armor to a file +//! +//! USAGE: +//! sq enarmor [OPTIONS] +//! +//! FLAGS: +//! -h, --help Prints help information +//! -V, --version Prints version information +//! +//! OPTIONS: +//! -i, --input <FILE> Sets the input file to use +//! -o, --output <FILE> Sets the output file to use +//! ``` + +include!("main.rs"); |