From 8738443f18c8e5cbc4b18df9812c4b9b338e0b30 Mon Sep 17 00:00:00 2001 From: Thomas DA ROCHA Date: Sat, 13 Aug 2022 02:10:08 +0200 Subject: [PATCH 01/76] Init project --- .gitignore | 1 + Cargo.lock | 381 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 26 ++++ README.md | 2 +- src/build.rs | 16 +++ src/cli.rs | 39 ++++++ src/config.rs | 0 src/dev.rs | 16 +++ src/main.rs | 17 +++ src/new.rs | 79 +++++++++++ src/start.rs | 17 +++ 11 files changed, 593 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/build.rs create mode 100644 src/cli.rs create mode 100644 src/config.rs create mode 100644 src/dev.rs create mode 100644 src/main.rs create mode 100644 src/new.rs create mode 100644 src/start.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6d4b219 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,381 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "3.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3dbbb6653e7c55cc8595ad3e1f7be8f32aba4eb7ff7f0fd1163d4f3d137c0a9" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ba52acd3b0a5c33aeada5cdaa3267cdc7c594a98731d4268cdc1532f4264cb4" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "dofigen_lib" +version = "0.0.0" +dependencies = [ + "serde", + "serde_json", + "serde_yaml", +] + +[[package]] +name = "env_logger" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "indexmap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" + +[[package]] +name = "lenra_cli" +version = "0.0.0" +dependencies = [ + "clap", + "dofigen_lib", + "env_logger", + "log", + "serde", + "serde_json", + "serde_yaml", +] + +[[package]] +name = "libc" +version = "0.2.129" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64de3cc433455c14174d42e554d4027ee631c4d046d43e3ecc6efc4636cdc7a7" + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "once_cell" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" + +[[package]] +name = "os_str_bytes" +version = "6.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "648001efe5d5c0102d8cea768e348da85d90af8ba91f0bea908f157951493cd4" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.143" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53e8e5d5b70924f74ff5c6d64d9a5acd91422117c60f48c4e07855238a254553" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.143" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3d8e8de557aee63c26b85b947f5e59b690d0454c753f3adeb5cd7835ab88391" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38dd04e3c8279e75b31ef29dbdceebfe5ad89f4d0937213c53f7d49d01b3d5a7" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "serde_yaml" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79b7c9017c64a49806c6e8df8ef99b92446d09c92457f85f91835b01a8064ae0" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" + +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" + +[[package]] +name = "unsafe-libyaml" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "931179334a56395bcf64ba5e0ff56781381c1a5832178280c7d7f91d1679aeb0" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..13fff5b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "lenra_cli" +version = "0.0.0" +edition = "2021" +license = "MIT" +description = "A Dockerfile generator using a simplified description in YAML or JSON format command line tool" +readme = "../README.md" +repository = "https://github.com/lenra-io/dofigen" +keywords = ["cli", "dockerfile", "generator", "yaml", "json"] +categories = ["command-line-utilities"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[[bin]] +edition = "2021" +name = "lenra" +path = "src/main.rs" + +[dependencies] +dofigen_lib = { path = "../dofigen/dofigen_lib", version = "0.0.0" } +clap = { version = "3.2", features = ["derive"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +serde_yaml = "0.9.2" +log = "0.4.17" +env_logger = "0.9.0" diff --git a/README.md b/README.md index 5fc8690..3513421 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# lenra-cli +# lenra_cli The Lenra's command line interface diff --git a/src/build.rs b/src/build.rs new file mode 100644 index 0000000..0ab79b8 --- /dev/null +++ b/src/build.rs @@ -0,0 +1,16 @@ +pub use clap::Args; + +use crate::cli::CliSubcommand; + +#[derive(Args)] +pub struct Build { + /// The app configuration file. + #[clap(parse(from_os_str), long, default_value = "lenra.config.yml")] + pub config: std::path::PathBuf, +} + +impl CliSubcommand for Build { + fn run(&self) { + todo!() + } +} \ No newline at end of file diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..4b4c16c --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,39 @@ +pub use clap::{Args, Parser, Subcommand}; + +use crate::{build::Build, dev::Dev, new::New, start::Start}; + +/// The Lenra CLI arguments to manage your local app development. +#[derive(Parser)] +#[clap(author, version, about, long_about = None)] +pub struct Cli { + #[clap(subcommand)] + pub command: Command, +} + +pub trait CliSubcommand { + fn run(&self); +} + +/// The subcommands +#[derive(Subcommand)] +pub enum Command { + /// Create a new Lenra app project + New(New), + /// Start your app in development mode + Dev(Dev), + /// Build your app in release mode + Build(Build), + /// Start your app previously built with the build command + Start(Start), +} + +impl CliSubcommand for Command { + fn run(&self) { + match self { + Command::New(new) => new.run(), + Command::Dev(dev) => dev.run(), + Command::Build(build) => build.run(), + Command::Start(start) => start.run(), + }; + } +} diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/dev.rs b/src/dev.rs new file mode 100644 index 0000000..e26a0cd --- /dev/null +++ b/src/dev.rs @@ -0,0 +1,16 @@ +pub use clap::Args; + +use crate::cli::CliSubcommand; + +#[derive(Args)] +pub struct Dev { + /// The app configuration file. + #[clap(parse(from_os_str), long, default_value = "lenra.config.yml")] + pub config: std::path::PathBuf, +} + +impl CliSubcommand for Dev { + fn run(&self) { + todo!() + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..961fe8e --- /dev/null +++ b/src/main.rs @@ -0,0 +1,17 @@ +use clap::Parser; +use cli::{Cli, CliSubcommand}; +use env_logger; + +mod build; +mod cli; +mod config; +mod dev; +mod new; +mod start; + +fn main() { + env_logger::init(); + let args = Cli::parse(); + // TODO: manage config file + args.command.run(); +} diff --git a/src/new.rs b/src/new.rs new file mode 100644 index 0000000..f8ca9df --- /dev/null +++ b/src/new.rs @@ -0,0 +1,79 @@ +pub use clap::Args; +use log; +use std::fs; +use std::process::Command; + +use crate::cli::CliSubcommand; + +#[derive(Args)] +pub struct New { + /// The project template + pub template: String, + + /// The target project path + #[clap(parse(from_os_str))] + path: std::path::PathBuf, + + /// The project name + #[clap(long)] + pub name: Option, +} + +impl CliSubcommand for New { + fn run(&self) { + if self.path.exists() { + panic!("The path '{}' already exists", self.path.display()) + } + let template = format!("https://github.com/lenra-io/template-{}", self.template); + let download_url = format!("{}/archive/refs/heads/main.zip", template); + let name = if let Some(n) = &self.name { + // TODO: check name pattern + n.clone() + } else { + String::from(self.path.file_name().unwrap().to_str().unwrap()) + }; + println!( + "new project {} from tempalte {}[{}]", + name, template, download_url + ); + + log::debug!("clone the template"); + match Command::new("git") + .arg("clone") + .arg(template) + .arg(self.path.as_os_str()) + .spawn() + { + Ok(child) => { + child.wait_with_output().expect("Failed to clone the template"); + } + Err(e) => match e.kind() { + std::io::ErrorKind::NotFound => panic!("`git` was not found!"), + _ => panic!("Some unkown error occurred"), + }, + } + + log::debug!("remove git directory"); + fs::remove_dir_all(self.path.join(".git")).unwrap(); + + log::debug!("init git project"); + Command::new("git") + .current_dir(self.path.as_os_str()) + .arg("init") + .output() + .expect("Failed initiating git project"); + Command::new("git") + .current_dir(self.path.as_os_str()) + .arg("add") + .arg(".") + .output() + .expect("Failed during git add"); + Command::new("git") + .current_dir(self.path.as_os_str()) + .arg("commit") + .arg("-m") + .arg("Init project") + .output() + .expect("Failed during git add"); + } +} diff --git a/src/start.rs b/src/start.rs new file mode 100644 index 0000000..8770a70 --- /dev/null +++ b/src/start.rs @@ -0,0 +1,17 @@ +pub use clap::Args; + +use crate::cli::CliSubcommand; + + +#[derive(Args)] +pub struct Start { + /// The app configuration file. + #[clap(parse(from_os_str), long, default_value = "lenra.config.yml")] + pub config: std::path::PathBuf, +} + +impl CliSubcommand for Start { + fn run(&self) { + todo!() + } +} \ No newline at end of file From c4127d0cb52d89c1de3473b262583abc460dfe18 Mon Sep 17 00:00:00 2001 From: Thomas DA ROCHA Date: Sat, 13 Aug 2022 14:33:32 +0200 Subject: [PATCH 02/76] feat: Template check --- Cargo.lock | 8 ++++++++ Cargo.toml | 2 ++ src/new.rs | 46 +++++++++++++++++++++++++++++++++++----------- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6d4b219..f64f211 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -144,6 +144,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "lenra_cli" version = "0.0.0" @@ -151,7 +157,9 @@ dependencies = [ "clap", "dofigen_lib", "env_logger", + "lazy_static", "log", + "regex", "serde", "serde_json", "serde_yaml", diff --git a/Cargo.toml b/Cargo.toml index 13fff5b..6c398f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,5 @@ serde_json = "1.0" serde_yaml = "0.9.2" log = "0.4.17" env_logger = "0.9.0" +regex = "1.6.0" +lazy_static = "1.4.0" diff --git a/src/new.rs b/src/new.rs index f8ca9df..8fc56f6 100644 --- a/src/new.rs +++ b/src/new.rs @@ -1,5 +1,7 @@ pub use clap::Args; +use lazy_static::lazy_static; use log; +use regex::Regex; use std::fs; use std::process::Command; @@ -7,10 +9,13 @@ use crate::cli::CliSubcommand; #[derive(Args)] pub struct New { - /// The project template + /// The project template from which your project will be created. + /// For example, defining `rust` or `template-rust` will use the next one: https://github.com/lenra-io/template-rust + /// You can find all our templates at this url: https://github.com/orgs/lenra-io/repositories?q=&type=template&language=&sort=stargazers + /// You also can set the template project full url to use custom ones. pub template: String, - /// The target project path + /// The project path #[clap(parse(from_os_str))] path: std::path::PathBuf, @@ -19,25 +24,42 @@ pub struct New { pub name: Option, } +lazy_static! { + static ref TEMPLATE_SHORT_REGEX: Regex = + Regex::new(r"^(template-)?([0-9a-zA-Z]+([_-][0-9a-zA-Z]+)*)$").unwrap(); + static ref NAME_REGEX: Regex = + Regex::new(r"^([0-9a-zA-Z]+([_-][0-9a-zA-Z]+)*)$").unwrap(); +} + impl CliSubcommand for New { fn run(&self) { if self.path.exists() { panic!("The path '{}' already exists", self.path.display()) } - let template = format!("https://github.com/lenra-io/template-{}", self.template); - let download_url = format!("{}/archive/refs/heads/main.zip", template); + + let template = if TEMPLATE_SHORT_REGEX.is_match(self.template.as_str()) { + format!( + "https://github.com/lenra-io/template-{}", + TEMPLATE_SHORT_REGEX.replace(self.template.as_str(), "$2") + ) + } else { + self.template.clone() + }; + let name = if let Some(n) = &self.name { - // TODO: check name pattern + if !NAME_REGEX.is_match(n) { + panic!("The specified name does not seems correct. Does not match the regex {}", NAME_REGEX.as_str()); + } n.clone() } else { String::from(self.path.file_name().unwrap().to_str().unwrap()) }; - println!( - "new project {} from tempalte {}[{}]", - name, template, download_url - ); - log::debug!("clone the template"); + log::debug!( + "clone the template {} into {}", + template, + self.path.display() + ); match Command::new("git") .arg("clone") .arg(template) @@ -45,7 +67,9 @@ impl CliSubcommand for New { .spawn() { Ok(child) => { - child.wait_with_output().expect("Failed to clone the template"); + child + .wait_with_output() + .expect("Failed to clone the template"); } Err(e) => match e.kind() { std::io::ErrorKind::NotFound => panic!("`git` was not found!"), From 33d99e257712fec0e9f9ee9e33b5db6dae0a3431 Mon Sep 17 00:00:00 2001 From: Thomas DA ROCHA Date: Sat, 13 Aug 2022 14:36:15 +0200 Subject: [PATCH 03/76] fix: Name not needed --- src/new.rs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/new.rs b/src/new.rs index 8fc56f6..95d377d 100644 --- a/src/new.rs +++ b/src/new.rs @@ -18,17 +18,11 @@ pub struct New { /// The project path #[clap(parse(from_os_str))] path: std::path::PathBuf, - - /// The project name - #[clap(long)] - pub name: Option, } lazy_static! { static ref TEMPLATE_SHORT_REGEX: Regex = Regex::new(r"^(template-)?([0-9a-zA-Z]+([_-][0-9a-zA-Z]+)*)$").unwrap(); - static ref NAME_REGEX: Regex = - Regex::new(r"^([0-9a-zA-Z]+([_-][0-9a-zA-Z]+)*)$").unwrap(); } impl CliSubcommand for New { @@ -45,15 +39,6 @@ impl CliSubcommand for New { } else { self.template.clone() }; - - let name = if let Some(n) = &self.name { - if !NAME_REGEX.is_match(n) { - panic!("The specified name does not seems correct. Does not match the regex {}", NAME_REGEX.as_str()); - } - n.clone() - } else { - String::from(self.path.file_name().unwrap().to_str().unwrap()) - }; log::debug!( "clone the template {} into {}", From 2ec4cca4be7f5669276b3f84b10410c8730de01c Mon Sep 17 00:00:00 2001 From: Thomas DA ROCHA Date: Sat, 13 Aug 2022 16:32:09 +0200 Subject: [PATCH 04/76] docs: First part --- Cargo.toml | 6 +-- README.md | 147 +++++++++++++++++++++++++++++++++++++++++++++++++- src/build.rs | 5 +- src/config.rs | 1 + src/dev.rs | 3 +- src/main.rs | 4 ++ src/new.rs | 4 ++ src/start.rs | 6 +-- 8 files changed, 165 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6c398f2..56ff0cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,10 +3,10 @@ name = "lenra_cli" version = "0.0.0" edition = "2021" license = "MIT" -description = "A Dockerfile generator using a simplified description in YAML or JSON format command line tool" +description = "THe Lenra's command line interface" readme = "../README.md" -repository = "https://github.com/lenra-io/dofigen" -keywords = ["cli", "dockerfile", "generator", "yaml", "json"] +repository = "https://github.com/lenra-io/lenra_cli" +keywords = ["cli", "lenra"] categories = ["command-line-utilities"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 3513421..fc41d56 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,145 @@ -# lenra_cli -The Lenra's command line interface +
+ + + + + + +[![Contributors][contributors-shield]][contributors-url] +[![Forks][forks-shield]][forks-url] +[![Stargazers][stars-shield]][stars-url] +[![Issues][issues-shield]][issues-url] +[![MIT License][license-shield]][license-url] + +# Lenra cli + +The Lenra's command line interface. + +[Report Bug](https://github.com/lenra-io/lenra_cli/issues) +ยท +[Request Feature](https://github.com/lenra-io/lenra_cli/issues) + + +## Getting Started + +### Prerequisites + +Install the Lenra's cli using one of the next possibilities. + +#### Cargo install + +First install Cargo, the Rust package manager: https://doc.rust-lang.org/cargo/getting-started/installation.html + +Then use the next command to install the Lenra's cli: + +```bash +cargo install lenra_cli +``` + +#### Download the binary + +You can download the binary from [the release page](https://github.com/lenra-io/lenra_cli/releases) and add it to your path environment variable. + +#### Use it with Docker + +You can run cli directly from it Docker image with the next command: + +```bash +docker run --rm -it -v $(pwd):/app lenra/cli +``` + +

(back to top)

+ +### How to use it + +Use the help options to understand how to use it: + +```bash +$ lenra --help +lenra_cli 0.0.0 +A Dockerfile generator using a simplified description in YAML or JSON format command line tool + +USAGE: + lenra + +OPTIONS: + -h, --help Print help information + -V, --version Print version information + +SUBCOMMANDS: + build Build your app in release mode + dev Start your app in development mode + help Print this message or the help of the given subcommand(s) + new Create a new Lenra app project + start Start your app previously built with the build command +``` + +### Configuration file + +The Lenra's configuration file `lenra.yml` describes your Lenra app configurations, like API versions or how to build it. + +#### Configuration + +The configuration is the main element of the file: + +| Field | Type | Description | +|------------------|------------------|-------------------------------| +| `componentsApi` | String | The components API version | + +

(back to top)

+ + +## Contributing + +Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. + +If you have a suggestion that would make this better, please open an issue with the tag "enhancement" or "bug". +Don't forget to give the project a star! Thanks again! + +

(back to top)

+ + + + +## License + +Distributed under the **MIT** License. See [LICENSE](./LICENSE) for more information. + +

(back to top)

+ + + + +## Contact + +Lenra - [@lenra_dev](https://twitter.com/lenra_dev) - contact@lenra.io + +Project Link: [https://github.com/lenra-io/lenra_cli](https://github.com/lenra-io/lenra_cli) + +

(back to top)

+ + + + +[contributors-shield]: https://img.shields.io/github/contributors/lenra-io/lenra_cli.svg?style=for-the-badge +[contributors-url]: https://github.com/lenra-io/lenra_cli/graphs/contributors +[forks-shield]: https://img.shields.io/github/forks/lenra-io/lenra_cli.svg?style=for-the-badge +[forks-url]: https://github.com/lenra-io/lenra_cli/network/members +[stars-shield]: https://img.shields.io/github/stars/lenra-io/lenra_cli.svg?style=for-the-badge +[stars-url]: https://github.com/lenra-io/lenra_cli/stargazers +[issues-shield]: https://img.shields.io/github/issues/lenra-io/lenra_cli.svg?style=for-the-badge +[issues-url]: https://github.com/lenra-io/lenra_cli/issues +[license-shield]: https://img.shields.io/github/license/lenra-io/lenra_cli.svg?style=for-the-badge +[license-url]: https://github.com/lenra-io/lenra_cli/blob/master/LICENSE.txt diff --git a/src/build.rs b/src/build.rs index 0ab79b8..a00496f 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1,11 +1,12 @@ pub use clap::Args; use crate::cli::CliSubcommand; +use crate::config::DEFAULT_CONFIG_FILE; #[derive(Args)] pub struct Build { /// The app configuration file. - #[clap(parse(from_os_str), long, default_value = "lenra.config.yml")] + #[clap(parse(from_os_str), long, default_value = DEFAULT_CONFIG_FILE)] pub config: std::path::PathBuf, } @@ -13,4 +14,4 @@ impl CliSubcommand for Build { fn run(&self) { todo!() } -} \ No newline at end of file +} diff --git a/src/config.rs b/src/config.rs index e69de29..d35bc51 100644 --- a/src/config.rs +++ b/src/config.rs @@ -0,0 +1 @@ +pub static DEFAULT_CONFIG_FILE: &str = "lenra.yml"; diff --git a/src/dev.rs b/src/dev.rs index e26a0cd..745d3b1 100644 --- a/src/dev.rs +++ b/src/dev.rs @@ -1,11 +1,12 @@ pub use clap::Args; use crate::cli::CliSubcommand; +use crate::config::DEFAULT_CONFIG_FILE; #[derive(Args)] pub struct Dev { /// The app configuration file. - #[clap(parse(from_os_str), long, default_value = "lenra.config.yml")] + #[clap(parse(from_os_str), long, default_value = DEFAULT_CONFIG_FILE)] pub config: std::path::PathBuf, } diff --git a/src/main.rs b/src/main.rs index 961fe8e..3266afa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,7 @@ +//! # lenra_cli +//! +//! The Lenra's command line interface + use clap::Parser; use cli::{Cli, CliSubcommand}; use env_logger; diff --git a/src/new.rs b/src/new.rs index 95d377d..6371714 100644 --- a/src/new.rs +++ b/src/new.rs @@ -1,3 +1,7 @@ +//! # new +//! +//! The new subcommand creates a new Lenra app project from a given template and into a given path + pub use clap::Args; use lazy_static::lazy_static; use log; diff --git a/src/start.rs b/src/start.rs index 8770a70..dfe0c8f 100644 --- a/src/start.rs +++ b/src/start.rs @@ -1,12 +1,12 @@ pub use clap::Args; use crate::cli::CliSubcommand; - +use crate::config::DEFAULT_CONFIG_FILE; #[derive(Args)] pub struct Start { /// The app configuration file. - #[clap(parse(from_os_str), long, default_value = "lenra.config.yml")] + #[clap(parse(from_os_str), long, default_value = DEFAULT_CONFIG_FILE)] pub config: std::path::PathBuf, } @@ -14,4 +14,4 @@ impl CliSubcommand for Start { fn run(&self) { todo!() } -} \ No newline at end of file +} From 72555b4bb10347d67afae09882c6e30a4985f1b1 Mon Sep 17 00:00:00 2001 From: Thomas DA ROCHA Date: Sun, 14 Aug 2022 01:45:50 +0200 Subject: [PATCH 05/76] feat: Only the last commit of the main branch --- src/main.rs | 2 +- src/new.rs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3266afa..c26fbe0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ //! # lenra_cli //! //! The Lenra's command line interface - + use clap::Parser; use cli::{Cli, CliSubcommand}; use env_logger; diff --git a/src/new.rs b/src/new.rs index 6371714..1e29cab 100644 --- a/src/new.rs +++ b/src/new.rs @@ -1,7 +1,7 @@ //! # new //! //! The new subcommand creates a new Lenra app project from a given template and into a given path - + pub use clap::Args; use lazy_static::lazy_static; use log; @@ -51,6 +51,9 @@ impl CliSubcommand for New { ); match Command::new("git") .arg("clone") + .arg("--single-branch") + .arg("--depth") + .arg("1") .arg(template) .arg(self.path.as_os_str()) .spawn() From b4ebd7a24b67902bf7855975c2847830dd90b549 Mon Sep 17 00:00:00 2001 From: Thomas DA ROCHA <8969556+taorepoara@users.noreply.github.com> Date: Thu, 18 Aug 2022 15:21:12 +0200 Subject: [PATCH 06/76] feat: Implement build command (#4) --- Cargo.lock | 54 ++++++++--- Cargo.toml | 2 +- README.md | 82 +++++++++++++++- src/build.rs | 259 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/cli.rs | 9 +- src/config.rs | 64 +++++++++++++ src/dev.rs | 17 ---- src/main.rs | 4 +- src/new.rs | 4 +- src/start.rs | 4 +- 10 files changed, 445 insertions(+), 54 deletions(-) delete mode 100644 src/dev.rs diff --git a/Cargo.lock b/Cargo.lock index f64f211..4bf7809 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -42,9 +42,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.2.16" +version = "3.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3dbbb6653e7c55cc8595ad3e1f7be8f32aba4eb7ff7f0fd1163d4f3d137c0a9" +checksum = "29e724a68d9319343bb3328c9cc2dfde263f4b3142ee1059a9980580171c954b" dependencies = [ "atty", "bitflags", @@ -59,9 +59,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "3.2.15" +version = "3.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ba52acd3b0a5c33aeada5cdaa3267cdc7c594a98731d4268cdc1532f4264cb4" +checksum = "13547f7012c01ab4a0e8f8967730ada8f9fdf419e8b6c792788f39cf4e46eefa" dependencies = [ "heck", "proc-macro-error", @@ -80,12 +80,16 @@ dependencies = [ ] [[package]] -name = "dofigen_lib" -version = "0.0.0" +name = "dofigen" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71144688ed025d3b62fa7f2d24f6f72891843c49976869c6d4b9927490ce9360" dependencies = [ + "clap", "serde", "serde_json", "serde_yaml", + "thiserror", ] [[package]] @@ -155,7 +159,7 @@ name = "lenra_cli" version = "0.0.0" dependencies = [ "clap", - "dofigen_lib", + "dofigen", "env_logger", "lazy_static", "log", @@ -167,9 +171,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.129" +version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64de3cc433455c14174d42e554d4027ee631c4d046d43e3ecc6efc4636cdc7a7" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" [[package]] name = "log" @@ -188,15 +192,15 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "once_cell" -version = "1.13.0" +version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" +checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" [[package]] name = "os_str_bytes" -version = "6.2.0" +version = "6.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "648001efe5d5c0102d8cea768e348da85d90af8ba91f0bea908f157951493cd4" +checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" [[package]] name = "proc-macro-error" @@ -296,9 +300,9 @@ dependencies = [ [[package]] name = "serde_yaml" -version = "0.9.4" +version = "0.9.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79b7c9017c64a49806c6e8df8ef99b92446d09c92457f85f91835b01a8064ae0" +checksum = "f50845f68d5c693aac7d72a25415ddd21cb8182c04eafe447b73af55a05f9e1b" dependencies = [ "indexmap", "itoa", @@ -339,6 +343,26 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" +[[package]] +name = "thiserror" +version = "1.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5f6586b7f764adc0231f4c79be7b920e766bb2f3e51b3661cdb263828f19994" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12bafc5b54507e0149cdf1b145a5d80ab80a90bcd9275df43d4fff68460f6c21" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "unicode-ident" version = "1.0.3" diff --git a/Cargo.toml b/Cargo.toml index 56ff0cd..6b64d92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ name = "lenra" path = "src/main.rs" [dependencies] -dofigen_lib = { path = "../dofigen/dofigen_lib", version = "0.0.0" } clap = { version = "3.2", features = ["derive"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" @@ -26,3 +25,4 @@ log = "0.4.17" env_logger = "0.9.0" regex = "1.6.0" lazy_static = "1.4.0" +dofigen = "1" diff --git a/README.md b/README.md index fc41d56..0242657 100644 --- a/README.md +++ b/README.md @@ -80,15 +80,73 @@ OPTIONS: SUBCOMMANDS: build Build your app in release mode - dev Start your app in development mode help Print this message or the help of the given subcommand(s) new Create a new Lenra app project start Start your app previously built with the build command ``` +### Subcommands + +This tools contains many subcommands to help you doing what you need. + +- [new](#new): creates a new Lenra app project +- [build](#build): builds the Lenra app of the current directory + +#### new + +This subcommand creates a new Lenra app project from a given template and in a given directory. +The target directory must not exist. + +```bash +$ lenra new --help +lenra-new +Create a new Lenra app project + +USAGE: + lenra new