Skip to content

Commit 734571a

Browse files
committed
style: apply cargo fmt to codebase
1 parent 1651d6c commit 734571a

18 files changed

Lines changed: 494 additions & 286 deletions

src/auth.rs

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,32 @@ pub fn check_status(profile_config: &config::ProfileConfig) -> AuthStatus {
3131
// 2. on-disk sandbox session (sandbox set <id>)
3232
// 3. user-scoped CLI session / api_key fallback
3333
let api_url = profile_config.api_url.to_string();
34-
let access_token = if let Some((sandbox_jwt, _)) =
35-
crate::sandbox_session::sandbox_token_in_use()
36-
{
37-
sandbox_jwt
38-
} else if crate::sandbox_session::load().is_some() {
39-
match crate::sandbox_session::ensure_access_token(&api_url) {
40-
Some(t) => t,
41-
None => return AuthStatus::Invalid(401),
42-
}
43-
} else {
44-
let api_key_fallback = profile_config
45-
.api_key
46-
.as_deref()
47-
.filter(|k| !k.is_empty() && *k != "PLACEHOLDER");
48-
49-
// PKCE-origin sessions don't write an api_key, so absence of a key
50-
// alone isn't "not configured" — only true if there's also no
51-
// cached JWT session to validate.
52-
if api_key_fallback.is_none() && crate::jwt::load_session().is_none() {
53-
return AuthStatus::NotConfigured;
54-
}
34+
let access_token =
35+
if let Some((sandbox_jwt, _)) = crate::sandbox_session::sandbox_token_in_use() {
36+
sandbox_jwt
37+
} else if crate::sandbox_session::load().is_some() {
38+
match crate::sandbox_session::ensure_access_token(&api_url) {
39+
Some(t) => t,
40+
None => return AuthStatus::Invalid(401),
41+
}
42+
} else {
43+
let api_key_fallback = profile_config
44+
.api_key
45+
.as_deref()
46+
.filter(|k| !k.is_empty() && *k != "PLACEHOLDER");
5547

56-
match crate::jwt::ensure_access_token(profile_config, api_key_fallback) {
57-
Ok(t) => t,
58-
Err(_) => return AuthStatus::Invalid(401),
59-
}
60-
};
48+
// PKCE-origin sessions don't write an api_key, so absence of a key
49+
// alone isn't "not configured" — only true if there's also no
50+
// cached JWT session to validate.
51+
if api_key_fallback.is_none() && crate::jwt::load_session().is_none() {
52+
return AuthStatus::NotConfigured;
53+
}
54+
55+
match crate::jwt::ensure_access_token(profile_config, api_key_fallback) {
56+
Ok(t) => t,
57+
Err(_) => return AuthStatus::Invalid(401),
58+
}
59+
};
6160

6261
let url = format!("{}/workspaces", profile_config.api_url);
6362
let client = reqwest::blocking::Client::new();
@@ -120,8 +119,9 @@ pub fn status(profile: &str) {
120119
.api_key
121120
.as_deref()
122121
.map(crate::util::mask_credential),
123-
ApiKeySource::Config => crate::jwt::load_session()
124-
.map(|s| crate::util::mask_credential(&s.refresh_token)),
122+
ApiKeySource::Config => {
123+
crate::jwt::load_session().map(|s| crate::util::mask_credential(&s.refresh_token))
124+
}
125125
};
126126
(label.to_string(), tail)
127127
};
@@ -142,8 +142,20 @@ pub fn status(profile: &str) {
142142
);
143143
match profile_config.workspaces.first() {
144144
Some(w) => {
145-
print_row("Workspace", &format!("{} {}", w.name.as_str().cyan(), format!("({})", w.public_id).dark_grey()));
146-
print_row("", &"use 'hotdata workspaces set' to switch workspaces".dark_grey().to_string());
145+
print_row(
146+
"Workspace",
147+
&format!(
148+
"{} {}",
149+
w.name.as_str().cyan(),
150+
format!("({})", w.public_id).dark_grey()
151+
),
152+
);
153+
print_row(
154+
"",
155+
&"use 'hotdata workspaces set' to switch workspaces"
156+
.dark_grey()
157+
.to_string(),
158+
);
147159
}
148160
None => print_row("Current Workspace", &"None".dark_grey().to_string()),
149161
}
@@ -163,10 +175,15 @@ pub fn status(profile: &str) {
163175
}
164176

165177
#[derive(Deserialize)]
166-
struct WsListResponse { workspaces: Vec<WsItem> }
178+
struct WsListResponse {
179+
workspaces: Vec<WsItem>,
180+
}
167181

168182
#[derive(Deserialize)]
169-
struct WsItem { public_id: String, name: String }
183+
struct WsItem {
184+
public_id: String,
185+
name: String,
186+
}
170187

171188
/// Wait for the browser callback, verify state, and extract the authorization code.
172189
///
@@ -179,12 +196,16 @@ fn receive_callback(
179196
success_title: &str,
180197
success_body: &str,
181198
) -> Result<String, String> {
182-
let request = server.recv().map_err(|e| format!("failed to receive callback: {e}"))?;
199+
let request = server
200+
.recv()
201+
.map_err(|e| format!("failed to receive callback: {e}"))?;
183202
let raw_url = request.url().to_string();
184203
let params = parse_query_params(&raw_url);
185204

186205
if params.get("state").map(String::as_str) != Some(expected_state) {
187-
let _ = request.respond(tiny_http::Response::from_string("Login failed: state mismatch"));
206+
let _ = request.respond(tiny_http::Response::from_string(
207+
"Login failed: state mismatch",
208+
));
188209
return Err("state mismatch — possible CSRF attack".into());
189210
}
190211

@@ -338,11 +359,17 @@ fn run_browser_auth(
338359
Some(w) => {
339360
print_row(
340361
"Workspace",
341-
&format!("{} {}", w.name.as_str().cyan(), format!("({})", w.public_id).dark_grey()),
362+
&format!(
363+
"{} {}",
364+
w.name.as_str().cyan(),
365+
format!("({})", w.public_id).dark_grey()
366+
),
342367
);
343368
print_row(
344369
"",
345-
&"use 'hotdata workspaces set' to switch workspaces".dark_grey().to_string(),
370+
&"use 'hotdata workspaces set' to switch workspaces"
371+
.dark_grey()
372+
.to_string(),
346373
);
347374
}
348375
None => print_row("Workspace", &"None".dark_grey().to_string()),
@@ -619,10 +646,7 @@ mod tests {
619646
let (_tmp, _guard) = with_temp_config_dir();
620647
save_test_session("revoked-jwt");
621648
let mut server = mockito::Server::new();
622-
let mock = server
623-
.mock("GET", "/workspaces")
624-
.with_status(401)
625-
.create();
649+
let mock = server.mock("GET", "/workspaces").with_status(401).create();
626650

627651
let profile = mock_profile(&server.url(), None);
628652
assert_eq!(check_status(&profile), AuthStatus::Invalid(401));
@@ -634,10 +658,7 @@ mod tests {
634658
let (_tmp, _guard) = with_temp_config_dir();
635659
save_test_session("jwt");
636660
let mut server = mockito::Server::new();
637-
let mock = server
638-
.mock("GET", "/workspaces")
639-
.with_status(403)
640-
.create();
661+
let mock = server.mock("GET", "/workspaces").with_status(403).create();
641662

642663
let profile = mock_profile(&server.url(), None);
643664
assert_eq!(check_status(&profile), AuthStatus::Invalid(403));
@@ -651,10 +672,7 @@ mod tests {
651672
// the user they need to re-auth.
652673
let (_tmp, _guard) = with_temp_config_dir();
653674
let mut server = mockito::Server::new();
654-
let mock = server
655-
.mock("POST", "/o/token/")
656-
.with_status(401)
657-
.create();
675+
let mock = server.mock("POST", "/o/token/").with_status(401).create();
658676

659677
let profile = mock_profile(&server.url(), Some("hd_revoked"));
660678
assert_eq!(check_status(&profile), AuthStatus::Invalid(401));
@@ -703,10 +721,7 @@ mod tests {
703721
let (_tmp, _guard) = with_temp_config_dir();
704722
save_test_session("expired-jwt");
705723
let mut server = mockito::Server::new();
706-
let mock = server
707-
.mock("GET", "/workspaces")
708-
.with_status(401)
709-
.create();
724+
let mock = server.mock("GET", "/workspaces").with_status(401).create();
710725

711726
let profile = mock_profile(&server.url(), None);
712727
assert!(!is_already_signed_in(&profile));

src/command.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,11 @@ pub enum DatasetsCommands {
469469
description: Option<String>,
470470

471471
/// SQL query to create the dataset from
472-
#[arg(long, conflicts_with = "query_id", required_unless_present = "query_id")]
472+
#[arg(
473+
long,
474+
conflicts_with = "query_id",
475+
required_unless_present = "query_id"
476+
)]
473477
sql: Option<String>,
474478

475479
/// Saved query ID to create the dataset from

src/config.rs

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ pub fn clear_workspaces(profile: &str) -> Result<(), String> {
127127
return Ok(());
128128
}
129129

130-
let content = fs::read_to_string(&config_path)
131-
.map_err(|e| format!("error reading config file: {e}"))?;
130+
let content =
131+
fs::read_to_string(&config_path).map_err(|e| format!("error reading config file: {e}"))?;
132132
let mut config_file: ConfigFile =
133133
serde_yaml::from_str(&content).map_err(|e| format!("error parsing config file: {e}"))?;
134134

@@ -174,11 +174,15 @@ pub fn save_default_workspace(profile: &str, workspace: WorkspaceEntry) -> Resul
174174
.map_err(|e| format!("error reading config file: {e}"))?;
175175
serde_yaml::from_str(&content).map_err(|e| format!("error parsing config file: {e}"))?
176176
} else {
177-
ConfigFile { profiles: HashMap::new() }
177+
ConfigFile {
178+
profiles: HashMap::new(),
179+
}
178180
};
179181

180182
let entry = config_file.profiles.entry(profile.to_string()).or_default();
181-
entry.workspaces.retain(|w| w.public_id != workspace.public_id);
183+
entry
184+
.workspaces
185+
.retain(|w| w.public_id != workspace.public_id);
182186
entry.workspaces.insert(0, workspace);
183187

184188
let content = serde_yaml::to_string(&config_file)
@@ -194,7 +198,9 @@ pub fn save_sandbox(profile: &str, sandbox_id: &str) -> Result<(), String> {
194198
.map_err(|e| format!("error reading config file: {e}"))?;
195199
serde_yaml::from_str(&content).map_err(|e| format!("error parsing config file: {e}"))?
196200
} else {
197-
ConfigFile { profiles: HashMap::new() }
201+
ConfigFile {
202+
profiles: HashMap::new(),
203+
}
198204
};
199205

200206
config_file
@@ -215,8 +221,8 @@ pub fn clear_sandbox(profile: &str) -> Result<(), String> {
215221
return Ok(());
216222
}
217223

218-
let content = fs::read_to_string(&config_path)
219-
.map_err(|e| format!("error reading config file: {e}"))?;
224+
let content =
225+
fs::read_to_string(&config_path).map_err(|e| format!("error reading config file: {e}"))?;
220226
let mut config_file: ConfigFile =
221227
serde_yaml::from_str(&content).map_err(|e| format!("error parsing config file: {e}"))?;
222228

@@ -229,15 +235,21 @@ pub fn clear_sandbox(profile: &str) -> Result<(), String> {
229235
write_config(&config_path, &content)
230236
}
231237

232-
pub fn save_current_database(profile: &str, workspace_id: &str, database_id: &str) -> Result<(), String> {
238+
pub fn save_current_database(
239+
profile: &str,
240+
workspace_id: &str,
241+
database_id: &str,
242+
) -> Result<(), String> {
233243
let config_path = config_path()?;
234244

235245
let mut config_file: ConfigFile = if config_path.exists() {
236246
let content = fs::read_to_string(&config_path)
237247
.map_err(|e| format!("error reading config file: {e}"))?;
238248
serde_yaml::from_str(&content).map_err(|e| format!("error parsing config file: {e}"))?
239249
} else {
240-
ConfigFile { profiles: HashMap::new() }
250+
ConfigFile {
251+
profiles: HashMap::new(),
252+
}
241253
};
242254

243255
config_file
@@ -259,7 +271,12 @@ pub fn load_current_database(profile: &str, workspace_id: &str) -> Option<String
259271
}
260272
let content = fs::read_to_string(&config_path).ok()?;
261273
let config_file: ConfigFile = serde_yaml::from_str(&content).ok()?;
262-
config_file.profiles.get(profile)?.current_databases.get(workspace_id).cloned()
274+
config_file
275+
.profiles
276+
.get(profile)?
277+
.current_databases
278+
.get(workspace_id)
279+
.cloned()
263280
}
264281

265282
pub fn clear_current_database(profile: &str, workspace_id: &str) -> Result<(), String> {
@@ -269,8 +286,8 @@ pub fn clear_current_database(profile: &str, workspace_id: &str) -> Result<(), S
269286
return Ok(());
270287
}
271288

272-
let content = fs::read_to_string(&config_path)
273-
.map_err(|e| format!("error reading config file: {e}"))?;
289+
let content =
290+
fs::read_to_string(&config_path).map_err(|e| format!("error reading config file: {e}"))?;
274291
let mut config_file: ConfigFile =
275292
serde_yaml::from_str(&content).map_err(|e| format!("error parsing config file: {e}"))?;
276293

@@ -283,7 +300,10 @@ pub fn clear_current_database(profile: &str, workspace_id: &str) -> Result<(), S
283300
write_config(&config_path, &content)
284301
}
285302

286-
pub fn resolve_workspace_id(provided: Option<String>, profile_config: &ProfileConfig) -> Result<String, String> {
303+
pub fn resolve_workspace_id(
304+
provided: Option<String>,
305+
profile_config: &ProfileConfig,
306+
) -> Result<String, String> {
287307
if let Some(id) = provided {
288308
return Ok(id);
289309
}
@@ -306,14 +326,20 @@ pub fn load(profile: &str) -> Result<ProfileConfig, String> {
306326
let config_file = config_path()?;
307327

308328
let mut profile_config = if config_file.exists() {
309-
let content =
310-
fs::read_to_string(&config_file).map_err(|e| format!("error reading config file: {e}"))?;
329+
let content = fs::read_to_string(&config_file)
330+
.map_err(|e| format!("error reading config file: {e}"))?;
311331
let config_file: ConfigFile = serde_yaml::from_str(&content).unwrap_or_else(|_| {
312332
eprintln!("{}", "error parsing config file.".red());
313-
eprintln!("Run 'hotdata auth login' (or 'hotdata auth') to generate a new config file.");
333+
eprintln!(
334+
"Run 'hotdata auth login' (or 'hotdata auth') to generate a new config file."
335+
);
314336
std::process::exit(1);
315337
});
316-
config_file.profiles.get(profile).cloned().unwrap_or_default()
338+
config_file
339+
.profiles
340+
.get(profile)
341+
.cloned()
342+
.unwrap_or_default()
317343
} else {
318344
ProfileConfig::default()
319345
};
@@ -364,11 +390,14 @@ pub mod test_helpers {
364390

365391
#[cfg(test)]
366392
mod tests {
367-
use super::*;
368393
use super::test_helpers::with_temp_config_dir;
394+
use super::*;
369395

370396
fn ws(id: &str, name: &str) -> WorkspaceEntry {
371-
WorkspaceEntry { public_id: id.into(), name: name.into() }
397+
WorkspaceEntry {
398+
public_id: id.into(),
399+
name: name.into(),
400+
}
372401
}
373402

374403
#[test]
@@ -490,7 +519,10 @@ mod tests {
490519
#[test]
491520
fn resolve_workspace_id_prefers_provided() {
492521
let profile = ProfileConfig {
493-
workspaces: vec![WorkspaceEntry { public_id: "ws-1".into(), name: "WS".into() }],
522+
workspaces: vec![WorkspaceEntry {
523+
public_id: "ws-1".into(),
524+
name: "WS".into(),
525+
}],
494526
..Default::default()
495527
};
496528
let result = resolve_workspace_id(Some("explicit-id".into()), &profile).unwrap();
@@ -500,7 +532,10 @@ mod tests {
500532
#[test]
501533
fn resolve_workspace_id_falls_back_to_first() {
502534
let profile = ProfileConfig {
503-
workspaces: vec![WorkspaceEntry { public_id: "ws-1".into(), name: "WS".into() }],
535+
workspaces: vec![WorkspaceEntry {
536+
public_id: "ws-1".into(),
537+
name: "WS".into(),
538+
}],
504539
..Default::default()
505540
};
506541
let result = resolve_workspace_id(None, &profile).unwrap();

0 commit comments

Comments
 (0)