diff options
Diffstat (limited to 'src/cmd/record.rs')
-rw-r--r-- | src/cmd/record.rs | 104 |
1 files changed, 70 insertions, 34 deletions
diff --git a/src/cmd/record.rs b/src/cmd/record.rs index c233dc4..83651f5 100644 --- a/src/cmd/record.rs +++ b/src/cmd/record.rs @@ -1,13 +1,79 @@ use crate::prelude::*; use tokio::io::AsyncWrite as _; +#[derive(serde::Deserialize)] +pub struct Config { + #[serde(default = "crate::config::default_ttyrec_filename")] + filename: String, + + #[serde(default = "crate::config::default_connection_buffer_size")] + buffer_size: usize, + + #[serde(default = "crate::config::default_command")] + command: String, + + #[serde(default = "crate::config::default_args")] + args: Vec<String>, +} + +impl crate::config::Config for Config { + fn merge_args<'a>( + &mut self, + matches: &clap::ArgMatches<'a>, + ) -> Result<()> { + if matches.is_present("filename") { + self.filename = matches.value_of("filename").unwrap().to_string(); + } + if matches.is_present("buffer-size") { + let buffer_size = matches.value_of("buffer-size").unwrap(); + self.buffer_size = buffer_size.parse().context( + crate::error::ParseBufferSize { input: buffer_size }, + )?; + } + if matches.is_present("command") { + self.command = matches.value_of("command").unwrap().to_string(); + } + if matches.is_present("args") { + self.args = matches + .values_of("args") + .unwrap() + .map(std::string::ToString::to_string) + .collect(); + } + Ok(()) + } + + fn run(&self) -> Result<()> { + let fut = RecordSession::new( + &self.filename, + self.buffer_size, + &self.command, + &self.args, + ); + tokio::run(fut.map_err(|e| { + eprintln!("{}", e); + })); + Ok(()) + } +} + +impl Default for Config { + fn default() -> Self { + Self { + filename: crate::config::default_ttyrec_filename(), + buffer_size: crate::config::default_connection_buffer_size(), + command: crate::config::default_command(), + args: crate::config::default_args(), + } + } +} + pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { app.about("Record a terminal session to a file") .arg( clap::Arg::with_name("filename") .long("filename") - .takes_value(true) - .required(true), + .takes_value(true), ) .arg( clap::Arg::with_name("buffer-size") @@ -18,38 +84,8 @@ pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> { .arg(clap::Arg::with_name("args").index(2).multiple(true)) } -pub fn run<'a>(matches: &clap::ArgMatches<'a>) -> Result<()> { - let filename = matches.value_of("filename").unwrap(); - let buffer_size = - matches - .value_of("buffer-size") - .map_or(Ok(4 * 1024 * 1024), |s| { - s.parse() - .context(crate::error::ParseBufferSize { input: s }) - })?; - let command = matches.value_of("command").map_or_else( - || std::env::var("SHELL").unwrap_or_else(|_| "/bin/bash".to_string()), - std::string::ToString::to_string, - ); - let args = if let Some(args) = matches.values_of("args") { - args.map(std::string::ToString::to_string).collect() - } else { - vec![] - }; - run_impl(filename, buffer_size, &command, &args) -} - -fn run_impl( - filename: &str, - buffer_size: usize, - command: &str, - args: &[String], -) -> Result<()> { - let fut = RecordSession::new(filename, buffer_size, command, args); - tokio::run(fut.map_err(|e| { - eprintln!("{}", e); - })); - Ok(()) +pub fn config() -> Box<dyn crate::config::Config> { + Box::new(Config::default()) } #[allow(clippy::large_enum_variant)] |