Skip to content

Commit 16f012f

Browse files
committed
Add check to disable publishing unstable apis by default
Signed-off-by: Ryan Bottriell <[email protected]>
1 parent 36c5262 commit 16f012f

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

crates/spk-cli/common/src/publish.rs

+20
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct Publisher {
2626
from: Arc<storage::RepositoryHandle>,
2727
to: Arc<storage::RepositoryHandle>,
2828
skip_source_packages: bool,
29+
allow_unstable_api: bool,
2930
force: bool,
3031
}
3132

@@ -42,6 +43,7 @@ impl Publisher {
4243
from: source,
4344
to: destination,
4445
skip_source_packages: false,
46+
allow_unstable_api: false,
4547
force: false,
4648
}
4749
}
@@ -64,6 +66,12 @@ impl Publisher {
6466
self
6567
}
6668

69+
/// Allow publishing packages defined with an unstable api version
70+
pub fn allow_unstable_api(mut self, allow_unstable_api: bool) -> Self {
71+
self.allow_unstable_api = allow_unstable_api;
72+
self
73+
}
74+
6775
/// Forcefully publishing a package will overwrite an existing publish if it exists.
6876
pub fn force(mut self, force: bool) -> Self {
6977
self.force = force;
@@ -99,6 +107,12 @@ impl Publisher {
99107
}
100108
Err(err) => return Err(err.into()),
101109
Ok(recipe) => {
110+
if !recipe.api_version().is_stable() && !self.allow_unstable_api {
111+
return Err(Error::String(format!(
112+
"Recipe has unstable api version, and allow unstable is not set: {:?}",
113+
recipe.api_version()
114+
)));
115+
}
102116
tracing::info!("publishing recipe: {}", recipe.ident().format_ident());
103117
if self.force {
104118
self.to.force_publish_recipe(&recipe).await?;
@@ -148,6 +162,12 @@ impl Publisher {
148162

149163
tracing::debug!(" loading package: {}", build.format_ident());
150164
let spec = self.from.read_package(build).await?;
165+
if !spec.api_version().is_stable() && !self.allow_unstable_api {
166+
return Err(Error::String(format!(
167+
"Package build has unstable api version, and allow unstable is not set: {:?}",
168+
spec.api_version()
169+
)));
170+
}
151171
let components = self.from.read_components(build).await?;
152172
tracing::info!("publishing package: {}", spec.ident().format_ident());
153173
let env_spec = components.values().cloned().collect();

crates/spk-cli/group2/src/cmd_publish.rs

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ pub struct Publish {
3333
#[clap(long, short)]
3434
force: bool,
3535

36+
/// Allow publishing package specs written in unstable api versions
37+
///
38+
/// The api version of a spec is the value of the `api` field within it.
39+
#[clap(long)]
40+
allow_unstable_api: bool,
41+
3642
/// The local packages to publish
3743
///
3844
/// This can be an entire package version with all builds or a
@@ -51,6 +57,7 @@ impl Run for Publish {
5157

5258
let publisher = Publisher::new(Arc::new(source.into()), Arc::new(target.into()))
5359
.skip_source_packages(self.no_source)
60+
.allow_unstable_api(self.allow_unstable_api)
5461
.force(self.force);
5562

5663
let mut published = Vec::new();

crates/spk-schema/src/spec.rs

+41-11
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ pub enum SpecRecipe {
183183
V0Package(super::v0::Spec<VersionIdent>),
184184
}
185185

186+
impl SpecRecipe {
187+
pub fn api_version(&self) -> RecipeApiVersion {
188+
match self {
189+
Self::V0Package(_) => RecipeApiVersion::V0Package,
190+
}
191+
}
192+
}
193+
186194
impl Recipe for SpecRecipe {
187195
type Output = Spec;
188196

@@ -279,8 +287,8 @@ impl FromYaml for SpecRecipe {
279287
// fairly generic, eg: 'expected struct YamlMapping'
280288
#[derive(Deserialize)]
281289
struct YamlMapping {
282-
#[serde(default = "ApiVersion::default")]
283-
api: ApiVersion,
290+
#[serde(default = "RecipeApiVersion::default")]
291+
api: RecipeApiVersion,
284292
}
285293

286294
let with_version = match serde_yaml::from_str::<YamlMapping>(&yaml) {
@@ -292,7 +300,7 @@ impl FromYaml for SpecRecipe {
292300
};
293301

294302
match with_version.api {
295-
ApiVersion::V0Package => {
303+
RecipeApiVersion::V0Package => {
296304
let inner =
297305
serde_yaml::from_str(&yaml).map_err(|err| SerdeError::new(yaml, err))?;
298306
Ok(Self::V0Package(inner))
@@ -313,6 +321,14 @@ pub enum Spec {
313321
V0Package(super::v0::Spec<BuildIdent>),
314322
}
315323

324+
impl Spec {
325+
pub fn api_version(&self) -> PackageApiVersion {
326+
match self {
327+
Self::V0Package(_) => PackageApiVersion::V0Package,
328+
}
329+
}
330+
}
331+
316332
impl Satisfy<PkgRequest> for Spec {
317333
fn check_satisfies_request(&self, request: &PkgRequest) -> Compatibility {
318334
match self {
@@ -452,8 +468,8 @@ impl FromYaml for Spec {
452468
// fairly generic, eg: 'expected struct YamlMapping'
453469
#[derive(Deserialize)]
454470
struct YamlMapping {
455-
#[serde(default = "ApiVersion::default")]
456-
api: ApiVersion,
471+
#[serde(default = "PackageApiVersion::default")]
472+
api: PackageApiVersion,
457473
}
458474

459475
let with_version = match serde_yaml::from_str::<YamlMapping>(&yaml) {
@@ -465,7 +481,7 @@ impl FromYaml for Spec {
465481
};
466482

467483
match with_version.api {
468-
ApiVersion::V0Package => {
484+
PackageApiVersion::V0Package => {
469485
let inner =
470486
serde_yaml::from_str(&yaml).map_err(|err| SerdeError::new(yaml, err))?;
471487
Ok(Self::V0Package(inner))
@@ -480,14 +496,28 @@ impl AsRef<Spec> for Spec {
480496
}
481497
}
482498

483-
#[derive(Deserialize, Serialize, Copy, Clone)]
484-
pub enum ApiVersion {
499+
#[derive(Default, Debug, Deserialize, Serialize, Copy, Clone)]
500+
pub enum RecipeApiVersion {
501+
#[default]
502+
#[serde(rename = "v0/package")]
503+
V0Package,
504+
}
505+
506+
impl RecipeApiVersion {
507+
pub fn is_stable(&self) -> bool {
508+
matches!(self, Self::V0Package)
509+
}
510+
}
511+
512+
#[derive(Default, Debug, Deserialize, Serialize, Copy, Clone)]
513+
pub enum PackageApiVersion {
514+
#[default]
485515
#[serde(rename = "v0/package")]
486516
V0Package,
487517
}
488518

489-
impl Default for ApiVersion {
490-
fn default() -> Self {
491-
Self::V0Package
519+
impl PackageApiVersion {
520+
pub fn is_stable(&self) -> bool {
521+
matches!(self, Self::V0Package)
492522
}
493523
}

0 commit comments

Comments
 (0)