vnetod/src/cli/switch.rs

92 lines
2.7 KiB
Rust
Raw Permalink Normal View History

2022-11-07 23:29:25 +03:00
//! Copyright (C) 2022, Dmitriy Pleshevskiy <dmitriy@pleshevski.ru>
2022-08-01 00:10:06 +03:00
//!
//! vnetod is free software: you can redistribute it and/or modify
//! it under the terms of the GNU General Public License as published by
//! the Free Software Foundation, either version 3 of the License, or
//! (at your option) any later version.
//!
//! vnetod is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with vnetod. If not, see <https://www.gnu.org/licenses/>.
//!
2022-11-10 00:19:18 +03:00
#[cfg(feature = "color")]
2022-11-12 16:25:35 +03:00
use termcolor::{Color, ColorSpec, StandardStream, WriteColor};
2022-11-10 00:19:18 +03:00
use crate::{cli::Args, domain};
use std::fs::File;
2022-11-10 00:19:18 +03:00
use std::io::Write;
#[derive(Debug)]
pub enum Error {
OpenFile,
2022-11-10 00:19:18 +03:00
WriteFile,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::OpenFile => f.write_str("Cannot open file"),
2022-11-10 00:19:18 +03:00
Error::WriteFile => f.write_str("Cannot write file"),
}
}
}
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)?;
2022-08-02 11:32:12 +03:00
2022-11-12 16:36:53 +03:00
if args.dry_run {
println!("Your file will be changed to the following")
}
2022-11-10 00:19:18 +03:00
let fs_writer = (!args.dry_run)
.then(|| {
File::create(args.output.as_ref().unwrap_or(&args.file)).map_err(|_| Error::OpenFile)
})
.transpose()?;
2022-11-12 16:25:35 +03:00
#[cfg(feature = "color")]
let color = args.color.clone();
println!();
2022-11-10 00:19:18 +03:00
let new_content = domain::switch::execute(domain::switch::Request {
2022-11-12 16:25:35 +03:00
content: &content.trim(),
sections: &args.sections,
2022-11-12 16:25:35 +03:00
on_line: Some(Box::new(move |line| {
#[cfg(feature = "color")]
print_line(line, color.clone());
#[cfg(not(feature = "color"))]
print!("{}", line)
})),
2022-11-10 00:19:18 +03:00
});
2022-11-12 16:25:35 +03:00
println!();
2022-11-10 00:19:18 +03:00
if let Some(mut fs_writer) = fs_writer {
fs_writer
.write_all(new_content.as_bytes())
.map_err(|_| Error::WriteFile)?;
}
Ok(())
}
#[cfg(feature = "color")]
2022-11-12 16:25:35 +03:00
fn print_line(line: &String, color: crate::cli::ColorVariant) {
let mut stdout = StandardStream::stdout(color.into());
2022-11-10 00:19:18 +03:00
let color = line
.starts_with("###")
.then_some(Color::Yellow)
.or_else(|| (!line.starts_with("#")).then_some(Color::Green));
stdout.set_color(ColorSpec::new().set_fg(color)).ok();
write!(&mut stdout, "{}", line).unwrap();
stdout.reset().ok();
}