Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
feat(toolchain): add get-current-version and get-endpoint commands (#549
Browse files Browse the repository at this point in the history
)
  • Loading branch information
NathanFlurry committed Oct 2, 2024
1 parent b10140e commit 294b466
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
80 changes: 80 additions & 0 deletions packages/cli/src/commands/backend/get_current_version.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
56 changes: 56 additions & 0 deletions packages/cli/src/commands/backend/get_endpoint.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
8 changes: 8 additions & 0 deletions packages/cli/src/commands/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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,
}
}
}

0 comments on commit 294b466

Please sign in to comment.