Skip to content

Commit

Permalink
cargo format
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantinos Athanasiou committed Dec 25, 2024
1 parent 7851534 commit 4e17566
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 92 deletions.
48 changes: 32 additions & 16 deletions protypo-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use anyhow::{anyhow, Error};
use clap::Parser;
use clap_derive::Subcommand;
use jsonptr::Pointer;
use protypo::Generator;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::fs;
use std::fs::File;
use std::path::{Path, PathBuf};
use tracing::{debug, error, info};
use tracing_subscriber::EnvFilter;
use protypo::{Generator};

#[derive(Parser, Debug)]
#[command(version, about="CLI application for downloading and running tera and rrgen templates", long_about = None)]
Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct Generate {

impl Default for Generate {
fn default() -> Generate {
Generate{
Generate {
output: ".".to_string(),
}
}
Expand All @@ -55,7 +55,7 @@ enum Commands {
/// use generator to create output
Generate {
/// path to generator
#[arg(short='p',long)]
#[arg(short = 'p', long)]
generator_path: Option<PathBuf>,
/// the name of the generator
#[arg(short, long, conflicts_with = "uri")]
Expand All @@ -64,11 +64,11 @@ enum Commands {
#[arg(short, long, conflicts_with = "uri")]
version: Option<String>,
/// uri to download and use generator
#[arg(short='u', long, conflicts_with = "name", conflicts_with = "version")]
#[arg(short = 'u', long, conflicts_with = "name", conflicts_with = "version")]
uri: Option<String>,
#[arg(long = "set")]
sets: Vec<String>,
}
},
}

#[tokio::main]
Expand All @@ -81,14 +81,23 @@ async fn main() -> Result<(), Error> {
let local_repo = dirs::data_local_dir().unwrap().join("protypo");
info!("directory for protypo config and data: {:?}!", local_repo);
let local_repo_generators = local_repo.join("generators");
info!("directory for installing templates: {:?}!", local_repo_generators);
info!(
"directory for installing templates: {:?}!",
local_repo_generators
);
match &cli.command {
Commands::New { name } => {
info!("Creating new template: {name}");
create_new_template(name);
Ok(())
},
Commands::Generate { name,version,uri, generator_path, sets} => {
}
Commands::Generate {
name,
version,
uri,
generator_path,
sets,
} => {
let mut values = json!({});
for set in sets {
let parts: Vec<&str> = set.splitn(2, '=').collect();
Expand All @@ -99,7 +108,7 @@ async fn main() -> Result<(), Error> {
let key = parts[0];
let val = parts[1];

let ptr_path= key.replace(".","/");
let ptr_path = key.replace(".", "/");
let ptr = Pointer::parse(ptr_path.as_str()).unwrap();
let replaced = ptr.assign(&mut values, json!(val)).unwrap().unwrap();
values = replaced;
Expand All @@ -109,15 +118,19 @@ async fn main() -> Result<(), Error> {
true if name.is_some() && version.is_some() => {
let generator_name = name.clone().unwrap();
let generator_version = version.clone().unwrap();
local_repo.join("generators").join(generator_name).join(generator_version)
},
local_repo
.join("generators")
.join(generator_name)
.join(generator_version)
}
true if generator_path.is_some() => {
let path = generator_path.clone().unwrap();
debug!("Searching for generator in path: {} ", path.display());
path
}
_ => {
let error_message = "Error: Either generator name and version or a URI must be provided.";
let error_message =
"Error: Either generator name and version or a URI must be provided.";
error!(error_message);
return Err(anyhow!(error_message));
}
Expand All @@ -131,8 +144,7 @@ async fn main() -> Result<(), Error> {
generator.generate(&ctx)?;

Ok(())
},

}
}
}

Expand All @@ -155,7 +167,11 @@ fn create_new_template(name: &str) {
};

let metadata_yaml = serde_yaml::to_string(&metadata).unwrap();
serde_yaml::to_writer(File::create(&format!("{}/template.yaml", package_dir)).unwrap(), &metadata_yaml).unwrap();
serde_yaml::to_writer(
File::create(&format!("{}/template.yaml", package_dir)).unwrap(),
&metadata_yaml,
)
.unwrap();
fs::create_dir_all(&package_dir).unwrap();

// Create the files
Expand All @@ -177,4 +193,4 @@ fn create_file(path: &str, _content: &str) {
// let file = File::create(path).unwrap();
// file.write_all(content.as_bytes()).unwrap();
println!("Created file: {}", path);
}
}
Loading

0 comments on commit 4e17566

Please sign in to comment.