-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create a new admin cli binary crate (#462)
* 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
Showing
8 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
[workspace] | ||
members = [ | ||
"admin", | ||
"cargo-shuttle", | ||
"codegen", | ||
"common", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod args; | ||
pub mod client; | ||
pub mod config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} |