From 1a6dfb8d466e1d9d5e1b6148c015b7dec3dca20e Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Tue, 2 Aug 2022 11:32:12 +0300 Subject: [PATCH] add dry-run option Closes #2 --- src/cli.rs | 3 +++ src/cli/switch.rs | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index e236cf0..37be42d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -41,6 +41,9 @@ pub struct Args { #[clap(short = 'o', long)] pub output: Option, + #[clap(long)] + pub dry_run: bool, + #[clap(value_parser)] pub sections: Vec, } diff --git a/src/cli/switch.rs b/src/cli/switch.rs index a73ab3c..ab3a2f3 100644 --- a/src/cli/switch.rs +++ b/src/cli/switch.rs @@ -16,6 +16,7 @@ use crate::{cli::Args, domain}; use std::fs::File; +use std::io::{stdout, Write}; #[derive(Debug)] pub enum Error { @@ -36,8 +37,15 @@ impl std::error::Error for Error {} pub fn execute(args: &Args) -> Result<(), Error> { let content = std::fs::read_to_string(&args.file).map_err(|_| Error::OpenFile)?; - let writer = - File::create(args.output.as_ref().unwrap_or(&args.file)).map_err(|_| Error::OpenFile)?; + + let writer: Box = if args.dry_run { + Box::new(stdout()) + } else { + Box::new( + File::create(args.output.as_ref().unwrap_or(&args.file)) + .map_err(|_| Error::OpenFile)?, + ) + }; domain::switch::execute(domain::switch::Request { content: &content,