Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions crates/dkdc-md-cli/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const ENV_VARS: &[&str] = &[
"MOTHERDUCK_API_KEY",
];

/// Trim whitespace and convert to an owned `String`.
fn trimmed(s: &str) -> String {
s.trim().to_string()
}

/// Resolve token: CLI flag takes precedence over env vars.
/// Pass `Some("-")` to read from stdin.
pub fn resolve_token_or(cli_token: Option<&str>) -> Result<String> {
Expand All @@ -24,9 +29,9 @@ fn resolve_token_or_with(
if token == "-" {
return read_token_from_reader(stdin);
}
let trimmed = token.trim();
anyhow::ensure!(!trimmed.is_empty(), "--token value must not be empty");
return Ok(trimmed.to_string());
let t = trimmed(token);
anyhow::ensure!(!t.is_empty(), "--token value must not be empty");
return Ok(t);
}
resolve_token_with(env_var)
}
Expand All @@ -36,19 +41,19 @@ fn read_token_from_reader(mut reader: impl Read) -> Result<String> {
reader
.read_to_string(&mut buf)
.context("failed to read token from stdin")?;
let trimmed = buf.trim().to_string();
anyhow::ensure!(!trimmed.is_empty(), "stdin was empty; expected a token");
Ok(trimmed)
let t = trimmed(&buf);
anyhow::ensure!(!t.is_empty(), "stdin was empty; expected a token");
Ok(t)
}

fn resolve_token_with(
env_var: impl Fn(&str) -> Result<String, std::env::VarError>,
) -> Result<String> {
for var in ENV_VARS {
if let Ok(val) = env_var(var) {
let trimmed = val.trim().to_string();
if !trimmed.is_empty() {
return Ok(trimmed);
let t = trimmed(&val);
if !t.is_empty() {
return Ok(t);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions crates/dkdc-md-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ureq::{Agent, http};
const BASE_URL: &str = "https://api.motherduck.com";
const TIMEOUT: Duration = Duration::from_secs(10);
const USER_AGENT: &str = concat!("dkdc-md-cli/", env!("CARGO_PKG_VERSION"));
const SUCCESS_STATUS: std::ops::Range<u16> = 200..300;

/// Characters that must be percent-encoded in a URL path segment.
const PATH_SEGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'#').add(b'%').add(b'/').add(b'?');
Expand Down Expand Up @@ -150,7 +151,7 @@ impl MotherduckClient {
// -- Ducklings --

pub fn get_duckling_config(&self, username: &str) -> Result<Value> {
self.get(&format!("/v1/users/{}/instances", encode_path(username),))
self.get(&format!("/v1/users/{}/instances", encode_path(username)))
}

pub fn set_duckling_config(
Expand Down Expand Up @@ -189,15 +190,15 @@ fn handle_response(mut resp: http::Response<ureq::Body>) -> Result<Value> {

fn parse_response(status: u16, text: String) -> Result<Value> {
match serde_json::from_str::<Value>(&text) {
Ok(body) if (200..300).contains(&status) => Ok(body),
Ok(body) if SUCCESS_STATUS.contains(&status) => Ok(body),
Ok(body) => {
let message = body
.get("message")
.and_then(|m| m.as_str())
.unwrap_or(&text);
bail!("API error ({status}): {message}");
}
Err(_) if (200..300).contains(&status) => Ok(Value::String(text)),
Err(_) if SUCCESS_STATUS.contains(&status) => Ok(Value::String(text)),
Err(_) => bail!("API error ({status}): {text}"),
}
}
Expand Down
Loading