Skip to content

Commit

Permalink
NEXT-37118 - add path arg & non force copying
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsKemper committed Jul 22, 2024
1 parent 87b3431 commit a5a1bde
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
3 changes: 1 addition & 2 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ fn main() {

profiles_content.push_str(&format!(
" (\"{}\", include_bytes!(concat!(env!(\"OUT_DIR\"), \"/profiles/{}\"))),\n",
file_name,
file_name
file_name, file_name
));
}

Expand Down
4 changes: 3 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ pub struct Cli {
pub enum Commands {
/// Copy all default profiles to current folder
CopyProfiles {
// TODO: Add a flag to define the target path/folder
/// Output path
#[arg(short, long)]
path: Option<PathBuf>,

/// Overwrite existing profiles
#[arg(short, long)]
Expand Down
56 changes: 40 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::data::{export, import, prepare_scripting_environment, ScriptingEnviro
use anyhow::Context;
use clap::Parser;
use std::collections::HashSet;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;
Expand Down Expand Up @@ -35,11 +36,8 @@ async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();

match cli.command {
Commands::CopyProfiles { force, list } => {
copy_profiles(force, list);

// TODO: add actual path to folder of profiles
println!("Successfully copied profiles.")
Commands::CopyProfiles { force, list, path } => {
copy_profiles(force, list, path);
}
Commands::Auth { domain, id, secret } => {
auth(domain, id, secret).await?;
Expand Down Expand Up @@ -79,20 +77,46 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}

pub fn copy_profiles(force: bool, list: bool) {
for (name, content) in PROFILES {
if list {
println!("Profile: {}", name);
pub fn copy_profiles(force: bool, list: bool, path: Option<PathBuf>) {
if list {
println!("Available profiles:");

for profile in PROFILES {
println!("- {}", profile.0);
}

return;
}

let mut dir_path = PathBuf::from("./profiles");

if path.is_some() {
let path = path.unwrap();

if !path.is_dir() && !force {
eprintln!("Path is not a directory: {:?}", path);
return;
}

// TODO: normal mode
dir_path = path;
}

if let Err(e) = fs::create_dir_all(&dir_path) {
eprintln!("Failed to create directory: {}", e);
return;
}

for (name, content) in PROFILES {
let dest_path = dir_path.join(name);

if dest_path.exists() && !force {
eprintln!("File {} already exists. Use --force to overwrite.", name);
continue;
}

// TODO: force mode

if force {
let dest_path = format!("./output/{}", name);
std::fs::create_dir_all("./output").unwrap(); // Ensure the output directory exists
std::fs::write(dest_path, content).unwrap();
match fs::write(&dest_path, content) {
Ok(_) => println!("Copied profile: {} -> {:?}", name, dest_path),
Err(e) => eprintln!("Failed to write file {}: {}", name, e),
}
}
}
Expand Down

0 comments on commit a5a1bde

Please sign in to comment.