@smbcloud / smbcloud-ascapi Mirror · read-only / commits / 9d2c2ce
Fork 0

Read-only mirror of github.com/smbcloudXYZ/smbcloud-ascapi.

Mirrored from https://github.com/smbcloudXYZ/smbcloud-ascapi.git

Synced Nov 25, 09:15 UTC

Ask which APK to replace when an edit has several

apk_upload picked the first APK on the edit, so with more than one it could overwrite another device tier's package. It now replaces the only APK, or the one named with --apk-id, and fails with the ids listed when there are several and none is named. The upload tests also get a unique temp file each, since they run in parallel in one process. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Co-Authored-By: siGit Code v1.7.6 <noreply@sigit.si>

Seto Elkahfi committed Nov 12, 2026 at 10:36 UTC 9d2c2ce7161153db5162b4aa089be2780511af63
4 files changed +78 -17
crates/amazon/src/apk.rs
+5 -1
@@ -98,7 +98,11 @@ mod tests {
98 }
99
100 fn temp_apk() -> std::path::PathBuf {
101 - let path = std::env::temp_dir().join(format!("amazon-test-{}.apk", std::process::id()));
101 + // Tests run in parallel inside one process, so the pid alone would
102 + // make them share (and truncate) one file.
103 + static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
104 + let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
105 + let path = std::env::temp_dir().join(format!("amazon-test-{}-{n}.apk", std::process::id()));
106 std::fs::write(&path, b"PK-apk-bytes").unwrap();
107 path
108 }
crates/cli/src/main.rs
+11 -3
@@ -855,11 +855,15 @@ enum AmazonEditsCommand {
855 enum AmazonApksCommand {
856 /// List the edit's APKs.
857 List { app_id: String },
858 - /// Upload an APK, or replace the edit's APK (ETag + If-Match) when it has one.
858 + /// Upload an APK. When the edit has one APK it is replaced (ETag +
859 + /// If-Match); with several, `--apk-id` must name the one to replace.
860 Upload {
861 app_id: String,
862 /// The signed .apk.
863 file: PathBuf,
864 + /// The APK to replace, from `apks list`.
865 + #[arg(long)]
866 + apk_id: Option<String>,
867 },
868 /// Remove an APK from the edit.
869 Delete { app_id: String, apk_id: String },
@@ -1422,8 +1426,12 @@ async fn run_amazon(
1426 AmazonApksCommand::List { app_id } => {
1427 print_json(&amazon::apk_list(&client, &app_id).await.map_err(fail)?)
1428 }
1425 - AmazonApksCommand::Upload { app_id, file } => print_json(
1426 - &amazon::apk_upload(&client, &app_id, &file, dry_run)
1429 + AmazonApksCommand::Upload {
1430 + app_id,
1431 + file,
1432 + apk_id,
1433 + } => print_json(
1434 + &amazon::apk_upload(&client, &app_id, &file, apk_id.as_deref(), dry_run)
1435 .await
1436 .map_err(fail)?,
1437 ),
crates/frontend/src/amazon.rs
+60 -12
@@ -74,22 +74,51 @@ fn sha256_file(path: &Path) -> Result<(u64, String), String> {
74 Ok((bytes.len() as u64, hex))
75 }
76
77 -fn first_apk_id(list: &Value) -> Option<String> {
78 - let items = list.as_array().or_else(|| list.get("apks")?.as_array())?;
79 - items.first()?.get("id")?.as_str().map(str::to_string)
77 +/// Every APK id on the edit, from a bare array or an `apks` wrapper.
78 +fn apk_ids(list: &Value) -> Vec<String> {
79 + let items = list
80 + .as_array()
81 + .or_else(|| list.get("apks").and_then(Value::as_array));
82 + items
83 + .into_iter()
84 + .flatten()
85 + .filter_map(|apk| apk.get("id")?.as_str().map(str::to_string))
86 + .collect()
87 }
88
82 -/// Upload the APK to the open edit; when the edit already has one, replace it
83 -/// (Amazon allows one APK per edit slot here, and replace keeps its targeting).
89 +/// Which APK an upload replaces: the one named, the only one there is, or
90 +/// none (a fresh upload). More than one with no name is an error, because
91 +/// replacing an arbitrary one could overwrite another device tier's APK.
92 +fn pick_target(ids: &[String], named: Option<&str>) -> Result<Option<String>, String> {
93 + match (named, ids) {
94 + (Some(id), _) if ids.iter().any(|x| x == id) => Ok(Some(id.to_string())),
95 + (Some(id), _) => Err(format!(
96 + "no APK {id:?} on the edit; it has: {}",
97 + ids.join(", ")
98 + )),
99 + (None, []) => Ok(None),
100 + (None, [only]) => Ok(Some(only.clone())),
101 + (None, many) => Err(format!(
102 + "the edit has {} APKs ({}); pass --apk-id to say which one to replace",
103 + many.len(),
104 + many.join(", ")
105 + )),
106 + }
107 +}
108 +
109 +/// Upload the APK to the open edit. With no APK on the edit this uploads; with
110 +/// one it replaces it; with several, `apk_id` must name the one to replace.
111 pub async fn apk_upload(
112 client: &AmazonClient,
113 app_id: &str,
114 apk: &Path,
115 + apk_id: Option<&str>,
116 dry_run: bool,
117 ) -> Result<UploadPlan, String> {
118 let (bytes, sha256) = sha256_file(apk)?;
119 let edit = open_edit(client, app_id).await?;
92 - let existing = first_apk_id(&client.list_apks(app_id, &edit.id).await.map_err(err)?);
120 + let ids = apk_ids(&client.list_apks(app_id, &edit.id).await.map_err(err)?);
121 + let existing = pick_target(&ids, apk_id)?;
122 let action = if existing.is_some() {
123 "replace"
124 } else {
@@ -220,16 +249,35 @@ pub async fn edit_delete(
249 mod tests {
250 use super::*;
251
252 + fn ids(list: &[&str]) -> Vec<String> {
253 + list.iter().map(|s| s.to_string()).collect()
254 + }
255 +
256 #[test]
224 - fn first_apk_id_reads_a_bare_array_or_an_apks_wrapper() {
257 + fn apk_ids_reads_a_bare_array_or_an_apks_wrapper() {
258 assert_eq!(
226 - first_apk_id(&serde_json::json!([{"id": "a1"}])).as_deref(),
227 - Some("a1")
259 + apk_ids(&serde_json::json!([{"id": "a1"}, {"id": "a3"}])),
260 + ids(&["a1", "a3"])
261 );
262 assert_eq!(
230 - first_apk_id(&serde_json::json!({"apks": [{"id": "a2"}]})).as_deref(),
231 - Some("a2")
263 + apk_ids(&serde_json::json!({"apks": [{"id": "a2"}]})),
264 + ids(&["a2"])
265 );
233 - assert_eq!(first_apk_id(&serde_json::json!([])), None);
266 + assert!(apk_ids(&serde_json::json!([])).is_empty());
267 + }
268 +
269 + #[test]
270 + fn one_apk_is_replaced_and_none_is_uploaded() {
271 + assert_eq!(pick_target(&[], None), Ok(None));
272 + assert_eq!(pick_target(&ids(&["a1"]), None), Ok(Some("a1".to_string())));
273 + }
274 +
275 + #[test]
276 + fn several_apks_need_a_name_and_the_name_must_exist() {
277 + let two = ids(&["a1", "a2"]);
278 + let err = pick_target(&two, None).unwrap_err();
279 + assert!(err.contains("--apk-id") && err.contains("a1") && err.contains("a2"));
280 + assert_eq!(pick_target(&two, Some("a2")), Ok(Some("a2".to_string())));
281 + assert!(pick_target(&two, Some("zz")).is_err());
282 }
283 }
docs/amazon.md
+2 -1
@@ -9,7 +9,8 @@ and point `AMAZON_APPSTORE_CREDENTIALS_JSON_PATH` at it (or pass
9 ```bash
10 asc amazon edits get <APP_ID> # the open edit, or null
11 asc amazon edits create <APP_ID>
12 -asc amazon apks upload <APP_ID> app.apk # replaces (ETag) when the edit already has an APK
12 +asc amazon apks upload <APP_ID> app.apk # replaces (ETag) the edit's only APK
13 +asc amazon apks upload <APP_ID> app.apk --apk-id <APK_ID> # required when the edit has several
14 asc amazon apks list <APP_ID>
15 asc amazon targeting get <APP_ID> <APK_ID>
16 asc amazon targeting set <APP_ID> <APK_ID> --file targeting.json