add support of change dotenv file

Closes #1
This commit is contained in:
Dmitriy Pleshevskiy 2022-07-29 18:51:54 +03:00
parent 3972001328
commit dd8f120152
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
2 changed files with 10 additions and 4 deletions

View file

@ -14,6 +14,8 @@
//! along with vnetod. If not, see <https://www.gnu.org/licenses/>.
//!
use std::path::PathBuf;
use clap::Parser;
#[derive(Parser)]
@ -31,6 +33,9 @@ under certain conditions;
"
)]
pub struct Args {
#[clap(short = 'f', long, default_value = ".env")]
pub file: PathBuf,
#[clap(value_parser)]
pub section_names: Vec<String>,
}

View file

@ -17,6 +17,7 @@
mod cli;
use std::path::Path;
use std::{
fs::File,
io::{BufWriter, Write},
@ -27,15 +28,15 @@ use clap::Parser;
fn main() -> Result<(), Error> {
let cli = cli::Args::parse();
change_env_layout(&cli.section_names)?;
change_env_layout(&cli.file, &cli.section_names)?;
Ok(())
}
fn change_env_layout(section_names: &[String]) -> Result<(), Error> {
let content = std::fs::read_to_string(".env").map_err(|_| Error::OpenFile)?;
fn change_env_layout(file: &Path, section_names: &[String]) -> Result<(), Error> {
let content = std::fs::read_to_string(file).map_err(|_| Error::OpenFile)?;
let mut writer = File::create(".env")
let mut writer = File::create(file)
.map_err(|_| Error::OpenFile)
.map(BufWriter::new)?;