Skip to content

Instantly share code, notes, and snippets.

@flavio
Created October 21, 2021 07:16
Show Gist options
  • Save flavio/7f60b12791b3de111f942896756fa24c to your computer and use it in GitHub Desktop.
Save flavio/7f60b12791b3de111f942896756fa24c to your computer and use it in GitHub Desktop.
Testing parsing of urls into oci references
use oci_distribution::{ParseError, Reference};
use std::str::FromStr;
use url::Url;
fn build_oci_reference(url: Url) -> Result<Reference, ParseError> {
Reference::from_str(
url.as_ref()
.strip_prefix("registry://")
.unwrap_or_else(|| url.as_ref()),
)
}
fn main() {
let urls = vec![
"registry://busybox",
"registry://busybox:v0.1.0",
"registry://flavio/awesome-stuff:v0.1.0",
"registry://flavio/awesome-stuff",
"registry://0.0.0.0:433/kubewarden/policies/psp-capabilities",
];
for url_str in urls {
print!("Parsing {} ... ", url_str);
let url = match Url::parse(url_str) {
Ok(url) => {
println!("OK");
url
}
Err(e) => {
println!("ERR: {:?}", e);
println!();
continue;
}
};
print!("building oci reference for {} ... ", url);
match build_oci_reference(url) {
Ok(reference) => {
println!("OK");
println!(" - registry: |{:?}|", reference.registry());
println!(" - repository: |{:?}|", reference.repository());
println!(" - tag: |{:?}|", reference.tag());
println!(" - whole: |{}|", reference.whole());
}
Err(e) => {
println!("ERR {:?}", e);
}
}
println!();
}
}
@flavio
Copy link
Author

flavio commented Oct 21, 2021

This produces the following output:

Parsing registry://busybox ... OK
building oci reference for registry://busybox ... OK
  - registry: |""|
  - repository: |"busybox"|
  - tag: |None|
  - whole: |busybox|

Parsing registry://busybox:v0.1.0 ... ERR: InvalidPort

Parsing registry://flavio/awesome-stuff:v0.1.0 ... OK
building oci reference for registry://flavio/awesome-stuff:v0.1.0 ... OK
  - registry: |"flavio"|
  - repository: |"awesome-stuff"|
  - tag: |Some("v0.1.0")|
  - whole: |flavio/awesome-stuff:v0.1.0|

Parsing registry://flavio/awesome-stuff ... OK
building oci reference for registry://flavio/awesome-stuff ... OK
  - registry: |"flavio"|
  - repository: |"awesome-stuff"|
  - tag: |None|
  - whole: |flavio/awesome-stuff|

Parsing registry://0.0.0.0:433/kubewarden/policies/psp-capabilities ... OK
building oci reference for registry://0.0.0.0:433/kubewarden/policies/psp-capabilities ... OK
  - registry: |"0.0.0.0:433"|
  - repository: |"kubewarden/policies/psp-capabilities"|
  - tag: |None|
  - whole: |0.0.0.0:433/kubewarden/policies/psp-capabilities|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment