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

Add cli checks #3

Merged
merged 4 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 23 additions & 0 deletions .github/workflows/linters-cargo.yml
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
name: Task - Linters

on:
workflow_dispatch:
workflow_call:

jobs:
prettier:
anshalshukla marked this conversation as resolved.
Show resolved Hide resolved
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:
anshalshukla marked this conversation as resolved.
Show resolved Hide resolved
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
23 changes: 23 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions .github/workflows/rust-build.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"MD033": false,
"MD041": false,
"MD045": false,
"MD003": false,
"MD013": {
"code_blocks": false,
"line_length": 135
}
}
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ path = "src/main.rs"
clap = { version = "4.4.11", features = ["derive"] }
dirs = "5.0.1"
inquire = "0.6.2"
serde = { version = "1.0.193", features = ["derive"] }
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"
remove_dir_all = "0.8.2"
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Madara CLI

A CLI tool to quickly deploy Madara app chains.
9 changes: 3 additions & 6 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,9 @@ fn generate_config() -> Result<AppChainConfig, InitError> {

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::<Vec<_>>())?;
let da_layer = get_option("Select DA layer for your app chain:", DALayer::iter().collect::<Vec<_>>())?;
Expand Down Expand Up @@ -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!");
Expand Down
2 changes: 1 addition & 1 deletion src/cli/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn list() {
/// But return app names after validating the {app}-config.toml
pub fn get_apps_list() -> Result<Vec<String>, io::Error> {
let app_configs = get_app_chains_home()?;
let app_names: Vec<String> = match fs::read_dir(&app_configs) {
let app_names: Vec<String> = match fs::read_dir(app_configs) {
Ok(entries) => entries
.filter_map(|entry| {
entry.ok().and_then(|entry| {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
27 changes: 10 additions & 17 deletions src/utils/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn get_latest_commit_hash(org: &str, repo: &str) -> Result<String, GithubErr
let github_api_url = format!("{}/repos/{}/{}/commits", GITHUB_API_BASE_URL, org, repo);

let client = Client::new();
let response = client.get(&github_api_url).header("User-Agent", "reqwest").send();
let response = client.get(github_api_url).header("User-Agent", "reqwest").send();

return match response {
Ok(response) => match response.json::<Vec<Commit>>() {
Expand All @@ -33,23 +33,16 @@ pub fn get_latest_commit_hash(org: &str, repo: &str) -> Result<String, GithubErr
}

pub fn git_clone(url: &str, path: &PathBuf) -> 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() {
Expand All @@ -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)
};
}
}
2 changes: 1 addition & 1 deletion src/utils/madara.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<AppChainConfig>(&toml_content) {
Ok(app_chain_config) => Ok((app_chain_config, true)),
Expand Down
11 changes: 11 additions & 0 deletions taplo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include = ["**/*.toml"]
exclude = ["**/bad.toml"]

[formatting]
align_entries = false

[[rule]]
keys = ["dependencies"]

[rule.formatting]
reorder_keys = true
Loading