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

Commit

Permalink
chore: add back cli
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Aug 1, 2024
1 parent 2939244 commit 81a0c93
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 7 deletions.
122 changes: 120 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = [
members = [ "rivet-cli",
"rivet-toolchain",
]

Expand Down
12 changes: 12 additions & 0 deletions rivet-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "rivet-cli"
version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.5.9", features = ["derive"] }
global-error = { git = "https://github.com/rivet-gg/rivet.git", rev = "22baf31efa3ffcdad65ecc72ce25425ab61b9c6f" }
toolchain = { version = "0.1.0", path = "../rivet-toolchain", package = "rivet-toolchain" }
tokio = { version = "1.38.1", features = ["full"] }
serde_json = "1.0.120"

50 changes: 50 additions & 0 deletions rivet-cli/src/commands/login.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use clap::Parser;
use global_error::prelude::*;
use toolchain::{
tasks::{check_login_state, start_device_link, wait_for_login, RunConfig},
util::task::run_task,
};

#[derive(Parser)]
pub struct Opts {
#[clap(long, default_value = "https://api.rivet.gg")]
api_endpoint: String,
}

impl Opts {
pub async fn execute(&self) -> GlobalResult<()> {
let (run_config, _temp_dir) = RunConfig::with_temp_dir()?;

// Check if linked
let output =
run_task::<check_login_state::Task>(run_config.clone(), check_login_state::Input {})
.await?;
if output.logged_in {
eprintln!("Already logged in. Sign out with `rivet unlink`.");
return Ok(());
}

// Start device link
let output = run_task::<start_device_link::Task>(
run_config.clone(),
start_device_link::Input {
api_endpoint: self.api_endpoint.clone(),
},
)
.await?;
eprintln!("{}", output.device_link_url);

// Wait for finish
run_task::<wait_for_login::Task>(
run_config.clone(),
wait_for_login::Input {
api_endpoint: self.api_endpoint.clone(),
device_link_token: output.device_link_token,
},
)
.await?;
eprintln!("Logged in");

Ok(())
}
}
20 changes: 20 additions & 0 deletions rivet-cli/src/commands/logout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use clap::Parser;
use global_error::prelude::*;
use toolchain::{
tasks::{unlink, RunConfig},
util::task::run_task,
};

#[derive(Parser)]
pub struct Opts {}

impl Opts {
pub async fn execute(&self) -> GlobalResult<()> {
let (run_config, _temp_dir) = RunConfig::with_temp_dir()?;

run_task::<unlink::Task>(run_config.clone(), unlink::Input {}).await?;
eprintln!("Logged out");

Ok(())
}
}
26 changes: 26 additions & 0 deletions rivet-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pub mod login;
pub mod logout;
pub mod task;

use clap::Parser;
use global_error::prelude::*;

#[derive(Parser)]
pub enum SubCommand {
Login(login::Opts),
Task {
#[clap(subcommand)]
subcommand: task::SubCommand,
},
Logout(logout::Opts),
}

impl SubCommand {
pub async fn execute(&self) -> GlobalResult<()> {
match self {
SubCommand::Login(opts) => opts.execute().await,
SubCommand::Task { subcommand } => subcommand.execute().await,
SubCommand::Logout(opts) => opts.execute().await,
}
}
}
35 changes: 35 additions & 0 deletions rivet-cli/src/commands/task.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use clap::Parser;
use global_error::prelude::*;

/// EXPERIMENTAL
#[derive(Parser)]
pub enum SubCommand {
Run(RunOpts),
}

impl SubCommand {
pub async fn execute(&self) -> GlobalResult<()> {
match self {
SubCommand::Run(opts) => opts.execute().await,
}
}
}

#[derive(Parser)]
pub struct RunOpts {
#[clap(long)]
run_config: String,
#[clap(long)]
name: String,
#[clap(long)]
input: String,
}

impl RunOpts {
pub async fn execute(&self) -> GlobalResult<()> {
let run_config = serde_json::from_str(&self.run_config)?;
let output = toolchain::tasks::run_task_json(run_config, &self.name, &self.input).await;
println!("{output}");
Ok(())
}
}
19 changes: 19 additions & 0 deletions rivet-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub mod commands;

use clap::Parser;
use global_error::GlobalResult;

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: commands::SubCommand,
}

#[tokio::main]
async fn main() -> GlobalResult<()> {
let cli = Cli::parse();
cli.command.execute().await?;
Ok(())
}

2 changes: 1 addition & 1 deletion rivet-toolchain/src/tasks/check_login_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Input {}

#[derive(Serialize)]
pub struct Output {
logged_in: bool,
pub logged_in: bool,
}

pub struct Task;
Expand Down
2 changes: 0 additions & 2 deletions rivet-toolchain/src/tasks/unlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use serde::{Deserialize, Serialize};

use crate::{config, util::task::TaskCtx};

// use crate::commands;

#[derive(Deserialize)]
pub struct Input {}

Expand Down
Loading

0 comments on commit 81a0c93

Please sign in to comment.