From c883c9bd4b4288a3bd07d49bb6766ed60fcd424d Mon Sep 17 00:00:00 2001 From: chesedo Date: Fri, 4 Nov 2022 11:22:54 +0200 Subject: [PATCH 1/6] feat: init shuttle-admin --- Cargo.lock | 4 ++++ Cargo.toml | 1 + admin/Cargo.toml | 6 ++++++ admin/src/main.rs | 3 +++ 4 files changed, 14 insertions(+) create mode 100644 admin/Cargo.toml create mode 100644 admin/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index b425cf9b7..04c148ef2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5303,6 +5303,10 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "45bb67a18fa91266cc7807181f62f9178a6873bfad7dc788c42e6430db40184f" +[[package]] +name = "shuttle-admin" +version = "0.1.0" + [[package]] name = "shuttle-codegen" version = "0.7.2" diff --git a/Cargo.toml b/Cargo.toml index 1e840d978..6ae4b77e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "admin", "cargo-shuttle", "codegen", "common", diff --git a/admin/Cargo.toml b/admin/Cargo.toml new file mode 100644 index 000000000..7d1959cdb --- /dev/null +++ b/admin/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shuttle-admin" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/admin/src/main.rs b/admin/src/main.rs new file mode 100644 index 000000000..e7a11a969 --- /dev/null +++ b/admin/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From 3b75c00365677432500506d546397f20e93acb7f Mon Sep 17 00:00:00 2001 From: chesedo Date: Fri, 4 Nov 2022 11:37:31 +0200 Subject: [PATCH 2/6] feat: add args --- Cargo.lock | 3 +++ admin/Cargo.toml | 1 + admin/src/args.rs | 17 +++++++++++++++++ admin/src/lib.rs | 1 + admin/src/main.rs | 7 ++++++- 5 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 admin/src/args.rs create mode 100644 admin/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 04c148ef2..8e872ce4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5306,6 +5306,9 @@ checksum = "45bb67a18fa91266cc7807181f62f9178a6873bfad7dc788c42e6430db40184f" [[package]] name = "shuttle-admin" version = "0.1.0" +dependencies = [ + "clap 4.0.18", +] [[package]] name = "shuttle-codegen" diff --git a/admin/Cargo.toml b/admin/Cargo.toml index 7d1959cdb..7dedc8dbf 100644 --- a/admin/Cargo.toml +++ b/admin/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +clap = { version = "4.0.0", features = [ "derive", "env" ] } diff --git a/admin/src/args.rs b/admin/src/args.rs new file mode 100644 index 000000000..c73e487bd --- /dev/null +++ b/admin/src/args.rs @@ -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, env = "SHUTTLE_API")] + pub api_url: Option, + + #[command(subcommand)] + pub command: Command, +} + +#[derive(Subcommand, Debug)] +pub enum Command { + /// Try to revive projects in the crashed state + Revive, +} diff --git a/admin/src/lib.rs b/admin/src/lib.rs new file mode 100644 index 000000000..6e10f4ada --- /dev/null +++ b/admin/src/lib.rs @@ -0,0 +1 @@ +pub mod args; diff --git a/admin/src/main.rs b/admin/src/main.rs index e7a11a969..2450a00cb 100644 --- a/admin/src/main.rs +++ b/admin/src/main.rs @@ -1,3 +1,8 @@ +use clap::Parser; +use shuttle_admin::args::Args; + fn main() { - println!("Hello, world!"); + let args = Args::parse(); + + println!("{args:?}"); } From 03fba4cec1b6260f34a45454f51040e19818033f Mon Sep 17 00:00:00 2001 From: chesedo Date: Fri, 4 Nov 2022 11:53:58 +0200 Subject: [PATCH 3/6] feat: simple client --- Cargo.lock | 3 +++ admin/Cargo.toml | 3 +++ admin/src/client.rs | 28 ++++++++++++++++++++++++++++ admin/src/lib.rs | 1 + 4 files changed, 35 insertions(+) create mode 100644 admin/src/client.rs diff --git a/Cargo.lock b/Cargo.lock index 8e872ce4d..c4d68828e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5307,7 +5307,10 @@ checksum = "45bb67a18fa91266cc7807181f62f9178a6873bfad7dc788c42e6430db40184f" name = "shuttle-admin" version = "0.1.0" dependencies = [ + "anyhow", "clap 4.0.18", + "reqwest", + "tokio", ] [[package]] diff --git a/admin/Cargo.toml b/admin/Cargo.toml index 7dedc8dbf..f6a234f65 100644 --- a/admin/Cargo.toml +++ b/admin/Cargo.toml @@ -4,4 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +anyhow = "1.0.62" clap = { version = "4.0.0", features = [ "derive", "env" ] } +reqwest = { version = "0.11.11", features = ["json"] } +tokio = { version = "1.20.1", features = ["macros"] } diff --git a/admin/src/client.rs b/admin/src/client.rs new file mode 100644 index 000000000..7cb206e53 --- /dev/null +++ b/admin/src/client.rs @@ -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 { + self.get("/admin/revive").await + } + + async fn get(&self, path: &str) -> Result { + 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") + } +} diff --git a/admin/src/lib.rs b/admin/src/lib.rs index 6e10f4ada..cb5f86f83 100644 --- a/admin/src/lib.rs +++ b/admin/src/lib.rs @@ -1 +1,2 @@ pub mod args; +pub mod client; From caf6ea1c86f6a6c33e081f4e27068507bafd6951 Mon Sep 17 00:00:00 2001 From: chesedo Date: Fri, 4 Nov 2022 12:22:05 +0200 Subject: [PATCH 4/6] feat: read api_key --- Cargo.lock | 2 ++ admin/Cargo.toml | 2 ++ admin/src/config.rs | 15 +++++++++++++++ admin/src/lib.rs | 1 + admin/src/main.rs | 4 +++- 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 admin/src/config.rs diff --git a/Cargo.lock b/Cargo.lock index c4d68828e..169ad4dde 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5309,8 +5309,10 @@ version = "0.1.0" dependencies = [ "anyhow", "clap 4.0.18", + "dirs", "reqwest", "tokio", + "toml", ] [[package]] diff --git a/admin/Cargo.toml b/admin/Cargo.toml index f6a234f65..c6ab4549a 100644 --- a/admin/Cargo.toml +++ b/admin/Cargo.toml @@ -6,5 +6,7 @@ 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"] } +toml = "0.5.9" diff --git a/admin/src/config.rs b/admin/src/config.rs new file mode 100644 index 000000000..52a295c44 --- /dev/null +++ b/admin/src/config.rs @@ -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") +} diff --git a/admin/src/lib.rs b/admin/src/lib.rs index cb5f86f83..f1c9fb465 100644 --- a/admin/src/lib.rs +++ b/admin/src/lib.rs @@ -1,2 +1,3 @@ pub mod args; pub mod client; +pub mod config; diff --git a/admin/src/main.rs b/admin/src/main.rs index 2450a00cb..e8887806f 100644 --- a/admin/src/main.rs +++ b/admin/src/main.rs @@ -1,8 +1,10 @@ use clap::Parser; -use shuttle_admin::args::Args; +use shuttle_admin::{args::Args, config::get_api_key}; fn main() { let args = Args::parse(); + let api_key = get_api_key(); println!("{args:?}"); + println!("{api_key}"); } From cd26adf0c779c58c3ec343cee2dc323842cf91be Mon Sep 17 00:00:00 2001 From: chesedo Date: Fri, 4 Nov 2022 12:33:35 +0200 Subject: [PATCH 5/6] refactor: hook it all together --- admin/Cargo.toml | 2 +- admin/src/args.rs | 4 ++-- admin/src/main.rs | 17 +++++++++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/admin/Cargo.toml b/admin/Cargo.toml index c6ab4549a..87ef178a7 100644 --- a/admin/Cargo.toml +++ b/admin/Cargo.toml @@ -8,5 +8,5 @@ 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"] } +tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread"] } toml = "0.5.9" diff --git a/admin/src/args.rs b/admin/src/args.rs index c73e487bd..5f64d8977 100644 --- a/admin/src/args.rs +++ b/admin/src/args.rs @@ -3,8 +3,8 @@ use clap::{Parser, Subcommand}; #[derive(Parser, Debug)] pub struct Args { /// run this command against the api at the supplied url - #[arg(long, env = "SHUTTLE_API")] - pub api_url: Option, + #[arg(long, default_value = "https://api.shuttle.rs", env = "SHUTTLE_API")] + pub api_url: String, #[command(subcommand)] pub command: Command, diff --git a/admin/src/main.rs b/admin/src/main.rs index e8887806f..91fac1a89 100644 --- a/admin/src/main.rs +++ b/admin/src/main.rs @@ -1,10 +1,19 @@ use clap::Parser; -use shuttle_admin::{args::Args, config::get_api_key}; +use shuttle_admin::{ + args::{Args, Command}, + client::Client, + config::get_api_key, +}; -fn main() { +#[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); - println!("{args:?}"); - println!("{api_key}"); + let res = match args.command { + Command::Revive => client.revive().await.expect("revive to succeed"), + }; + + println!("{res}"); } From 409451c8df7645dee6dd9504eb8bc23976413a9c Mon Sep 17 00:00:00 2001 From: chesedo Date: Fri, 4 Nov 2022 17:59:39 +0200 Subject: [PATCH 6/6] refactor: switch to post --- Cargo.lock | 2 +- admin/src/client.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 169ad4dde..1bb820ac4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5308,7 +5308,7 @@ name = "shuttle-admin" version = "0.1.0" dependencies = [ "anyhow", - "clap 4.0.18", + "clap 4.0.4", "dirs", "reqwest", "tokio", diff --git a/admin/src/client.rs b/admin/src/client.rs index 7cb206e53..7738fc512 100644 --- a/admin/src/client.rs +++ b/admin/src/client.rs @@ -11,18 +11,18 @@ impl Client { } pub async fn revive(&self) -> Result { - self.get("/admin/revive").await + self.post("/admin/revive").await } - async fn get(&self, path: &str) -> Result { + async fn post(&self, path: &str) -> Result { reqwest::Client::new() - .get(format!("{}{}", self.api_url, path)) + .post(format!("{}{}", self.api_url, path)) .bearer_auth(&self.api_key) .send() .await - .context("failed to make get request")? + .context("failed to make post request")? .text() .await - .context("failed to get text body from response") + .context("failed to post text body from response") } }