add dry-run option

Closes #2
This commit is contained in:
Dmitriy Pleshevskiy 2022-08-02 11:32:12 +03:00
parent 146c0697f2
commit 1a6dfb8d46
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
2 changed files with 13 additions and 2 deletions

View File

@ -41,6 +41,9 @@ pub struct Args {
#[clap(short = 'o', long)]
pub output: Option<PathBuf>,
#[clap(long)]
pub dry_run: bool,
#[clap(value_parser)]
pub sections: Vec<String>,
}

View File

@ -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<dyn Write> = 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,