Skip to content

Commit e93709c

Browse files
alukachclaude
andcommitted
refactor: rename sanitize_role_arn to sanitize_for_filename, improve delete error handling
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 978965f commit e93709c

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

src/cache.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ fn is_keyring_unavailable(err: &keyring::Error) -> bool {
2020
}
2121

2222
/// Replace any character that isn't alphanumeric, `-`, or `_` with `_`.
23-
fn sanitize_role_arn(role_arn: &str) -> String {
24-
role_arn
23+
fn sanitize_for_filename(s: &str) -> String {
24+
s
2525
.chars()
2626
.map(|c| {
2727
if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
@@ -38,7 +38,7 @@ fn sanitize_role_arn(role_arn: &str) -> String {
3838
/// `~/.cache` on Linux, `%LocalAppData%` on Windows).
3939
fn cache_path(role_arn: &str) -> Result<PathBuf, String> {
4040
let cache_dir = dirs::cache_dir().ok_or("Could not determine cache directory")?;
41-
let sanitized = sanitize_role_arn(role_arn);
41+
let sanitized = sanitize_for_filename(role_arn);
4242
Ok(cache_dir
4343
.join("source-coop")
4444
.join("credentials")
@@ -170,7 +170,7 @@ pub struct RefreshTokenData {
170170

171171
/// Produce a filesystem-safe key from an issuer URL.
172172
fn issuer_key(issuer: &str) -> String {
173-
sanitize_role_arn(issuer)
173+
sanitize_for_filename(issuer)
174174
}
175175

176176
/// Full path to the refresh-token cache file for a given issuer.
@@ -300,7 +300,11 @@ pub fn delete_refresh_token(issuer: &str) -> Result<(), String> {
300300
// Try deleting from file
301301
let path = refresh_cache_path(issuer)?;
302302
match fs::remove_file(&path) {
303-
Ok(()) | Err(_) => {} // ignore file-not-found or other errors
303+
Ok(()) => {}
304+
Err(e) if e.kind() == io::ErrorKind::NotFound => {}
305+
Err(e) => {
306+
eprintln!("Warning: could not delete {}: {e}", path.display());
307+
}
304308
}
305309

306310
Ok(())
@@ -322,7 +326,11 @@ pub fn delete_credentials(role_arn: &str) -> Result<(), String> {
322326
// Try deleting from file
323327
let path = cache_path(role_arn)?;
324328
match fs::remove_file(&path) {
325-
Ok(()) | Err(_) => {} // ignore file-not-found or other errors
329+
Ok(()) => {}
330+
Err(e) if e.kind() == io::ErrorKind::NotFound => {}
331+
Err(e) => {
332+
eprintln!("Warning: could not delete {}: {e}", path.display());
333+
}
326334
}
327335

328336
Ok(())
@@ -343,20 +351,20 @@ mod tests {
343351

344352
#[test]
345353
fn sanitize_simple_name() {
346-
assert_eq!(sanitize_role_arn("source-coop-user"), "source-coop-user");
354+
assert_eq!(sanitize_for_filename("source-coop-user"), "source-coop-user");
347355
}
348356

349357
#[test]
350358
fn sanitize_arn_with_special_chars() {
351359
assert_eq!(
352-
sanitize_role_arn("arn:aws:iam::123:role/Foo"),
360+
sanitize_for_filename("arn:aws:iam::123:role/Foo"),
353361
"arn_aws_iam__123_role_Foo"
354362
);
355363
}
356364

357365
#[test]
358366
fn sanitize_preserves_underscores() {
359-
assert_eq!(sanitize_role_arn("my_role-name"), "my_role-name");
367+
assert_eq!(sanitize_for_filename("my_role-name"), "my_role-name");
360368
}
361369

362370
#[test]

0 commit comments

Comments
 (0)