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

Commit

Permalink
feat(sidekick): add backend dev command
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Jul 11, 2024
1 parent 8d6d8c2 commit 7e9ae70
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
67 changes: 67 additions & 0 deletions cli/src/commands/sidekick/backend_dev.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use clap::Parser;
use global_error::prelude::*;
use serde::Serialize;
use std::collections::HashMap;

use crate::commands::backend;

use super::SideKickHandler;

#[derive(Parser)]
pub struct Opts {
#[clap(long)]
no_color: bool,
#[clap(long)]
capture_output: bool,
}

#[derive(Serialize)]
pub struct Output {
pub exit_code: i32,
pub stdout: String,
pub stderr: String,
}

impl SideKickHandler for Output {}

impl Opts {
pub async fn execute(&self) -> GlobalResult<Output> {
// Run command
//
// Force-deploy migrations since we don't have TTY access to prompt for
// migrations. We also don't want to promp for migrations if the dev is
// setting this up for the first time.
let args = vec!["dev".into(), "--force-deploy-migrations".into()];
let mut env = HashMap::new();
if self.no_color {
env.insert("OPENGB_TERM_COLOR".into(), "never".into());
}
let mut opengb_command = backend::build_opengb_command(backend::OpenGbCommandOpts {
args,
env,
cwd: std::env::current_dir()?,
})?;

if self.capture_output {
let opengb_output = opengb_command.output().await?;

let output = Output {
exit_code: opengb_output.status.code().unwrap_or(1),
stdout: String::from_utf8(opengb_output.stdout)?,
stderr: String::from_utf8(opengb_output.stderr)?,
};

Ok(output)
} else {
let opengb_status = opengb_command.status().await?;

let output = Output {
exit_code: opengb_status.code().unwrap_or(1),
stdout: String::new(),
stderr: String::new(),
};

Ok(output)
}
}
}
17 changes: 15 additions & 2 deletions cli/src/commands/sidekick/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::util::{
struct_fmt::{self, Format},
};

pub mod backend_dev;
pub mod backend_gen_sdk;
pub mod deploy;
pub mod generate_config;
Expand Down Expand Up @@ -62,6 +63,7 @@ pub enum SubCommand {
GetNamespaceDevelopmentToken(get_namespace_dev_token::Opts),
/// Generate config
GenerateConfig(generate_config::Opts),
BackendDev(backend_dev::Opts),
BackendGenerateSdk(backend_gen_sdk::Opts),
/// Unlink current game
Unlink(unlink::Opts),
Expand Down Expand Up @@ -89,8 +91,14 @@ impl SubCommand {
pub async fn pre_execute(
&self,
token: &Option<String>,
show_terminal: bool,
inside_terminal: bool,
) -> GlobalResult<PreExecuteHandled> {
if show_terminal {
SubCommand::show_terminal().await?;
return Ok(PreExecuteHandled::Yes);
}

let mut handled = PreExecuteHandled::Yes;
let response = match self {
SubCommand::ShowTerm(opts) => serialize_output(opts.execute().await),
Expand All @@ -99,6 +107,7 @@ impl SubCommand {
SubCommand::CheckLoginState => serialize_output(self.validate_token(&token)),
SubCommand::GetCliVersion(opts) => serialize_output(opts.execute().await),
SubCommand::GenerateConfig(opts) => serialize_output(opts.execute().await),
SubCommand::BackendDev(opts) => serialize_output(opts.execute().await),
SubCommand::BackendGenerateSdk(opts) => serialize_output(opts.execute().await),
_ => {
// If the command is anything else, we need to check if a token
Expand Down Expand Up @@ -136,7 +145,7 @@ impl SubCommand {
inside_terminal: bool,
) -> GlobalResult<()> {
if show_terminal {
SubCommand::show_terminal(ctx).await?;
SubCommand::show_terminal().await?;
return Ok(());
}

Expand All @@ -151,6 +160,7 @@ impl SubCommand {
| SubCommand::CheckLoginState
| SubCommand::WaitForLogin(_)
| SubCommand::GenerateConfig(_)
| SubCommand::BackendDev(_)
| SubCommand::BackendGenerateSdk(_)
| SubCommand::GetCliVersion(_) => {
unreachable!("This command should be handled before this")
Expand Down Expand Up @@ -203,7 +213,10 @@ impl SubCommand {
Ok(())
}

pub async fn show_terminal(_ctx: &cli_core::Ctx) -> GlobalResult<()> {
/**
* Reads the raw env vars and spawns a new terminal for the given command.
*/
pub async fn show_terminal() -> GlobalResult<()> {
// TODO(forest): The code doesn't handle the case where the binary
// path or the arguments contain special characters that might need
// to be escaped or quoted.
Expand Down
6 changes: 5 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,15 @@ async fn handle_opts() -> GlobalResult<()> {
// Sidekick sign-in can also be called before the token is validated
if let SubCommand::Sidekick {
command,
show_terminal,
inside_terminal,
..
} = &opts.command
{
if let Ok(PreExecuteHandled::Yes) = command.pre_execute(&token, *inside_terminal).await {
if let Ok(PreExecuteHandled::Yes) = command
.pre_execute(&token, *show_terminal, *inside_terminal)
.await
{
return Ok(());
}
}
Expand Down

0 comments on commit 7e9ae70

Please sign in to comment.