Skip to content

Commit fa0cfc8

Browse files
committed
Merge branch 'main' of github:cryptidtech/bs
2 parents 425debc + 6615381 commit fa0cfc8

File tree

2 files changed

+66
-44
lines changed

2 files changed

+66
-44
lines changed

cli/src/bin/bs.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,30 @@ async fn main() -> Result<(), Error> {
8181
.with_file(true)
8282
.without_time()
8383
.try_init()
84-
.map_err(bs_cli::Error::Log)?;
84+
.map_err(Error::Log)?;
8585

8686
// load the config
8787
let config = Config::from_path(opt.config, opt.keyfile, opt.sshagent, opt.sshagentenv)?;
8888

89-
let ret = match opt.cmd {
90-
Some(cmd) => match cmd {
89+
let command_reader: Box<dyn Iterator<Item = Command>> = match opt.cmd {
90+
Some(command) => Box::new(std::iter::once(command)),
91+
None => Box::new(repl::ReplHelper::read()),
92+
};
93+
94+
for command in command_reader {
95+
let ret = match command {
9196
// process a config command
9297
Command::Config { cmd } => subcmds::config::go(cmd, &config).await,
9398
// process a plog command
94-
Command::Plog { cmd } => subcmds::plog::go(*cmd, &config).await,
95-
},
96-
None => repl::ReplHelper::run().await,
97-
};
99+
Command::Plog { cmd } => plog::go(*cmd, &config).await,
100+
};
98101

99-
if let Err(ref e) = ret {
100-
eprintln!("{}", e);
101-
Opt::clap().print_help().unwrap();
102-
println!();
103-
ret?
102+
if let Err(ref e) = ret {
103+
eprintln!("{}", e);
104+
Opt::clap().print_help().unwrap();
105+
println!();
106+
ret?
107+
}
104108
}
105109

106110
// save any config changes

cli/src/repl.rs

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use crate::{
22
subcmds::{config, plog},
3-
Command, Error,
3+
Command,
44
};
55
use colored::Colorize;
66
use rustyline::{
77
completion::{Completer, FilenameCompleter, Pair},
88
highlight::{CmdKind, Highlighter, MatchingBracketHighlighter},
99
hint::{Hint, Hinter},
10+
history::DefaultHistory,
1011
validate::MatchingBracketValidator,
1112
Completer, Context, Helper, Validator,
1213
};
@@ -404,7 +405,7 @@ impl Default for ReplHelper {
404405

405406
impl ReplHelper {
406407
/// Run the REPL
407-
pub async fn run() -> Result<(), Error> {
408+
pub fn read() -> ReplHelperIterator {
408409
let config = rustyline::Config::builder()
409410
.history_ignore_space(true)
410411
.completion_type(rustyline::CompletionType::List)
@@ -416,14 +417,29 @@ impl ReplHelper {
416417
rl.set_helper(Some(h));
417418
rl.helper_mut().expect("to get helper").colored_prompt = "bs>".green().to_string();
418419

419-
let mut input;
420-
Self::help();
420+
help();
421+
ReplHelperIterator { rl }
422+
}
423+
}
424+
425+
/// An iterator for the REPL that yields commands
426+
/// as long as the user doesn't quit
427+
pub struct ReplHelperIterator {
428+
rl: rustyline::Editor<ReplHelper, DefaultHistory>,
429+
}
430+
431+
impl Iterator for ReplHelperIterator {
432+
type Item = Command;
421433

434+
fn next(&mut self) -> Option<Self::Item> {
435+
let mut input;
422436
loop {
423-
let readline = rl.readline("bs> ");
437+
let readline = self.rl.readline("bs> ");
424438
match readline {
425439
Ok(line) => {
426-
rl.add_history_entry(line.as_str())?;
440+
self.rl
441+
.add_history_entry(line.as_str())
442+
.expect("to add history");
427443
input = line;
428444
}
429445
Err(rustyline::error::ReadlineError::Interrupted) => {
@@ -443,22 +459,24 @@ impl ReplHelper {
443459
if input.is_empty() {
444460
continue;
445461
}
446-
rl.add_history_entry(input.as_str())?;
462+
self.rl
463+
.add_history_entry(input.as_str())
464+
.expect("to add history");
447465
let mut parser = Parser::new(&input);
448466
parser.skip_ws();
449467
match parser.simple_value() {
450468
Err(_) => {}
451469
Ok(word) => match word.as_str() {
452470
"config" | "plog" => match Parser::parse_command(&input) {
453471
Ok(command) => {
454-
println!("Parsed command: {:?}", command);
472+
return Some(command);
455473
}
456474
Err(err) => {
457475
eprintln!("Error parsing command: {}", err);
458476
}
459477
},
460478
"help" => {
461-
Self::help();
479+
help();
462480
}
463481
"quit" => {
464482
break;
@@ -467,29 +485,7 @@ impl ReplHelper {
467485
},
468486
}
469487
}
470-
Ok(())
471-
}
472-
473-
fn help() {
474-
println!(
475-
r#"Commands:
476-
config <subcommand> [parameters]
477-
plot <subcommand> [parameters]
478-
help
479-
quit
480-
481-
Config Subcommands:
482-
open
483-
484-
Plog Subcommands:
485-
close
486-
fork
487-
merge
488-
open [key-op=STRING_COMMA_SEPARATED_LIST] [string-op=STRING_COMMA_SEPARATED_LIST] [file-op=STRING_COMMA_SEPARATED_LIST] [vlad=STRING] [pub-key=STRING] [entry-key=STRING] [lock=PATH_TO_SCRIPT] [unlock=PATH_TO_SCRIPT] [output=PATH_TO_FILE]
489-
print [input=PATH_TO_FILE]
490-
update [key-op=STRING_COMMA_SEPARATED_LIST] [string-op=STRING_COMMA_SEPARATED_LIST] [file-op=STRING_COMMA_SEPARATED_LIST] [delete-op=STRING_COMMA_SEPARATED_LIST] [entry-signing-key=PATH_TO_FILE] [input=PATH_TO_FILE] [lock=PATH_TO_SCRIPT] [unlock=PATH_TO_SCRIPT] [output=PATH_TO_FILE]
491-
"#
492-
)
488+
None
493489
}
494490
}
495491

@@ -636,3 +632,25 @@ impl CommandHint {
636632
}
637633
}
638634
}
635+
636+
fn help() {
637+
println!(
638+
r#"Commands:
639+
config <subcommand> [parameters]
640+
plog <subcommand> [parameters]
641+
help
642+
quit
643+
644+
Config Subcommands:
645+
open
646+
647+
Plog Subcommands:
648+
close
649+
fork
650+
merge
651+
open [key-op=STRING_COMMA_SEPARATED_LIST] [string-op=STRING_COMMA_SEPARATED_LIST] [file-op=STRING_COMMA_SEPARATED_LIST] [vlad=STRING] [pub-key=STRING] [entry-key=STRING] [lock=PATH_TO_SCRIPT] [unlock=PATH_TO_SCRIPT] [output=PATH_TO_FILE]
652+
print [input=PATH_TO_FILE]
653+
update [key-op=STRING_COMMA_SEPARATED_LIST] [string-op=STRING_COMMA_SEPARATED_LIST] [file-op=STRING_COMMA_SEPARATED_LIST] [delete-op=STRING_COMMA_SEPARATED_LIST] [entry-signing-key=PATH_TO_FILE] [input=PATH_TO_FILE] [lock=PATH_TO_SCRIPT] [unlock=PATH_TO_SCRIPT] [output=PATH_TO_FILE]
654+
"#
655+
)
656+
}

0 commit comments

Comments
 (0)