Skip to content

Commit

Permalink
feat: create a new admin cli binary crate (#462)
Browse files Browse the repository at this point in the history
* feat: init shuttle-admin

* feat: add args

* feat: simple client

* feat: read api_key

* refactor: hook it all together

* refactor: switch to post
  • Loading branch information
chesedo authored Nov 4, 2022
1 parent 88c877d commit 7471c08
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 0 deletions.
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.post("/admin/revive").await
}

async fn post(&self, path: &str) -> Result<String> {
reqwest::Client::new()
.post(format!("{}{}", self.api_url, path))
.bearer_auth(&self.api_key)
.send()
.await
.context("failed to make post request")?
.text()
.await
.context("failed to post 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}");
}

0 comments on commit 7471c08

Please sign in to comment.