Skip to content
Closed
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
4 changes: 4 additions & 0 deletions crates/cfkv/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub struct Cli {
#[arg(short, long)]
pub debug: bool,

/// Use local KV instance (wrangler dev)
#[arg(short, long)]
pub local: bool,

#[command(subcommand)]
pub command: Commands,
}
Expand Down
8 changes: 7 additions & 1 deletion crates/cfkv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
return Err("No storage configured. Add one with: cfkv storage add <name> --account-id <ID> --namespace-id <ID> --api-token <TOKEN>".into());
};

let client_config = ClientConfig::new(
let mut client_config = ClientConfig::new(
&account_id,
&namespace_id,
cloudflare_kv::AuthCredentials::token(api_token),
);

// Apply local flag if specified
if cli.local {
client_config = client_config.with_local(true);
}

let client = KvClient::new(client_config);

match cli.command {
Expand Down
45 changes: 44 additions & 1 deletion crates/cloudflare-kv/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,60 @@ mod tests {
}

#[test]
fn test_endpoint_urls() {
fn test_endpoint_urls_remote() {
let config = test_config();
let kv_endpoint = config.kv_endpoint();
let list_endpoint = config.kv_list_endpoint();

assert!(
kv_endpoint.contains("accounts/account-id/storage/kv/namespaces/namespace-id/values")
);
assert!(kv_endpoint.contains("https://api.cloudflare.com/client/v4"));
assert!(
list_endpoint.contains("accounts/account-id/storage/kv/namespaces/namespace-id/keys")
);
assert!(list_endpoint.contains("https://api.cloudflare.com/client/v4"));
}

#[test]
fn test_endpoint_urls_local() {
let creds = AuthCredentials::token("test-token");
let config = ClientConfig::new("account-id", "namespace-id", creds).with_local(true);
let kv_endpoint = config.kv_endpoint();
let list_endpoint = config.kv_list_endpoint();

assert!(
kv_endpoint.contains("accounts/account-id/storage/kv/namespaces/namespace-id/values")
);
assert!(kv_endpoint.contains("http://localhost:8787"));
assert!(
list_endpoint.contains("accounts/account-id/storage/kv/namespaces/namespace-id/keys")
);
assert!(list_endpoint.contains("http://localhost:8787"));
}

#[test]
fn test_local_remote_switching() {
let creds = AuthCredentials::token("test-token");
let mut config = ClientConfig::new("account-id", "namespace-id", creds);

// Start with remote
assert!(!config.is_local);
assert!(config
.base_url()
.contains("https://api.cloudflare.com/client/v4"));

// Switch to local
config = config.with_local(true);
assert!(config.is_local);
assert!(config.base_url().contains("http://localhost:8787"));

// Switch back to remote
config = config.with_local(false);
assert!(!config.is_local);
assert!(config
.base_url()
.contains("https://api.cloudflare.com/client/v4"));
}

#[test]
Expand Down
31 changes: 27 additions & 4 deletions crates/cloudflare-kv/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ pub struct ClientConfig {
pub account_id: String,
pub namespace_id: String,
pub credentials: AuthCredentials,
pub base_url: String,
pub remote_base_url: String,
pub local_base_url: String,
pub is_local: bool,
}

impl ClientConfig {
Expand All @@ -49,23 +51,44 @@ impl ClientConfig {
account_id: account_id.into(),
namespace_id: namespace_id.into(),
credentials,
base_url: "https://api.cloudflare.com/client/v4".to_string(),
remote_base_url: "https://api.cloudflare.com/client/v4".to_string(),
local_base_url: "http://localhost:8787".to_string(),
is_local: false,
}
}

/// Get the current base URL based on local/remote setting
pub fn base_url(&self) -> &str {
if self.is_local {
&self.local_base_url
} else {
&self.remote_base_url
}
}

/// Set local mode
pub fn with_local(mut self, is_local: bool) -> Self {
self.is_local = is_local;
self
}

/// Get KV API endpoint URL
pub fn kv_endpoint(&self) -> String {
format!(
"{}/accounts/{}/storage/kv/namespaces/{}/values",
self.base_url, self.account_id, self.namespace_id
self.base_url(),
self.account_id,
self.namespace_id
)
}

/// Get KV list endpoint URL
pub fn kv_list_endpoint(&self) -> String {
format!(
"{}/accounts/{}/storage/kv/namespaces/{}/keys",
self.base_url, self.account_id, self.namespace_id
self.base_url(),
self.account_id,
self.namespace_id
)
}
}
Expand Down
Loading