1use crate::files::{copy_file_mode, get_relative_path};
2use anyhow::{Context, Result};
3use clap::Parser;
4use std::path::PathBuf;
5
6#[derive(Debug, Clone, Parser)]
7pub struct Args {
8 #[arg()]
10 new_file: PathBuf,
11}
12
13pub fn add(args: &Args, config: &crate::config::Config) -> Result<()> {
14 let absolute_new_file = if args.new_file.as_path().is_absolute() {
15 args.new_file.clone()
16 } else {
17 std::env::current_dir()
18 .context("get current working directory from environment")?
19 .join(&args.new_file)
20 };
21 let relative_new_file = get_relative_path(&config.dirs.home, &absolute_new_file)
22 .context("get relative target file")?;
23 let target_path = config.dirs.source.join(relative_new_file);
24 println!(
25 "Adding new file to source directory: {}",
26 target_path.display(),
27 );
28 if let Some(parent) = target_path.parent() {
29 std::fs::create_dir_all(parent).context("create sub-directory in source directory")?;
30 }
31 std::fs::copy(&absolute_new_file, &target_path).context("copy file to source directory")?;
32 copy_file_mode(&absolute_new_file, &target_path)?;
33 Ok(())
34}