Skip to content

Commit 751f337

Browse files
authored
feat(cargo-shuttle): --secrets arg to use non-default secrets file (#1642)
* feat(cargo-shuttle): secrets arg local run * feat(cargo-shuttle): secrets arg deploy * fix: remove redundant path check
1 parent 0b97911 commit 751f337

File tree

3 files changed

+80
-19
lines changed

3 files changed

+80
-19
lines changed

cargo-shuttle/src/args.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct ShuttleArgs {
3838
}
3939

4040
// Common args for subcommands that deal with projects.
41-
#[derive(Parser, Debug)]
41+
#[derive(Parser, Clone, Debug)]
4242
pub struct ProjectArgs {
4343
/// Specify the working directory
4444
#[arg(global = true, long, visible_alias = "wd", default_value = ".", value_parser = OsStringValueParser::new().try_map(parse_path))]
@@ -259,14 +259,17 @@ pub struct LogoutArgs {
259259
#[arg(long)]
260260
pub reset_api_key: bool,
261261
}
262-
#[derive(Parser)]
262+
#[derive(Parser, Default)]
263263
pub struct DeployArgs {
264264
/// Allow deployment with uncommitted files
265265
#[arg(long, visible_alias = "ad")]
266266
pub allow_dirty: bool,
267267
/// Don't run pre-deploy tests
268268
#[arg(long, visible_alias = "nt")]
269269
pub no_test: bool,
270+
271+
#[command(flatten)]
272+
pub secret_args: SecretsArgs,
270273
}
271274

272275
#[derive(Parser, Debug)]
@@ -280,6 +283,16 @@ pub struct RunArgs {
280283
/// Use release mode for building the project
281284
#[arg(long, short = 'r')]
282285
pub release: bool,
286+
287+
#[command(flatten)]
288+
pub secret_args: SecretsArgs,
289+
}
290+
291+
#[derive(Parser, Debug, Default)]
292+
pub struct SecretsArgs {
293+
/// Use this secrets file instead
294+
#[arg(long, value_parser = OsStringValueParser::new().try_map(parse_path))]
295+
pub secrets: Option<PathBuf>,
283296
}
284297

285298
#[derive(Parser, Clone, Debug, Default)]

cargo-shuttle/src/lib.rs

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -935,12 +935,14 @@ impl Shuttle {
935935
service: &BuiltService,
936936
idx: u16,
937937
) -> Result<Option<(Child, runtime::Client)>> {
938-
let crate_directory = service.crate_directory();
939-
let secrets_path = if crate_directory.join("Secrets.dev.toml").exists() {
940-
crate_directory.join("Secrets.dev.toml")
941-
} else {
942-
crate_directory.join("Secrets.toml")
943-
};
938+
let secrets_path = run_args.secret_args.secrets.clone().unwrap_or_else(|| {
939+
let crate_dir = service.crate_directory();
940+
if crate_dir.join("Secrets.dev.toml").exists() {
941+
crate_dir.join("Secrets.dev.toml")
942+
} else {
943+
crate_dir.join("Secrets.toml")
944+
}
945+
});
944946
trace!("Loading secrets from {}", secrets_path.display());
945947

946948
let secrets: HashMap<String, String> = if let Ok(secrets_str) = read_to_string(secrets_path)
@@ -1610,7 +1612,7 @@ impl Shuttle {
16101612
}
16111613
}
16121614

1613-
deployment_req.data = self.make_archive()?;
1615+
deployment_req.data = self.make_archive(args.secret_args.secrets.clone())?;
16141616
if deployment_req.data.len() > CREATE_SERVICE_BODY_LIMIT {
16151617
bail!(
16161618
r#"The project is too large - the limit is {} MB. \
@@ -2050,7 +2052,7 @@ impl Shuttle {
20502052
Ok(CommandOutcome::Ok)
20512053
}
20522054

2053-
fn make_archive(&self) -> Result<Vec<u8>> {
2055+
fn make_archive(&self, secrets_file: Option<PathBuf>) -> Result<Vec<u8>> {
20542056
let include_patterns = self.ctx.assets();
20552057
let encoder = GzEncoder::new(Vec::new(), Compression::new(3));
20562058
let mut tar = Builder::new(encoder);
@@ -2086,8 +2088,12 @@ impl Shuttle {
20862088

20872089
let mut globs = GlobSetBuilder::new();
20882090

2089-
// Always include secrets
2090-
globs.add(Glob::new("**/Secrets.toml").unwrap());
2091+
if let Some(secrets_file) = secrets_file.clone() {
2092+
entries.push(secrets_file);
2093+
} else {
2094+
// Default: Include all Secrets.toml files
2095+
globs.add(Glob::new("**/Secrets.toml").unwrap());
2096+
}
20912097

20922098
// User provided includes
20932099
if let Some(rules) = include_patterns {
@@ -2117,11 +2123,17 @@ impl Shuttle {
21172123
continue;
21182124
}
21192125

2120-
let name = path
2126+
let mut name = path
21212127
.strip_prefix(working_directory.parent().context("get parent dir")?)
21222128
.context("strip prefix of path")?
21232129
.to_owned();
21242130

2131+
// if this is the custom secrets file, rename it to Secrets.toml
2132+
if secrets_file.as_ref().is_some_and(|sf| sf == &path) {
2133+
name.pop();
2134+
name.push("Secrets.toml");
2135+
}
2136+
21252137
archive_files.insert(path, name);
21262138
}
21272139

@@ -2307,7 +2319,7 @@ mod tests {
23072319
use flate2::read::GzDecoder;
23082320
use tar::Archive;
23092321

2310-
use crate::args::ProjectArgs;
2322+
use crate::args::{DeployArgs, ProjectArgs, SecretsArgs};
23112323
use crate::Shuttle;
23122324
use std::fs::{self, canonicalize};
23132325
use std::path::PathBuf;
@@ -2320,11 +2332,13 @@ mod tests {
23202332
dunce::canonicalize(path).unwrap()
23212333
}
23222334

2323-
fn get_archive_entries(project_args: ProjectArgs) -> Vec<String> {
2335+
fn get_archive_entries(project_args: ProjectArgs, deploy_args: DeployArgs) -> Vec<String> {
23242336
let mut shuttle = Shuttle::new().unwrap();
23252337
shuttle.load_project(&project_args).unwrap();
23262338

2327-
let archive = shuttle.make_archive().unwrap();
2339+
let archive = shuttle
2340+
.make_archive(deploy_args.secret_args.secrets)
2341+
.unwrap();
23282342

23292343
let tar = GzDecoder::new(&archive[..]);
23302344
let mut archive = Archive::new(tar);
@@ -2364,10 +2378,10 @@ mod tests {
23642378
fs::write(working_directory.join("target").join("binary"), b"12345").unwrap();
23652379

23662380
let project_args = ProjectArgs {
2367-
working_directory,
2381+
working_directory: working_directory.clone(),
23682382
name: Some("archiving-test".to_owned()),
23692383
};
2370-
let mut entries = get_archive_entries(project_args);
2384+
let mut entries = get_archive_entries(project_args.clone(), Default::default());
23712385
entries.sort();
23722386

23732387
assert_eq!(
@@ -2390,10 +2404,43 @@ mod tests {
23902404
"src/main.rs",
23912405
]
23922406
);
2407+
2408+
fs::remove_file(working_directory.join("Secrets.toml")).unwrap();
2409+
let mut entries = get_archive_entries(
2410+
project_args,
2411+
DeployArgs {
2412+
secret_args: SecretsArgs {
2413+
secrets: Some(working_directory.join("Secrets.toml.example")),
2414+
},
2415+
..Default::default()
2416+
},
2417+
);
2418+
entries.sort();
2419+
2420+
assert_eq!(
2421+
entries,
2422+
vec![
2423+
".gitignore",
2424+
".ignore",
2425+
"Cargo.toml",
2426+
"Secrets.toml", // got moved here
2427+
// Secrets.toml.example was the given secrets file, so it got moved
2428+
"Shuttle.toml",
2429+
"asset1", // normal file
2430+
"asset2", // .gitignore'd, but included in Shuttle.toml
2431+
// asset3 is .ignore'd
2432+
"asset4", // .gitignore'd, but un-ignored in .ignore
2433+
"asset5", // .ignore'd, but included in Shuttle.toml
2434+
"dist/dist1", // .gitignore'd, but included in Shuttle.toml
2435+
"nested/static/nested1", // normal file
2436+
// nested/static/nestedignore is .gitignore'd
2437+
"src/main.rs",
2438+
]
2439+
);
23932440
}
23942441

23952442
#[test]
2396-
fn load_project_returns_proper_working_directory_in_project_args() {
2443+
fn finds_workspace_root() {
23972444
let project_args = ProjectArgs {
23982445
working_directory: path_from_workspace_root("examples/axum/hello-world/src"),
23992446
name: None,

common-tests/src/cargo_shuttle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub async fn cargo_shuttle_run(working_directory: &str, external: bool) -> Strin
3030
port,
3131
external,
3232
release: false,
33+
secret_args: Default::default(),
3334
};
3435

3536
let runner = Shuttle::new().unwrap().run(

0 commit comments

Comments
 (0)