Skip to content

Commit

Permalink
Merge pull request #4 from shopware/next-37318/copy-default-profiles-…
Browse files Browse the repository at this point in the history
…from-executable

NEXT-37318 - copy default profiles from executable
  • Loading branch information
LarsKemper authored Jul 29, 2024
2 parents 6a69ee3 + 3352229 commit 64b89d3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ pub enum Commands {
skip: Vec<String>,
},

/// Copy all default profiles to current folder
CopyProfiles {
/// Output path
#[arg(short, long)]
path: Option<PathBuf>,

/// Overwrite existing profiles
#[arg(short, long)]
force: bool,

/// List all available profiles
#[arg(short, long)]
list: bool,
},

/// Authenticate with a given shopware shop via integration admin API.
/// Credentials are stored in .credentials.toml in the current working directory.
Auth {
Expand Down
67 changes: 67 additions & 0 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 All @@ -15,6 +16,25 @@ mod cli;
mod config_file;
mod data;

const PROFILES: &[(&str, &str)] = &[
(
"manufacturer.yaml",
include_str!("../profiles/manufacturer.yaml"),
),
(
"product_required.yaml",
include_str!("../profiles/product_required.yaml"),
),
(
"product_variants.yaml",
include_str!("../profiles/product_variants.yaml"),
),
(
"product_with_manufacturer.yaml",
include_str!("../profiles/product_with_manufacturer.yaml"),
),
];

#[derive(Debug)]
pub struct SyncContext {
pub sw_client: SwClient,
Expand All @@ -37,6 +57,9 @@ async fn main() -> anyhow::Result<()> {
index(skip).await?;
println!("Successfully triggered indexing.");
}
Commands::CopyProfiles { force, list, path } => {
copy_profiles(force, list, path);
}
Commands::Auth { domain, id, secret } => {
auth(domain, id, secret).await?;
println!("Successfully authenticated. You can continue with other commands now.")
Expand Down Expand Up @@ -90,6 +113,50 @@ async fn index(skip: Vec<String>) -> anyhow::Result<()> {
Ok(())
}

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;
}

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;
}

match fs::write(&dest_path, content) {
Ok(_) => println!("Copied profile: {} -> {:?}", name, dest_path),
Err(e) => eprintln!("Failed to write file {}: {}", name, e),
}
}
}

async fn auth(domain: String, id: String, secret: String) -> anyhow::Result<()> {
let credentials = Credentials {
base_url: domain.trim_end_matches('/').to_string(),
Expand Down

0 comments on commit 64b89d3

Please sign in to comment.