This repository has been archived by the owner on Dec 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toolchain): add get-current-version and get-endpoint commands (#549
- Loading branch information
1 parent
b10140e
commit 294b466
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters