|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Shared image-name resolution for community sandbox images. |
| 5 | +//! |
| 6 | +//! Both the CLI and TUI need to expand bare sandbox names (e.g. `"base"`) into |
| 7 | +//! fully-qualified container image references. This module centralises that |
| 8 | +//! logic so every client resolves names identically. |
| 9 | +
|
| 10 | +/// Default registry prefix for community sandbox images. |
| 11 | +/// |
| 12 | +/// Bare sandbox names are expanded to `{prefix}/{name}:latest`. |
| 13 | +/// Override at runtime with the `OPENSHELL_COMMUNITY_REGISTRY` env var. |
| 14 | +pub const DEFAULT_COMMUNITY_REGISTRY: &str = "ghcr.io/nvidia/openshell-community/sandboxes"; |
| 15 | + |
| 16 | +/// Resolve a user-supplied image string into a fully-qualified reference. |
| 17 | +/// |
| 18 | +/// Resolution rules (applied in order): |
| 19 | +/// 1. If the value contains `/`, `:`, or `.` it is treated as a complete image |
| 20 | +/// reference and returned as-is. |
| 21 | +/// 2. Otherwise it is treated as a community sandbox name and expanded to |
| 22 | +/// `{registry}/{value}:latest` where `{registry}` defaults to |
| 23 | +/// [`DEFAULT_COMMUNITY_REGISTRY`] but can be overridden via the |
| 24 | +/// `OPENSHELL_COMMUNITY_REGISTRY` environment variable. |
| 25 | +/// |
| 26 | +/// This function only handles image-name resolution. Dockerfile detection is |
| 27 | +/// the responsibility of the caller (e.g. the CLI's `resolve_from()`). |
| 28 | +pub fn resolve_community_image(value: &str) -> String { |
| 29 | + // Already a fully-qualified reference. |
| 30 | + if value.contains('/') || value.contains(':') || value.contains('.') { |
| 31 | + return value.to_string(); |
| 32 | + } |
| 33 | + |
| 34 | + // Community sandbox shorthand → expand with registry prefix. |
| 35 | + let prefix = std::env::var("OPENSHELL_COMMUNITY_REGISTRY") |
| 36 | + .unwrap_or_else(|_| DEFAULT_COMMUNITY_REGISTRY.to_string()); |
| 37 | + let prefix = prefix.trim_end_matches('/'); |
| 38 | + format!("{prefix}/{value}:latest") |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(test)] |
| 42 | +#[allow(unsafe_code)] |
| 43 | +mod tests { |
| 44 | + use super::*; |
| 45 | + |
| 46 | + #[test] |
| 47 | + fn bare_name_expands_to_community_registry() { |
| 48 | + let result = resolve_community_image("base"); |
| 49 | + assert_eq!( |
| 50 | + result, |
| 51 | + "ghcr.io/nvidia/openshell-community/sandboxes/base:latest" |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn bare_name_with_env_override() { |
| 57 | + // Use a temp env override. Safety: test-only, and these env-var tests |
| 58 | + // are not run concurrently with other tests reading the same var. |
| 59 | + let key = "OPENSHELL_COMMUNITY_REGISTRY"; |
| 60 | + let prev = std::env::var(key).ok(); |
| 61 | + // SAFETY: single-threaded test context; no other thread reads this var. |
| 62 | + unsafe { std::env::set_var(key, "my-registry.example.com/sandboxes") }; |
| 63 | + let result = resolve_community_image("python"); |
| 64 | + assert_eq!(result, "my-registry.example.com/sandboxes/python:latest"); |
| 65 | + // Restore. |
| 66 | + match prev { |
| 67 | + Some(v) => unsafe { std::env::set_var(key, v) }, |
| 68 | + None => unsafe { std::env::remove_var(key) }, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn full_reference_with_slash_passes_through() { |
| 74 | + let input = "ghcr.io/myorg/myimage:v1"; |
| 75 | + assert_eq!(resolve_community_image(input), input); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn reference_with_colon_passes_through() { |
| 80 | + let input = "myimage:latest"; |
| 81 | + assert_eq!(resolve_community_image(input), input); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn reference_with_dot_passes_through() { |
| 86 | + let input = "registry.example.com"; |
| 87 | + assert_eq!(resolve_community_image(input), input); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn trailing_slash_in_env_is_trimmed() { |
| 92 | + let key = "OPENSHELL_COMMUNITY_REGISTRY"; |
| 93 | + let prev = std::env::var(key).ok(); |
| 94 | + // SAFETY: single-threaded test context; no other thread reads this var. |
| 95 | + unsafe { std::env::set_var(key, "my-registry.example.com/sandboxes/") }; |
| 96 | + let result = resolve_community_image("base"); |
| 97 | + assert_eq!(result, "my-registry.example.com/sandboxes/base:latest"); |
| 98 | + match prev { |
| 99 | + Some(v) => unsafe { std::env::set_var(key, v) }, |
| 100 | + None => unsafe { std::env::remove_var(key) }, |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments