Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create a new admin cli binary crate #462

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"admin",
"cargo-shuttle",
"codegen",
"common",
Expand Down
12 changes: 12 additions & 0 deletions admin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "shuttle-admin"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.62"
clap = { version = "4.0.0", features = [ "derive", "env" ] }
dirs = "4.0.0"
reqwest = { version = "0.11.11", features = ["json"] }
tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread"] }
toml = "0.5.9"
17 changes: 17 additions & 0 deletions admin/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
pub struct Args {
/// run this command against the api at the supplied url
#[arg(long, default_value = "https://api.shuttle.rs", env = "SHUTTLE_API")]
pub api_url: String,

#[command(subcommand)]
pub command: Command,
}

#[derive(Subcommand, Debug)]
pub enum Command {
/// Try to revive projects in the crashed state
Revive,
}
28 changes: 28 additions & 0 deletions admin/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use anyhow::{Context, Result};

pub struct Client {
api_url: String,
api_key: String,
}

impl Client {
pub fn new(api_url: String, api_key: String) -> Self {
Self { api_url, api_key }
}

pub async fn revive(&self) -> Result<String> {
self.get("/admin/revive").await
chesedo marked this conversation as resolved.
Show resolved Hide resolved
}

async fn get(&self, path: &str) -> Result<String> {
reqwest::Client::new()
.get(format!("{}{}", self.api_url, path))
.bearer_auth(&self.api_key)
.send()
.await
.context("failed to make get request")?
.text()
.await
.context("failed to get text body from response")
}
}
15 changes: 15 additions & 0 deletions admin/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::{fs, path::PathBuf};

pub fn get_api_key() -> String {
let data = fs::read_to_string(config_path()).expect("shuttle config file to exist");
let toml: toml::Value = toml::from_str(&data).expect("to parse shuttle config file");

toml["api_key"].to_string()
}

fn config_path() -> PathBuf {
dirs::config_dir()
.expect("system to have a config path")
.join("shuttle")
.join("config.toml")
}
3 changes: 3 additions & 0 deletions admin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod args;
pub mod client;
pub mod config;
19 changes: 19 additions & 0 deletions admin/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use clap::Parser;
use shuttle_admin::{
args::{Args, Command},
client::Client,
config::get_api_key,
};

#[tokio::main]
async fn main() {
let args = Args::parse();
let api_key = get_api_key();
let client = Client::new(args.api_url.clone(), api_key);

let res = match args.command {
Command::Revive => client.revive().await.expect("revive to succeed"),
};

println!("{res}");
}