Skip to content
This repository was archived by the owner on Jan 25, 2026. It is now read-only.

Commit 8375a4e

Browse files
committed
Add list-upstream-caches command
1 parent c201395 commit 8375a4e

5 files changed

Lines changed: 55 additions & 3 deletions

File tree

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
131131
snarf.succeed('export SNARF_CLIENT_TOKEN=$(snarf -s snarfd:9000 create-token); \
132132
snarf -s snarfd:9000 add-upstream-cache --token $SNARF_CLIENT_TOKEN "https://cache.nixos.org"; \
133+
snarf -s snarfd:9000 list-upstream-caches --token $SNARF_CLIENT_TOKEN | grep "https://cache.nixos.org"; \
133134
snarf -s snarfd:9000 add-closure --token $SNARF_CLIENT_TOKEN $(realpath $(which snarf))')
134135
'';
135136
};

proto/management.proto

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ service ManagementService {
1414
rpc CreateClientToken(NewClientTokenRequest) returns (ClientToken) {}
1515
rpc FilterHashes(stream NARHashRequest) returns (stream NARHashResponse);
1616
rpc AddUpstreamCache(AddUpstreamCacheRequest) returns (AddUpstreamCacheResponse);
17-
}
17+
rpc ListUpstreamCaches(ListUpstreamCachesRequest) returns (ListUpstreamCachesResponse);
18+
}
1819

1920
message NARHashRequest {
2021
bytes digest = 1;
@@ -31,3 +32,9 @@ message AddUpstreamCacheRequest {
3132
message AddUpstreamCacheResponse {
3233
bool success = 1;
3334
}
35+
36+
message ListUpstreamCachesRequest {}
37+
38+
message ListUpstreamCachesResponse {
39+
repeated string base_urls = 1;
40+
}

src/bin/client.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ enum ClientCommand {
3232
/// The base_url of the upstream cache
3333
base_url: String,
3434
},
35+
/// List all configured upstream caches
36+
ListUpstreamCaches {
37+
/// The authentication token
38+
#[arg(short, long, env = "SNARF_CLIENT_TOKEN", required = true)]
39+
token: String,
40+
},
3541
}
3642

3743
/// CLI arguments for the client
@@ -61,11 +67,14 @@ async fn main() -> anyhow::Result<(), Box<dyn std::error::Error + Send + Sync>>
6167

6268
let client_cli = ClientCli::parse();
6369

64-
Ok(match &client_cli.command {
70+
match &client_cli.command {
6571
ClientCommand::AddClosure { .. } => add_closure(&client_cli).await?,
6672
ClientCommand::CreateToken => create_token(&client_cli).await?,
6773
ClientCommand::AddUpstreamCache { .. } => add_upstream_cache(&client_cli).await?,
68-
})
74+
ClientCommand::ListUpstreamCaches { .. } => list_upstream_caches(&client_cli).await?,
75+
};
76+
77+
Ok(())
6978
}
7079

7180
/// Add the closure of a path in the store to the cache. The path can be of arbitrary depth, in any case
@@ -243,6 +252,23 @@ async fn add_upstream_cache(client_cli: &ClientCli) -> anyhow::Result<()> {
243252
Ok(())
244253
}
245254

255+
async fn list_upstream_caches(client_cli: &ClientCli) -> anyhow::Result<()> {
256+
let ClientCommand::ListUpstreamCaches { token } = &client_cli.command else {
257+
bail!("Upstream cache called with the wrong command");
258+
};
259+
260+
let response = get_client(&client_cli.server_address, Some(token))
261+
.await?
262+
.list_upstream_caches(tonic::Request::new(ListUpstreamCachesRequest {}))
263+
.await?;
264+
265+
for base_url in response.into_inner().base_urls {
266+
println!("{}", base_url);
267+
}
268+
269+
Ok(())
270+
}
271+
246272
/// Get a client to connect to the server at server_address.
247273
///
248274
/// An optional token can be passed to authenticate the requests.

src/cache.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ impl NARCache {
5858
debug!("Checking narinfo {} for existence", url);
5959
Ok(client.head(&url).send().await?.status().is_success())
6060
}
61+
62+
pub fn base_url(&self) -> &str {
63+
&self.base_url
64+
}
6165
}
6266

6367
pub mod persistence {

src/server/services.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,18 @@ impl management_service_server::ManagementService for ManagementServiceWrapper {
283283
success: true,
284284
}))
285285
}
286+
287+
async fn list_upstream_caches(
288+
&self,
289+
_: tonic::Request<ListUpstreamCachesRequest>,
290+
) -> anyhow::Result<tonic::Response<ListUpstreamCachesResponse>, tonic::Status> {
291+
Ok(tonic::Response::new(ListUpstreamCachesResponse {
292+
base_urls: self
293+
.upstream_caches
294+
.caches()
295+
.iter()
296+
.map(|cache| cache.base_url().to_owned())
297+
.collect(),
298+
}))
299+
}
286300
}

0 commit comments

Comments
 (0)