Skip to content

Commit 2576fce

Browse files
Merge branch 'main' into feat/silent-flag
2 parents 4fe7244 + d26e6c8 commit 2576fce

File tree

17 files changed

+852
-181
lines changed

17 files changed

+852
-181
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

admin/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ publish = false
88
shuttle-common = { workspace = true, features = ["models"] }
99

1010
anyhow = { workspace = true }
11+
bytes = { workspace = true }
1112
clap = { workspace = true, features = ["env"] }
1213
dirs = { workspace = true }
1314
reqwest = { workspace = true, features = ["json"] }

admin/src/args.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{fs, io, path::PathBuf};
22

33
use clap::{Error, Parser, Subcommand};
4+
use shuttle_common::models::user::UserId;
45

56
#[derive(Parser, Debug)]
67
pub struct Args {
@@ -27,6 +28,11 @@ pub enum Command {
2728
/// Manage project names
2829
ProjectNames,
2930

31+
ChangeProjectOwner {
32+
project_name: String,
33+
new_user_id: UserId,
34+
},
35+
3036
/// Viewing and managing stats
3137
#[command(subcommand)]
3238
Stats(StatsCommand),

admin/src/client.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use anyhow::{Context, Result};
1+
use anyhow::{bail, Context, Result};
2+
use bytes::Bytes;
23
use serde::{de::DeserializeOwned, Serialize};
34
use shuttle_common::models::{admin::ProjectResponse, stats, ToJson};
45
use tracing::trace;
@@ -73,6 +74,15 @@ impl Client {
7374
self.get("/admin/projects").await
7475
}
7576

77+
pub async fn change_project_owner(&self, project_name: &str, new_user_id: &str) -> Result<()> {
78+
self.get_raw(&format!(
79+
"/admin/projects/change-owner/{project_name}/{new_user_id}"
80+
))
81+
.await?;
82+
83+
Ok(())
84+
}
85+
7686
pub async fn get_load(&self) -> Result<stats::LoadResponse> {
7787
self.get("/admin/stats/load").await
7888
}
@@ -130,15 +140,20 @@ impl Client {
130140
.context("failed to extract json body from delete response")
131141
}
132142

133-
async fn get<R: DeserializeOwned>(&self, path: &str) -> Result<R> {
134-
reqwest::Client::new()
143+
async fn get_raw(&self, path: &str) -> Result<Bytes> {
144+
let res = reqwest::Client::new()
135145
.get(format!("{}{}", self.api_url, path))
136146
.bearer_auth(&self.api_key)
137147
.send()
138148
.await
139-
.context("failed to make get request")?
140-
.to_json()
141-
.await
142-
.context("failed to post text body from response")
149+
.context("making request")?;
150+
if !res.status().is_success() {
151+
bail!("API call returned non-2xx: {:?}", res);
152+
}
153+
res.bytes().await.context("getting response body")
154+
}
155+
156+
async fn get<R: DeserializeOwned>(&self, path: &str) -> Result<R> {
157+
serde_json::from_slice(&self.get_raw(path).await?).context("deserializing body")
143158
}
144159
}

admin/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use std::{fs, path::PathBuf};
22

33
pub fn get_api_key() -> String {
4+
if let Ok(s) = std::env::var("SHUTTLE_API_KEY") {
5+
return s;
6+
}
7+
48
let data = fs::read_to_string(config_path()).expect("shuttle config file to exist");
59
let toml: toml::Value = toml::from_str(&data).expect("to parse shuttle config file");
610

admin/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ async fn main() {
165165
client.idle_cch().await.expect("cch projects to be idled");
166166
"Idled CCH projects".to_string()
167167
}
168+
Command::ChangeProjectOwner {
169+
project_name,
170+
new_user_id,
171+
} => {
172+
client
173+
.change_project_owner(&project_name, &new_user_id)
174+
.await
175+
.unwrap();
176+
format!("Changed project owner: {project_name} -> {new_user_id}")
177+
}
168178
};
169179

170180
println!("{res}");

0 commit comments

Comments
 (0)