Copy files with tour init

This commit is contained in:
2026-02-18 17:37:01 +00:00
parent efa297a07a
commit 7aa3ca651e
4 changed files with 31 additions and 2 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
/target
.tour/

View File

@@ -4,6 +4,7 @@
// Populate with tour/steps/0/files/file2
use crate::TOUR_DIR;
use crate::utils::copy_files;
use std::fs;
use std::fs::File;
@@ -11,13 +12,21 @@ use std::io::Write;
use std::path::PathBuf;
pub fn init(files: Vec<PathBuf>, message: String) -> Result<(), std::io::Error> {
if fs::exists(TOUR_DIR).is_ok() {
// Convert PathBuf to &Path
let files = files.iter().map(|p| p.as_path()).collect();
// Check TOUR_DIR exists (it shouldn't because user calls init)
if fs::exists(TOUR_DIR)? {
panic!("{} folder exists", TOUR_DIR);
}
// Create dir recursively
fs::create_dir_all(format!("{}/{}", TOUR_DIR, "steps/0/files"))?;
// Copy files listed by command to step 0
let dest = format!("{}/steps/0/files/", TOUR_DIR);
copy_files(files, dest.as_ref())?;
// Copy message to step 0
let mut message_file = File::create("./.tour/steps/0/message")?;
write!(message_file, "{}", message)?;

View File

@@ -1,6 +1,6 @@
use clap::{Parser, Subcommand};
use std::error::Error;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
mod init;
mod utils;

19
src/utils.rs Normal file
View File

@@ -0,0 +1,19 @@
use std::fs;
use std::io;
use std::path::Path;
pub fn copy_files(files: Vec<&Path>, dest_dir: &Path) -> Result<(), io::Error> {
for file in files {
// Get the relative path components
let dest_path = dest_dir.join(file);
// Create parent directories if they don't exist
if let Some(parent) = dest_path.parent() {
fs::create_dir_all(parent)?;
}
// Copy the file
fs::copy(file, dest_path)?;
}
Ok(())
}