Created
October 21, 2021 07:16
-
-
Save flavio/7f60b12791b3de111f942896756fa24c to your computer and use it in GitHub Desktop.
Testing parsing of urls into oci references
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This produces the following output: