From 6bb41e0411bb579c4b4628c948dd202cf23889b1 Mon Sep 17 00:00:00 2001 From: apoorvsadana <95699312+apoorvsadana@users.noreply.github.com> Date: Mon, 1 Jan 2024 12:30:30 +0530 Subject: [PATCH] Add cli checks (#3) * add: cli checks and readme --- .github/workflows/linters-cargo.yml | 23 ++++++++++++++++++++ .github/workflows/linters.yml | 33 +++++++++++++++++++++++++++++ .github/workflows/pull-request.yml | 23 ++++++++++++++++++++ .github/workflows/rust-build.yml | 22 +++++++++++++++++++ .markdownlint.json | 10 +++++++++ Cargo.toml | 12 +++++------ README.md | 3 +++ src/cli/init.rs | 9 +++----- src/cli/list.rs | 2 +- src/cli/run.rs | 2 +- src/utils/github.rs | 27 +++++++++-------------- src/utils/madara.rs | 2 +- src/utils/toml.rs | 4 ++-- taplo.toml | 11 ++++++++++ 14 files changed, 149 insertions(+), 34 deletions(-) create mode 100644 .github/workflows/linters-cargo.yml create mode 100644 .github/workflows/linters.yml create mode 100644 .github/workflows/pull-request.yml create mode 100644 .github/workflows/rust-build.yml create mode 100644 .markdownlint.json create mode 100644 README.md create mode 100644 taplo.toml diff --git a/.github/workflows/linters-cargo.yml b/.github/workflows/linters-cargo.yml new file mode 100644 index 0000000..9617223 --- /dev/null +++ b/.github/workflows/linters-cargo.yml @@ -0,0 +1,23 @@ +--- +name: Task - Linters Cargo + +on: + workflow_dispatch: + workflow_call: + +jobs: + cargo-lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + # selecting a toolchain either by action or manual `rustup` calls should happen + # before the plugin, as the cache uses the current rustc version as its cache key + - run: rustup show + + - uses: Swatinem/rust-cache@v2 + - name: Format and clippy + run: | + cargo fmt -- --check + cargo clippy --no-deps -- -D warnings + cargo clippy --tests --no-deps -- -D warnings diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml new file mode 100644 index 0000000..966b7cf --- /dev/null +++ b/.github/workflows/linters.yml @@ -0,0 +1,33 @@ +--- +name: Task - Linters + +on: + workflow_dispatch: + workflow_call: + +jobs: + prettier: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run prettier + run: |- + npx prettier --check . + + markdown-lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: avto-dev/markdown-lint@v1 + with: + config: "./.markdownlint.json" + args: "." + ignore: "./target" + + toml-lint: + runs-on: ubuntu-latest + steps: + - name: Checkout toml files + uses: actions/checkout@v3 + - name: Run toml check + run: npx @taplo/cli fmt --config ./taplo.toml --check diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml new file mode 100644 index 0000000..93081bc --- /dev/null +++ b/.github/workflows/pull-request.yml @@ -0,0 +1,23 @@ +--- +name: Workflow - Pull Request + +on: + workflow_dispatch: + pull_request: + branches: [main] + push: + branches: [main] + +jobs: + linters: + name: Run linters + uses: ./.github/workflows/linters.yml + + rust_build: + name: Build Rust project + uses: ./.github/workflows/rust-build.yml + + linters_cargo: + name: Run Cargo linters + uses: ./.github/workflows/linters-cargo.yml + needs: rust_build diff --git a/.github/workflows/rust-build.yml b/.github/workflows/rust-build.yml new file mode 100644 index 0000000..f7cb3e0 --- /dev/null +++ b/.github/workflows/rust-build.yml @@ -0,0 +1,22 @@ +--- +name: Task - Build Rust + +on: + workflow_dispatch: + workflow_call: + +jobs: + rust_build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + # selecting a toolchain either by action or manual `rustup` calls should happen + # before the plugin, as the cache uses the current rustc version as its cache key + - run: rustup show + + - uses: Swatinem/rust-cache@v2 + + - name: Build the project + run: | + cargo build --release --workspace diff --git a/.markdownlint.json b/.markdownlint.json new file mode 100644 index 0000000..6b3fcec --- /dev/null +++ b/.markdownlint.json @@ -0,0 +1,10 @@ +{ + "MD033": false, + "MD041": false, + "MD045": false, + "MD003": false, + "MD013": { + "code_blocks": false, + "line_length": 135 + } +} diff --git a/Cargo.toml b/Cargo.toml index 6eb65ba..b93be91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,13 +12,13 @@ path = "src/main.rs" [dependencies] clap = { version = "4.4.11", features = ["derive"] } dirs = "5.0.1" +git2 = "0.18.1" inquire = "0.6.2" -serde = { version = "1.0.193", features = ["derive"] } +log = "0.4.20" +remove_dir_all = "0.8.2" +reqwest = { version = "0.11.23", features = ["json", "blocking"] } +serde = { version = "1.0.193", features = ["derive"] } strum = { version = "0.25.0", features = ["derive"] } strum_macros = { version = "0.25.3", features = [] } -toml = "0.8.8" -reqwest = { version = "0.11.23", features = ["json", "blocking"] } thiserror = "1.0.52" -log = "0.4.20" -git2 = "0.18.1" -remove_dir_all = "0.8.2" \ No newline at end of file +toml = "0.8.8" diff --git a/README.md b/README.md new file mode 100644 index 0000000..2bd4a85 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Madara CLI + +A CLI tool to quickly deploy Madara app chains. diff --git a/src/cli/init.rs b/src/cli/init.rs index 498345d..b1204ab 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -44,12 +44,9 @@ fn generate_config() -> Result { let app_chains_home = get_app_chains_home()?; let binding = app_chains_home.join(format!("{}/data", app_chain)); - let default_base_path = match binding.to_str() { - Some(path_str) => path_str, - None => "madara-data", - }; + let default_base_path = binding.to_str().unwrap_or("madara-data"); - let base_path = get_text_input("Enter base path for data directory of your app chain:", Some(&default_base_path))?; + let base_path = get_text_input("Enter base path for data directory of your app chain:", Some(default_base_path))?; let chain_id = get_text_input("Enter chain id for your app chain:", Some("MADARA"))?; let mode = get_option("Select mode for your app chain:", RollupMode::iter().collect::>())?; let da_layer = get_option("Select DA layer for your app chain:", DALayer::iter().collect::>())?; @@ -80,7 +77,7 @@ fn write_config(config: &AppChainConfig) -> Result<(), InitError> { let app_home = get_app_home(&config.app_chain)?; let full_file_path = app_home.join(config_file); - if let Err(err) = fs::write(&full_file_path, toml) { + if let Err(err) = fs::write(full_file_path, toml) { panic!("Error writing to file: {}", err); } else { log::info!("Data written to file successfully!"); diff --git a/src/cli/list.rs b/src/cli/list.rs index 9981bff..fd41eeb 100644 --- a/src/cli/list.rs +++ b/src/cli/list.rs @@ -18,7 +18,7 @@ pub fn list() { /// But return app names after validating the {app}-config.toml pub fn get_apps_list() -> Result, io::Error> { let app_configs = get_app_chains_home()?; - let app_names: Vec = match fs::read_dir(&app_configs) { + let app_names: Vec = match fs::read_dir(app_configs) { Ok(entries) => entries .filter_map(|entry| { entry.ok().and_then(|entry| { diff --git a/src/cli/run.rs b/src/cli/run.rs index c0216e6..e60d74a 100644 --- a/src/cli/run.rs +++ b/src/cli/run.rs @@ -32,7 +32,7 @@ fn start_app_chain() -> Result<(), RunError> { let app_chain: &str = &app; madara::clone_madara_and_build_repo()?; - madara::setup_and_run_madara(&app_chain)?; + madara::setup_and_run_madara(app_chain)?; Ok(()) } diff --git a/src/utils/github.rs b/src/utils/github.rs index 49025ea..1d37566 100644 --- a/src/utils/github.rs +++ b/src/utils/github.rs @@ -18,7 +18,7 @@ pub fn get_latest_commit_hash(org: &str, repo: &str) -> Result match response.json::>() { @@ -33,23 +33,16 @@ pub fn get_latest_commit_hash(org: &str, repo: &str) -> Result Result<(), GithubError> { - match Repository::open(path) { - Ok(repo) => { - // Check if the repository is valid - if repo.is_empty() == Ok(false) { - let remote = repo.find_remote("origin")?; - match remote.url() { - Some(remote_url) => { - if remote_url == url { - return Ok(()); - } - } - None => (), + if let Ok(repo) = Repository::open(path) { + // Check if the repository is valid + if repo.is_empty() == Ok(false) { + let remote = repo.find_remote("origin")?; + if let Some(remote_url) = remote.url() { + if remote_url == url { + return Ok(()); } } } - // We will clone the repo - Err(_) => {} } if path.exists() { @@ -69,11 +62,11 @@ pub fn git_clone(url: &str, path: &PathBuf) -> Result<(), GithubError> { let status = output.status; - return if status.success() { + if status.success() { log::info!("Clone successful!"); Ok(()) } else { log::error!("Clone failed"); Err(GithubError::FailedToCloneRepo) - }; + } } diff --git a/src/utils/madara.rs b/src/utils/madara.rs index 612f0a4..df50969 100644 --- a/src/utils/madara.rs +++ b/src/utils/madara.rs @@ -27,7 +27,7 @@ pub fn clone_madara_and_build_repo() -> Result<(), MadaraError> { pub fn setup_and_run_madara(app_chain: &str) -> Result<(), MadaraError> { let madara_path = get_madara_home()?.join("madara"); - let (config, _) = match regenerate_app_config(&app_chain) { + let (config, _) = match regenerate_app_config(app_chain) { Ok((config, valid)) => (config, valid), Err(err) => { log::error!("Failed to fetch the required app chain: {}", err); diff --git a/src/utils/toml.rs b/src/utils/toml.rs index 6011fb6..040fa49 100644 --- a/src/utils/toml.rs +++ b/src/utils/toml.rs @@ -5,9 +5,9 @@ use crate::utils::errors::TomlError; use crate::utils::paths::get_app_home; pub fn regenerate_app_config(app: &str) -> Result<(AppChainConfig, bool), TomlError> { - let app_home = get_app_home(&app)?; + let app_home = get_app_home(app)?; let app_config = app_home.join(format!("{}-config.toml", app)); - let toml_content = fs::read_to_string(&app_config)?; + let toml_content = fs::read_to_string(app_config)?; match toml::from_str::(&toml_content) { Ok(app_chain_config) => Ok((app_chain_config, true)), diff --git a/taplo.toml b/taplo.toml new file mode 100644 index 0000000..e49b662 --- /dev/null +++ b/taplo.toml @@ -0,0 +1,11 @@ +include = ["**/*.toml"] +exclude = ["**/bad.toml"] + +[formatting] +align_entries = false + +[[rule]] +keys = ["dependencies"] + +[rule.formatting] +reorder_keys = true