Skip to content

Commit

Permalink
feat: deploy command (#5)
Browse files Browse the repository at this point in the history
* impl in progress

* impl in progress

* default project config file generation + deploy impl continued on both cli and lib side

* impl in progress deploy done with small leftover

* deployment command ready

* small updates

* make config values kebab case

* small updates

* small updates

* small update

* changed dependencies of tari repo

* cargo update
  • Loading branch information
ksrichard authored Dec 4, 2024
1 parent 21a698f commit 133d7b4
Show file tree
Hide file tree
Showing 28 changed files with 6,619 additions and 950 deletions.
6,411 changes: 5,592 additions & 819 deletions Cargo.lock

Large diffs are not rendered by default.

34 changes: 16 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
[package]
name = "tari_cli"
[workspace]
members = ["crates/tari_deploy", "crates/cli"]
resolver = "2"

[workspace.package]
version = "0.1.0"
edition = "2021"
authors = ["The Tari Development Community"]
repository = "https://github.com/tari-project/tari-cli"
license = "BSD-3-Clause"

[[bin]]
name = "tari"
path = "src/main.rs"

[dependencies]
anyhow = "1.0.93"
clap = { version = "4.5.20", features = ["derive", "string"] }
dirs-next = "2.0.0"
[workspace.dependencies]
tokio = { version = "1.41.1", features = ["full"] }
toml = "0.8.19"
serde = { version = "1.0.215", features = ["derive"] }
termimad = "0.31.0"
cargo-generate = "^0.22.0"
git2 = { version = "^0.19", features = ["default"] }
thiserror = "2.0.3"
spinners = "4.1.1"
convert_case = "0.6.0"
dialoguer = { version = "0.11.0", features = ["default", "fuzzy-select"] }
cargo_toml = "0.20.5"
url = { version = "2.5.3", features = ["default", "serde"] }

tari_core = { git = "https://github.com/tari-project/tari.git", branch = "feature-dan2" }
tari_common_types = { git = "https://github.com/tari-project/tari.git", branch = "feature-dan2" }
minotari_app_grpc = { git = "https://github.com/tari-project/tari.git", branch = "feature-dan2" }

tari_wallet_daemon_client = { git = "https://github.com/tari-project/tari-dan.git", branch = "development" }
tari_dan_engine = { git = "https://github.com/tari-project/tari-dan.git", branch = "development" }
tari_engine_types = { git = "https://github.com/tari-project/tari-dan.git", branch = "development" }
tari_template_lib = { git = "https://github.com/tari-project/tari-dan.git", branch = "development" }
30 changes: 30 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "cli"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
license.workspace = true

[[bin]]
name = "tari"
path = "src/main.rs"

[dependencies]
tari_deploy = { path = "../tari_deploy" }
tari_core = { workspace = true }
anyhow = "1.0.93"
clap = { version = "4.5.20", features = ["derive", "string"] }
dirs-next = "2.0.0"
tokio = { workspace = true }
toml = "0.8.19"
serde = { workspace = true }
termimad = "0.31.0"
cargo-generate = "^0.22.0"
git2 = { version = "^0.19", features = ["default"] }
thiserror = { workspace = true }
spinners = "4.1.1"
convert_case = "0.6.0"
dialoguer = { version = "0.11.0", features = ["default", "fuzzy-select"] }
cargo_toml = "0.20.5"
url = { workspace = true }
122 changes: 51 additions & 71 deletions src/cli/arguments.rs → crates/cli/src/cli/arguments.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::{env, path::PathBuf};

use anyhow::anyhow;
use clap::{
builder::{styling::AnsiColor, Styles},
Parser, Subcommand,
};
use convert_case::{Case, Casing};

use crate::cli::commands::create::CreateArgs;
use crate::cli::commands::deploy;
use crate::cli::commands::deploy::DeployArgs;
use crate::cli::commands::new::NewArgs;
use crate::{
cli::{
commands::{create, new},
Expand All @@ -19,6 +14,14 @@ use crate::{
git::repository::GitRepository,
loading,
};
use anyhow::anyhow;
use clap::{
builder::{styling::AnsiColor, Styles},
Parser, Subcommand, ValueEnum,
};
use convert_case::{Case, Casing};
use std::fmt::{Display, Formatter};
use std::{env, path::PathBuf};

const DEFAULT_DATA_FOLDER_NAME: &str = "tari_cli";
const TEMPLATE_REPOS_FOLDER_NAME: &str = "template_repositories";
Expand All @@ -35,17 +38,17 @@ pub fn cli_styles() -> Styles {
.valid(AnsiColor::BrightGreen.on_default())
}

fn default_base_dir() -> PathBuf {
pub fn default_base_dir() -> PathBuf {
dirs_next::data_dir()
.unwrap_or_else(|| env::current_dir().unwrap())
.join(DEFAULT_DATA_FOLDER_NAME)
}

fn default_target_dir() -> PathBuf {
pub fn default_target_dir() -> PathBuf {
env::current_dir().unwrap()
}

fn default_config_file() -> PathBuf {
pub fn default_config_file() -> PathBuf {
dirs_next::config_dir()
.unwrap_or_else(|| env::current_dir().unwrap())
.join(DEFAULT_DATA_FOLDER_NAME)
Expand All @@ -57,11 +60,11 @@ pub fn config_override_parser(config_override: &str) -> Result<ConfigOverride, S
return Err(String::from("Override cannot be empty!"));
}

let splitted: Vec<&str> = config_override.split("=").collect();
if splitted.len() != 2 {
let split: Vec<&str> = config_override.split("=").collect();
if split.len() != 2 {
return Err(String::from("Invalid override!"));
}
let (key, value) = (splitted.first().unwrap(), splitted.get(1).unwrap());
let (key, value) = (split.first().unwrap(), split.get(1).unwrap());

if !Config::is_override_key_valid(key) {
return Err(format!("Override key invalid: {}", key));
Expand Down Expand Up @@ -105,8 +108,8 @@ pub struct CommonArguments {
#[command(styles = cli_styles())]
#[command(
version,
about = "🚀 Tari DAN CLI 🚀",
long_about = "🚀 Tari DAN CLI 🚀\nHelps you manage the flow of developing Tari templates."
about = "🚀 Tari CLI 🚀",
long_about = "🚀 Tari CLI 🚀\nHelps you manage the flow of developing Tari templates."
)]
pub struct Cli {
#[clap(flatten)]
Expand All @@ -116,39 +119,41 @@ pub struct Cli {
command: Commands,
}

#[derive(Clone, ValueEnum, Debug, PartialEq)]
#[clap(rename_all = "snake_case")]
pub enum Network {
/// Local network
Local,
/// Main network
MainNet,
/// Test network
TestNet,
/// Custom network
Custom,
}

impl Display for Network {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", format!("{:?}", self).to_case(Case::Snake))
}
}

#[derive(Clone, Subcommand)]
pub enum Commands {
/// Creates a new Tari templates project
Create {
/// Name of the project
#[arg(value_parser = project_name_parser)]
name: String,

/// (Optional) Selected project template (ID).
/// It will be prompted if not set.
#[arg(short = 't', long)]
template: Option<String>,

/// Target folder where the new project will be generated
#[arg(long, value_name = "PATH", default_value = default_target_dir().into_os_string()
)]
target: PathBuf,
#[clap(flatten)]
args: CreateArgs,
},
/// Creates a new Tari wasm template project
New {
/// Name of the project
#[arg(value_parser = project_name_parser)]
name: String,

/// (Optional) Selected wasm template (ID).
/// It will be prompted if not set.
#[arg(short = 't', long)]
template: Option<String>,

/// Target folder where the new project will be generated.
#[arg(long, value_name = "PATH", default_value = default_target_dir().into_os_string()
)]
target: PathBuf,
#[clap(flatten)]
args: NewArgs,
},
/// Deploying Tari template to a network
Deploy {
#[clap(flatten)]
args: DeployArgs,
},
}

Expand Down Expand Up @@ -251,34 +256,9 @@ impl Cli {
)?;

match &self.command {
Commands::Create {
name,
template,
target,
} => {
create::handle(
config,
project_template_repo,
name.as_str(),
template.as_ref(),
target.clone(),
)
.await
}
Commands::New {
name,
template,
target,
} => {
new::handle(
config,
wasm_template_repo,
name.as_ref(),
template.as_ref(),
target.clone(),
)
.await
}
Commands::Create { args } => create::handle(config, project_template_repo, args).await,
Commands::New { args } => new::handle(config, wasm_template_repo, args).await,
Commands::Deploy { args } => deploy::handle(args).await,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,37 @@

use std::path::PathBuf;

use anyhow::anyhow;
use cargo_generate::{GenerateArgs, TemplatePath};
use thiserror::Error;

use crate::{
cli::{config::Config, util},
git::repository::GitRepository,
loading,
loading, project,
templates::Collector,
};
use anyhow::anyhow;
use cargo_generate::{GenerateArgs, TemplatePath};
use clap::Parser;
use thiserror::Error;
use tokio::fs;

const PROJECT_TEMPLATE_EXTRA_TEMPLATES_FIELD_NAME: &str = "templates_dir";

#[derive(Clone, Parser, Debug)]
pub struct CreateArgs {
/// Name of the project
#[arg(value_parser = crate::cli::arguments::project_name_parser)]
pub name: String,

/// (Optional) Selected project template (ID).
/// It will be prompted if not set.
#[arg(short = 't', long)]
pub template: Option<String>,

/// Target folder where the new project will be generated
#[arg(long, value_name = "PATH", default_value = crate::cli::arguments::default_target_dir().into_os_string()
)]
pub target: PathBuf,
}

#[derive(Error, Debug)]
pub enum CreateHandlerError {
#[error("Template not found by name: {0}. Possible values: {1:?}")]
Expand All @@ -27,9 +45,7 @@ pub enum CreateHandlerError {
pub async fn handle(
config: Config,
project_template_repo: GitRepository,
name: &str,
project_template: Option<&String>,
target: PathBuf,
args: &CreateArgs,
) -> anyhow::Result<()> {
// selecting project template
let templates = loading!(
Expand All @@ -43,7 +59,7 @@ pub async fn handle(
.await
)?;

let template = match project_template {
let template = match &args.template {
Some(template_id) => templates
.iter()
.filter(|template| template.id().to_lowercase() == template_id.to_lowercase())
Expand All @@ -66,8 +82,8 @@ pub async fn handle(

// generate new project
let generate_args = GenerateArgs {
name: Some(name.to_string()),
destination: Some(target.clone()),
name: Some(args.name.to_string()),
destination: Some(args.target.clone()),
template_path: TemplatePath {
path: Some(template_path),
..TemplatePath::default()
Expand All @@ -79,15 +95,29 @@ pub async fn handle(
cargo_generate::generate(generate_args)
)?;

let final_path = args.target.join(args.name.as_str());

// create templates dir if set
if let Some(templates_dir) = template
.extra()
.get(PROJECT_TEMPLATE_EXTRA_TEMPLATES_FIELD_NAME)
{
util::create_dir(&target.join(&name).join(templates_dir)).await?;
util::create_dir(&final_path.join(templates_dir)).await?;
}

// init project config file (remove if exists already somehow)
let project_config_file = final_path.join(project::CONFIG_FILE_NAME);
if util::file_exists(&project_config_file).await? {
fs::remove_file(&project_config_file).await?;
}
fs::write(
&project_config_file,
toml::to_string(&project::Config::default())?,
)
.await?;

// git init
let mut new_repo = GitRepository::new(target.join(name));
let mut new_repo = GitRepository::new(final_path);
new_repo.init()?;

Ok(())
Expand Down
Loading

0 comments on commit 133d7b4

Please sign in to comment.