From 114f307facf5321ca648ac007d42dd4b9ac2defa Mon Sep 17 00:00:00 2001 From: mateuszpiorowski Date: Thu, 14 Mar 2024 10:53:14 +0100 Subject: [PATCH] update --- .github/workflows/lint.yml | 17 ++++++ Cargo.toml | 2 +- README.md | 2 +- src/config.rs | 12 +++- src/main.rs | 114 ++++++++++--------------------------- src/opts.rs | 9 +-- 6 files changed, 62 insertions(+), 94 deletions(-) create mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..6e600dc --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,17 @@ +name: Lint +on: + pull_request: + push: + branches: + - main + +env: + RUSTFLAGS: "-Dwarnings" + +jobs: + clippy_check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Run Clippy + run: cargo clippy --all-targets --all-features diff --git a/Cargo.toml b/Cargo.toml index fcc6a49..103cce5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "templates" -version = "0.8.0" +version = "0.9.0" edition = "2021" authors = ["Mateusz PiĆ³rowski "] license = "MIT" diff --git a/README.md b/README.md index b82c460..997ed98 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Templates CLI +# Template CLI Have You ever wanted to create new NextJS route which let You use query parameters using `useSearchParams` hook? Or insert a server side page exporting load and action functions for SvelteKit with new `satisfies` operator? diff --git a/src/config.rs b/src/config.rs index 270178d..0206155 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,6 +10,7 @@ use std::{ pub struct Config { pub config_path: PathBuf, pub templates_path: PathBuf, + pub clipboard_command: String, } impl Config { @@ -29,6 +30,7 @@ impl Config { return Ok(Config { config_path, templates_path: PathBuf::from("~/templates"), + clipboard_command: "xclip".to_string(), }); } // If config file exists, read it @@ -40,9 +42,15 @@ impl Config { .and_then(|v| v.as_str()) .map(|v| PathBuf::from(v)) .context("Templates folder path not found in config")?; - return Ok(Config { + let clipboard_command = config_json + .get("clipboard_command") + .and_then(|v| v.as_str()) + .map(|v| v.to_string()) + .unwrap_or("xclip".to_string()); + Ok(Config { config_path, templates_path, - }); + clipboard_command, + }) } } diff --git a/src/main.rs b/src/main.rs index a60a685..d9240bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use anyhow::{Context, Ok, Result}; use clap::Parser; use serde_json::{from_str, to_string, to_string_pretty, Value}; +use std::io::Write; use std::{fs, path::PathBuf}; use templates::{ config::Config, @@ -14,8 +15,9 @@ fn main() -> Result<()> { match opts.action { Action::Set(val) => { println!("Setting templates path to {:?}", val.path); + println!("Setting clipboard command to {:?}", val.clipboard); check_folder(&val.path)?; - set_templates_path(&val.path, &config.config_path)?; + set_config(&val.path, &val.clipboard, &config.config_path)?; } Action::Show(val) => { let page = &val.page; @@ -29,26 +31,24 @@ fn main() -> Result<()> { println!("{}", template_content); } Action::Copy(val) => { - let pages = &val.pages; + let page = &val.page; let project = &val.project.unwrap_or("".to_string()); - check_folder(&config.templates_path.join(project))?; - - let working_path = match val.path { - Some(path) => path, - None => PathBuf::from("."), - }; - for page in pages { - let template = find_template_file(&config.templates_path.join(project), page)? + + let template = find_template_file(&config.templates_path.join(project), page)? .context(format!("Template not found. Create it in the templates folder with the format [{page}]filename", page = page))?; - println!( - "Copying {:?} to {:?}", - template.path, - working_path.join(&template.name) - ); - fs::create_dir_all(&working_path).context("Folder not created")?; - fs::copy(&template.path, &working_path.join(template.name)) - .context("File not copied")?; - } + let template_path = &template.path; + let template_content = fs::read_to_string(&template_path).context("File not found")?; + + let mut child = std::process::Command::new(config.clipboard_command) + .stdin(std::process::Stdio::piped()) + .spawn() + .context("Clipboard not found")?; + child + .stdin + .as_mut() + .unwrap() + .write_all(template_content.as_bytes()) + .context("Clipboard not written")?; } Action::Var(val) => { let project_path = PathBuf::from(val.project.unwrap_or("".to_string())); @@ -65,64 +65,10 @@ fn main() -> Result<()> { } } Ok(()) - - // let setup = Setup::try_from(Opts::parse())?; - // let templates_path = &setup.config.templates_path; - // let path = &setup.path; - // match setup.action { - // Action::Set(val) => { - // println!("Setting templates path to {:?}", val.path); - // set_templates_path(&val.path, &setup.config.config_path)?; - // } - // Action::Var(val) => { - // println!("Replacing variables in {:?}", val.path); - // let var_file: &PathBuf = &templates_path.join(&val.project).join("var"); - // check_file(var_file)?; - // replace_template_variables(&val.path, var_file)?; - // } - // Action::Copy(copy) => { - // let project = ©.project; - // let page = ©.page; - - // let template = find_template_file(&templates_path.join(project), page)? - // .context( - // anyhow!("Template not found. Create it in the templates folder with the format [{page}]name") - // )?; - // let template_path = &template.path; - // let template_content = fs::read_to_string(&template_path).context("File not found")?; - // println!("Copying {:?}", template_path); - // println!("{}", template_content); - // } - // Action::Use(val) => { - // let project = &val.project; - // let pages = &val.pages; - // check_folder(&templates_path.join(project))?; - - // for page in pages { - // let template = find_template_file(&templates_path.join(project), page)? - // .context(anyhow!("Template not found. Create it in the templates folder with the format [{page}]name"))?; - // println!( - // "Copying {:?} to {:?}", - // template.path, - // path.join(&template.name) - // ); - // fs::create_dir_all(&path).context("Folder not created")?; - // fs::copy(&template.path, &path.join(template.name)).context("File not copied")?; - // } - // } - // Action::List => { - // list_templates(&templates_path)?; - // } - // Action::Config => { - // println!("{:?}", setup); - // } - // } - // Ok(()) } #[derive(Debug)] struct Template { - name: String, path: PathBuf, } @@ -131,18 +77,22 @@ struct Template { * @param path Path to the templates folder * @return Result */ -fn set_templates_path(path: &PathBuf, config_path: &PathBuf) -> Result<()> { +fn set_config(path: &PathBuf, clipboard: &str, config_path: &PathBuf) -> Result<()> { // templates path - let templates_str = &path.to_str().context("Path not valid")?; - let templates_json = to_string(&templates_str).context("Json not valid")?; + let templates_json: serde_json::Value = + from_str(&to_string(path).context("Json not valid")?) + .with_context(|| format!("Json not valid: {:?}", path))?; + let clipboard_json: serde_json::Value = + from_str(&to_string(clipboard).context("Json not valid")?) + .with_context(|| format!("Json not valid: {:?}", clipboard))?; // read config let mut config_string = std::fs::read_to_string(&config_path).context("Config not found")?; let mut config_json: Value = from_str(&config_string).context("Config not valid")?; // write config - config_json["templates_path"] = from_str(&templates_json) - .with_context(|| format!("Json not valid: {:?}", templates_json))?; + config_json["templates_path"] = templates_json; + config_json["clipboard_command"] = clipboard_json; config_string = to_string_pretty(&config_json) .with_context(|| format!("Config not valid {:?}", config_string))?; @@ -150,7 +100,7 @@ fn set_templates_path(path: &PathBuf, config_path: &PathBuf) -> Result<()> { fs::write(&config_path, &config_string) .with_context(|| format!("Config not written to {:?}", config_path))?; - return Ok(()); + Ok(()) } /** @@ -170,14 +120,12 @@ fn find_template_file(project_path: &PathBuf, page: &str) -> Result Result<()> { @@ -225,5 +173,5 @@ fn list_templates(templates_path: &PathBuf) -> Result<()> { } } - return Ok(()); + Ok(()) } diff --git a/src/opts.rs b/src/opts.rs index 0e125bb..6321d19 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -29,23 +29,18 @@ pub enum Action { #[derive(Args, Debug, PartialEq)] pub struct Set { pub path: PathBuf, + pub clipboard: String, } #[derive(Args, Debug, PartialEq)] pub struct Show { pub page: String, - pub project: Option, } #[derive(Args, Debug, PartialEq)] pub struct Copy { - pub pages: Vec, - - #[arg(short = 'p', long = "path")] - pub path: Option, - - #[arg(last = true)] + pub page: String, pub project: Option, }