Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEXT-37318 - copy default profiles from executable #4

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)] = &[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to try to write a unit test that ensures that all files in ./profiles folder are actually defined in this array?
https://doc.rust-lang.org/book/ch11-01-writing-tests.html

You could also extend it to parse these profiles and see if that produces errors.

If you don't, that's also fine and I try to cover it together with my "test coverage" ticket 🙂

(
"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;
}
Comment on lines +127 to +138
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little more idomatic rust 🤓

Suggested change
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;
}
let Some(dir_path) = path else {
PathBuf::from("./profiles")
};
if !dir_path.is_dir() && !force {
eprintln!("Path is not a directory: {:?}", path);
return;
}


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