Skip to content

Commit 752ea29

Browse files
authored
refa: move env config logic to common, add to admin (#2039)
* refa: move env config logic to common, add to admin * minor nits * lul
1 parent 51e13f5 commit 752ea29

File tree

11 files changed

+253
-248
lines changed

11 files changed

+253
-248
lines changed

Cargo.lock

Lines changed: 3 additions & 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ publish = false
66

77
[dependencies]
88
shuttle-api-client = { workspace = true, default-features = true }
9-
shuttle-common = { workspace = true, features = ["models"] }
9+
shuttle-common = { workspace = true, features = ["models", "config"] }
1010

1111
anyhow = { workspace = true }
1212
clap = { workspace = true, features = ["env"] }

admin/src/args.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use clap::{Parser, Subcommand};
2-
use shuttle_common::{
3-
constants::SHUTTLE_API_URL,
4-
models::{project::ComputeTier, user::AccountTier},
5-
};
2+
use shuttle_common::models::{project::ComputeTier, user::AccountTier};
63

74
#[derive(Parser, Debug)]
85
pub struct Args {
9-
/// run this command against the api at the supplied url
10-
#[arg(long, env = "SHUTTLE_API", default_value = SHUTTLE_API_URL)]
11-
pub api_url: String,
6+
/// Target a different Shuttle API env (use a separate global config) (default: None (= prod = production))
7+
// ("SHUTTLE_ENV" is used for user-facing environments (agnostic of Shuttle API env))
8+
#[arg(global = true, long, env = "SHUTTLE_API_ENV")]
9+
pub api_env: Option<String>,
10+
/// URL for the Shuttle API to target (overrides inferred URL from api_env)
11+
#[arg(global = true, long, env = "SHUTTLE_API")]
12+
pub api_url: Option<String>,
1213

1314
#[command(subcommand)]
1415
pub command: Command,

admin/src/config.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

admin/src/lib.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,50 @@
11
pub mod args;
22
pub mod client;
3-
pub mod config;
3+
4+
use shuttle_common::{
5+
config::{Config, ConfigManager, GlobalConfig, GlobalConfigManager},
6+
constants::{other_env_api_url, SHUTTLE_API_URL},
7+
};
48

59
use crate::{
610
args::{Args, Command},
711
client::Client,
8-
config::get_api_key,
912
};
1013

1114
pub async fn run(args: Args) {
1215
tracing::trace!(?args, "starting with args");
1316

14-
let api_key = get_api_key();
17+
let api_key = match std::env::var("SHUTTLE_API_KEY") {
18+
Ok(s) => s,
19+
Err(_) => {
20+
let mut global = Config::<_, GlobalConfig>::new(
21+
GlobalConfigManager::new(args.api_env.clone()).unwrap(),
22+
);
23+
let path = global.manager.path();
24+
tracing::trace!(?path, "looking for config");
25+
if !global.exists() {
26+
global.create().unwrap();
27+
}
28+
global.open().expect("load global configuration");
29+
global
30+
.as_ref()
31+
.unwrap()
32+
.api_key
33+
.clone()
34+
.expect("api key in config")
35+
}
36+
};
37+
let api_url = args
38+
.api_url
39+
// calculate env-specific url if no explicit url given but an env was given
40+
.or_else(|| args.api_env.as_ref().map(|env| other_env_api_url(env)))
41+
.unwrap_or_else(|| SHUTTLE_API_URL.to_string());
42+
let api_url = format!("{api_url}/admin");
43+
tracing::trace!(?api_url, "");
44+
1545
let client = Client::new(
16-
format!("{}/admin", args.api_url),
46+
// always in admin mode
47+
api_url,
1748
api_key,
1849
args.client_timeout,
1950
);

cargo-shuttle/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ default-run = "shuttle"
1010

1111
[dependencies]
1212
shuttle-api-client = { workspace = true, default-features = true }
13-
shuttle-common = { workspace = true, features = ["models", "tables"] }
13+
shuttle-common = { workspace = true, features = ["models", "tables", "config"] }
1414

1515
anyhow = { workspace = true }
1616
async-trait = { workspace = true }

0 commit comments

Comments
 (0)