Skip to content

Commit

Permalink
Make add command use deploy internals (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulocsanz committed Jun 25, 2024
1 parent c2e5685 commit 9013120
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 105 deletions.
137 changes: 33 additions & 104 deletions src/commands/add.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use anyhow::bail;
use is_terminal::IsTerminal;
use std::collections::BTreeMap;
use std::time::Duration;
use std::collections::HashMap;
use strum::IntoEnumIterator;

use crate::{
consts::TICK_STRING, controllers::database::DatabaseType, mutations::TemplateVolume,
util::prompt::prompt_multi_options,
};
use crate::{controllers::database::DatabaseType, util::prompt::prompt_multi_options};

use super::*;

Expand All @@ -17,6 +13,16 @@ pub struct Args {
/// The name of the database to add
#[arg(short, long, value_enum)]
database: Vec<DatabaseType>,
/// The "{key}={value}" environment variable pair to set the template variables
///
/// To specify the variable for a single service prefix it with "{service}."
/// Example:
///
/// ```bash
/// railway deploy -t postgres -v "MY_SPECIAL_ENV_VAR=1" -v "Backend.Port=3000"
/// ```
#[arg(short, long)]
variable: Vec<String>,
}

pub async fn command(args: Args, _json: bool) -> Result<()> {
Expand All @@ -38,110 +44,33 @@ pub async fn command(args: Args, _json: bool) -> Result<()> {
bail!("No database selected");
}

let variables: HashMap<String, String> = args
.variable
.iter()
.map(|v| {
let mut split = v.split('=');
let key = split
.next()
.as_deref()
.unwrap_or_default()
.trim()
.to_owned();
let value = split.collect::<Vec<&str>>().join("=").trim().to_owned();
(key, value)
})
.filter(|(_, value)| !value.is_empty())
.collect();

for db in databases {
if std::io::stdout().is_terminal() {
let spinner = indicatif::ProgressBar::new_spinner()
.with_style(
indicatif::ProgressStyle::default_spinner()
.tick_chars(TICK_STRING)
.template("{spinner:.green} {msg}")?,
)
.with_message(format!("Creating {db}..."));
spinner.enable_steady_tick(Duration::from_millis(100));
fetch_and_create(&client, &configs, db.clone(), &linked_project).await?;
spinner.finish_with_message(format!("Created {db}"));
deploy::fetch_and_create(&client, &configs, db.to_slug(), &linked_project, &variables)
.await?;
} else {
println!("Creating {}...", db);
fetch_and_create(&client, &configs, db, &linked_project).await?;
deploy::fetch_and_create(&client, &configs, db.to_slug(), &linked_project, &variables)
.await?;
}
}

Ok(())
}
/// fetch database details via `TemplateDetail`
/// create database via `TemplateDeploy`
async fn fetch_and_create(
client: &reqwest::Client,
configs: &Configs,
db: DatabaseType,
linked_project: &LinkedProject,
) -> Result<(), anyhow::Error> {
let details = post_graphql::<queries::TemplateDetail, _>(
client,
configs.get_backboard(),
queries::template_detail::Variables { code: db.to_slug() },
)
.await?;

let services: Vec<mutations::template_deploy::TemplateDeployService> = details
.template
.services
.edges
.iter()
.map(|s| {
let s_var = s
.node
.config
.variables
.iter()
.map(|variable| {
(
variable.name.clone(),
variable.default_value.clone().unwrap(),
)
})
.collect::<BTreeMap<String, String>>();

let s_vol = s
.node
.config
.volumes
.clone()
.map(|volumes| {
volumes
.into_iter()
.map(|volume| TemplateVolume {
mount_path: volume.mount_path.clone(),
name: volume.name.clone(),
})
.collect::<Vec<TemplateVolume>>()
})
.unwrap_or_default();

mutations::template_deploy::TemplateDeployService {
commit: None,
has_domain: Some(s.node.config.domains.iter().any(|d| d.has_service_domain)),
healthcheck_path: None,
id: Some(s.node.id.clone()),
is_private: None,
name: Some(s.node.config.name.clone()),
owner: None,
root_directory: None,
service_icon: s.node.config.icon.clone(),
service_name: s.node.config.name.clone(),
start_command: s
.node
.config
.deploy_config
.as_ref()
.and_then(|deploy_config| deploy_config.start_command.clone()),
tcp_proxy_application_port: s.node.config.tcp_proxies.as_ref().and_then(
|tcp_proxies| tcp_proxies.first().map(|first| first.application_port),
),
template: s.node.config.source.image.clone(),
variables: (!s_var.is_empty()).then_some(s_var),
volumes: (!s_vol.is_empty()).then_some(s_vol),
}
})
.collect();

let vars = mutations::template_deploy::Variables {
project_id: linked_project.project.clone(),
environment_id: linked_project.environment.clone(),
services,
template_code: db.to_slug(),
};

post_graphql::<mutations::TemplateDeploy, _>(client, configs.get_backboard(), vars).await?;
Ok(())
}
2 changes: 1 addition & 1 deletion src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub async fn command(args: Args, _json: bool) -> Result<()> {
}
/// fetch database details via `TemplateDetail`
/// create database via `TemplateDeploy`
async fn fetch_and_create(
pub async fn fetch_and_create(
client: &reqwest::Client,
configs: &Configs,
template: String,
Expand Down

0 comments on commit 9013120

Please sign in to comment.