diff --git a/packages/cli/src/commands/backend/get_current_version.rs b/packages/cli/src/commands/backend/get_current_version.rs new file mode 100644 index 00000000..f3f0bb44 --- /dev/null +++ b/packages/cli/src/commands/backend/get_current_version.rs @@ -0,0 +1,80 @@ +use clap::Parser; +use serde_json::json; +use std::process::ExitCode; +use toolchain::{game_server, rivet_api::apis}; + +/// Get the current game version +#[derive(Parser)] +pub struct Opts { + environment: String, +} + +impl Opts { + pub async fn execute(&self) -> ExitCode { + let ctx = match toolchain::toolchain_ctx::load().await { + Ok(x) => x, + Err(_) => { + eprintln!("failed to get ctx"); + return ExitCode::FAILURE; + } + }; + + let env = match apis::cloud_games_api::cloud_games_get_game_by_id( + &ctx.openapi_config_cloud, + &ctx.game_id.to_string(), + None, + ) + .await + { + Ok(x) => x, + Err(err) => { + eprintln!("failed to get environments: {err}"); + return ExitCode::FAILURE; + } + }; + + if let Some(env) = env + .game + .namespaces + .iter() + .find(|x| x.name_id == self.environment) + { + // Fetch build with the current tag & select first one + let tags = serde_json::to_string(&json!({ + game_server::CURRENT_BUILD_TAG: "true" + })) + .unwrap_or_default(); + + let current_build = match apis::servers_builds_api::servers_builds_list( + &ctx.openapi_config_cloud, + &ctx.game_id.to_string(), + &env.namespace_id.to_string(), + Some(&tags), + ) + .await + { + Ok(builds) => builds.builds.into_iter().next(), + Err(err) => { + eprintln!("failed to get current build: {err}"); + return ExitCode::FAILURE; + } + }; + + if let Some(build) = current_build { + if let Some(tag) = build.tags.get(game_server::VERSION_BUILD_TAG) { + println!("{tag}"); + ExitCode::SUCCESS + } else { + eprintln!("No version tag found"); + ExitCode::FAILURE + } + } else { + eprintln!("No current build found"); + ExitCode::FAILURE + } + } else { + eprintln!("environment not found"); + ExitCode::FAILURE + } + } +} diff --git a/packages/cli/src/commands/backend/get_endpoint.rs b/packages/cli/src/commands/backend/get_endpoint.rs new file mode 100644 index 00000000..9bc4e6a5 --- /dev/null +++ b/packages/cli/src/commands/backend/get_endpoint.rs @@ -0,0 +1,56 @@ +use clap::Parser; +use std::process::ExitCode; +use toolchain::{backend, rivet_api::apis}; + +/// Get the current game version +#[derive(Parser)] +pub struct Opts { + environment: String, +} + +impl Opts { + pub async fn execute(&self) -> ExitCode { + let ctx = match toolchain::toolchain_ctx::load().await { + Ok(x) => x, + Err(err) => { + eprintln!("failed to get ctx: {err}"); + return ExitCode::FAILURE; + } + }; + + let env = match apis::cloud_games_api::cloud_games_get_game_by_id( + &ctx.openapi_config_cloud, + &ctx.game_id.to_string(), + None, + ) + .await + { + Ok(x) => x, + Err(err) => { + eprintln!("failed to get environments: {err}"); + return ExitCode::FAILURE; + } + }; + + if let Some(env) = env + .game + .namespaces + .iter() + .find(|x| x.name_id == self.environment) + { + match backend::get_or_create_backend(&ctx, env.namespace_id).await { + Ok(x) => { + eprintln!("{}", x.endpoint); + ExitCode::SUCCESS + } + Err(err) => { + eprintln!("failed to get backend: {err}"); + ExitCode::FAILURE + } + } + } else { + eprintln!("environment not found"); + ExitCode::FAILURE + } + } +} diff --git a/packages/cli/src/commands/backend/mod.rs b/packages/cli/src/commands/backend/mod.rs index ae0bbf3e..8a3e5661 100644 --- a/packages/cli/src/commands/backend/mod.rs +++ b/packages/cli/src/commands/backend/mod.rs @@ -1,6 +1,8 @@ pub mod build; pub mod gen_openapi; pub mod gen_sdk; +pub mod get_current_version; +pub mod get_endpoint; pub mod show; use clap::Subcommand; @@ -15,6 +17,10 @@ pub enum SubCommand { GenerateOpenApi(gen_openapi::Opts), #[clap(name = "generate-sdk")] GenerateSdk(gen_sdk::Opts), + #[clap(name = "get-endpoint")] + GetEndpoint(get_endpoint::Opts), + #[clap(name = "get-current-version")] + GetCurrentVersion(get_current_version::Opts), } impl SubCommand { @@ -24,6 +30,8 @@ impl SubCommand { SubCommand::Show(opts) => opts.execute().await, SubCommand::GenerateOpenApi(opts) => opts.execute().await, SubCommand::GenerateSdk(opts) => opts.execute().await, + SubCommand::GetEndpoint(opts) => opts.execute().await, + SubCommand::GetCurrentVersion(opts) => opts.execute().await, } } }