Skip to content

Commit

Permalink
deploy: 27066c6
Browse files Browse the repository at this point in the history
  • Loading branch information
theduke committed Jun 12, 2024
1 parent 6ef45db commit 2281a52
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 60 deletions.
2 changes: 1 addition & 1 deletion crates/doc/search-index.js

Large diffs are not rendered by default.

150 changes: 106 additions & 44 deletions crates/doc/src/wasmer_cli/commands/package/download.rs.html
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,56 @@
<a href="#280" id="280">280</a>
<a href="#281" id="281">281</a>
<a href="#282" id="282">282</a>
</pre></div><pre class="rust"><code><span class="kw">use </span>std::path::PathBuf;
<a href="#283" id="283">283</a>
<a href="#284" id="284">284</a>
<a href="#285" id="285">285</a>
<a href="#286" id="286">286</a>
<a href="#287" id="287">287</a>
<a href="#288" id="288">288</a>
<a href="#289" id="289">289</a>
<a href="#290" id="290">290</a>
<a href="#291" id="291">291</a>
<a href="#292" id="292">292</a>
<a href="#293" id="293">293</a>
<a href="#294" id="294">294</a>
<a href="#295" id="295">295</a>
<a href="#296" id="296">296</a>
<a href="#297" id="297">297</a>
<a href="#298" id="298">298</a>
<a href="#299" id="299">299</a>
<a href="#300" id="300">300</a>
<a href="#301" id="301">301</a>
<a href="#302" id="302">302</a>
<a href="#303" id="303">303</a>
<a href="#304" id="304">304</a>
<a href="#305" id="305">305</a>
<a href="#306" id="306">306</a>
<a href="#307" id="307">307</a>
<a href="#308" id="308">308</a>
<a href="#309" id="309">309</a>
<a href="#310" id="310">310</a>
<a href="#311" id="311">311</a>
<a href="#312" id="312">312</a>
<a href="#313" id="313">313</a>
</pre></div><pre class="rust"><code><span class="kw">use </span>std::{env::current_dir, path::PathBuf};

<span class="kw">use </span>anyhow::{bail, Context};
<span class="kw">use </span>dialoguer::console::{style, Emoji};
<span class="kw">use </span>indicatif::{ProgressBar, ProgressStyle};
<span class="kw">use </span>tempfile::NamedTempFile;
<span class="kw">use </span>wasmer_config::package::{PackageIdent, PackageSource};
<span class="kw">use </span>wasmer_registry::wasmer_env::WasmerEnv;
<span class="kw">use </span>wasmer_wasix::http::reqwest::get_proxy;

<span class="kw">use </span><span class="kw">crate</span>::opts::{ApiOpts, WasmerEnv};

<span class="doccomment">/// Download a package from the registry.
</span><span class="attr">#[derive(clap::Parser, Debug)]
</span><span class="kw">pub struct </span>PackageDownload {
<span class="attr">#[clap(flatten)]
</span>env: WasmerEnv,
</span><span class="kw">pub </span>api: ApiOpts,

<span class="attr">#[clap(flatten)]
</span><span class="kw">pub </span>env: WasmerEnv,

<span class="doccomment">/// Verify that the downloaded file is a valid package.
</span><span class="attr">#[clap(long)]
Expand All @@ -303,7 +338,7 @@
<span class="doccomment">/// Path where the package file should be written to.
/// If not specified, the data will be written to stdout.
</span><span class="attr">#[clap(short = <span class="string">&#39;o&#39;</span>, long)]
</span>out_path: PathBuf,
</span>out_path: <span class="prelude-ty">Option</span>&lt;PathBuf&gt;,

<span class="doccomment">/// Run the download command without any output
</span><span class="attr">#[clap(long)]
Expand Down Expand Up @@ -345,7 +380,7 @@

step_num += <span class="number">1</span>;

<span class="kw">if let </span><span class="prelude-val">Some</span>(parent) = <span class="self">self</span>.out_path.parent() {
<span class="kw">if let </span><span class="prelude-val">Some</span>(parent) = <span class="self">self</span>.out_path.as_ref().and_then(|p| p.parent()) {
<span class="kw">match </span>parent.metadata() {
<span class="prelude-val">Ok</span>(m) =&gt; {
<span class="kw">if </span>!m.is_dir() {
Expand Down Expand Up @@ -373,53 +408,70 @@

step_num += <span class="number">1</span>;

<span class="kw">let </span>(download_url, token, ident) = <span class="kw">match </span><span class="kw-2">&amp;</span><span class="self">self</span>.package {
<span class="kw">let </span>(download_url, ident, filename) = <span class="kw">match </span><span class="kw-2">&amp;</span><span class="self">self</span>.package {
PackageSource::Ident(PackageIdent::Named(id)) =&gt; {
<span class="kw">let </span>endpoint = <span class="self">self</span>.env.registry_endpoint()<span class="question-mark">?</span>;
<span class="kw">let </span>client = <span class="kw">if </span><span class="self">self</span>.api.token.is_some() {
<span class="self">self</span>.api.client()
} <span class="kw">else </span>{
<span class="self">self</span>.api.client_unauthennticated()
}<span class="question-mark">?</span>;

<span class="kw">let </span>version = id.version_or_default().to_string();
<span class="kw">let </span>version = <span class="kw">if </span>version == <span class="string">&quot;*&quot; </span>{ <span class="prelude-val">None </span>} <span class="kw">else </span>{ <span class="prelude-val">Some</span>(version) };
<span class="kw">let </span>version = <span class="kw">if </span>version == <span class="string">&quot;*&quot; </span>{
String::from(<span class="string">&quot;latest&quot;</span>)
} <span class="kw">else </span>{
version.to_string()
};
<span class="kw">let </span>full_name = id.full_name();
<span class="kw">let </span>token = <span class="self">self</span>.env.get_token_opt().map(|x| x.to_string());

<span class="kw">let </span>package = wasmer_registry::query_package_from_registry(
endpoint.as_str(),
<span class="kw-2">&amp;</span>full_name,
version.as_deref(),
token.as_deref(),
)
.with_context(|| {
<span class="macro">format!</span>(
<span class="kw">let </span>rt = tokio::runtime::Runtime::new()<span class="question-mark">?</span>;
<span class="kw">let </span>package = rt
.block_on(wasmer_api::query::get_package_version(
<span class="kw-2">&amp;</span>client,
full_name.clone(),
version.clone(),
))<span class="question-mark">?
</span>.with_context(|| {
<span class="macro">format!</span>(
<span class="string">&quot;could not retrieve package information for package &#39;{}&#39; from registry &#39;{}&#39;&quot;</span>,
full_name, endpoint,
full_name, client.graphql_endpoint(),
)
})<span class="question-mark">?</span>;
})<span class="question-mark">?</span>;

<span class="kw">let </span>download_url = package
.pirita_url
.context(<span class="string">&quot;registry does provide a container download container download URL&quot;</span>)<span class="question-mark">?</span>;
.distribution_v3
.pirita_download_url
.context(<span class="string">&quot;registry did not provide a container download URL&quot;</span>)<span class="question-mark">?</span>;

<span class="kw">let </span>ident = <span class="macro">format!</span>(<span class="string">&quot;{}@{}&quot;</span>, package.package, package.version);
<span class="kw">let </span>ident = <span class="macro">format!</span>(<span class="string">&quot;{}@{}&quot;</span>, full_name, package.version);
<span class="kw">let </span>filename = <span class="kw">if let </span><span class="prelude-val">Some</span>(ns) = <span class="kw-2">&amp;</span>package.package.namespace {
<span class="macro">format!</span>(
<span class="string">&quot;{}--{}@{}.webc&quot;</span>,
ns.clone(),
package.package.package_name,
package.version
)
} <span class="kw">else </span>{
<span class="macro">format!</span>(<span class="string">&quot;{}@{}.webc&quot;</span>, package.package.package_name, package.version)
};

(download_url, token, ident)
(download_url, ident, filename)
}
PackageSource::Ident(PackageIdent::Hash(hash)) =&gt; {
<span class="kw">let </span>endpoint = <span class="self">self</span>.env.registry_endpoint()<span class="question-mark">?</span>;
<span class="kw">let </span>token = <span class="self">self</span>.env.get_token_opt().map(|x| x.to_string());

<span class="kw">let </span>client = wasmer_api::WasmerClient::new(endpoint, <span class="string">&quot;wasmer-cli&quot;</span>)<span class="question-mark">?</span>;
<span class="kw">let </span>client = <span class="kw">if let </span><span class="prelude-val">Some</span>(token) = <span class="kw-2">&amp;</span>token {
client.with_auth_token(token.clone())
<span class="kw">let </span>client = <span class="kw">if </span><span class="self">self</span>.api.token.is_some() {
<span class="self">self</span>.api.client()
} <span class="kw">else </span>{
client
};
<span class="self">self</span>.api.client_unauthennticated()
}<span class="question-mark">?</span>;

<span class="kw">let </span>rt = tokio::runtime::Runtime::new()<span class="question-mark">?</span>;
<span class="kw">let </span>pkg = rt.block_on(wasmer_api::query::get_package_release(<span class="kw-2">&amp;</span>client, <span class="kw-2">&amp;</span>hash.to_string()))<span class="question-mark">?
</span>.with_context(|| <span class="macro">format!</span>(<span class="string">&quot;Package with {hash} does not exist in the registry, or is not accessible&quot;</span>))<span class="question-mark">?</span>;

<span class="kw">let </span>ident = hash.to_string();
<span class="kw">let </span>filename = <span class="macro">format!</span>(<span class="string">&quot;{}.webc&quot;</span>, hash);

(pkg.webc_url, token, ident)
(pkg.webc_url, ident, filename)
}
PackageSource::Path(p) =&gt; <span class="macro">bail!</span>(<span class="string">&quot;cannot download a package from a local path: &#39;{p}&#39;&quot;</span>),
PackageSource::Url(url) =&gt; <span class="macro">bail!</span>(<span class="string">&quot;cannot download a package from a URL: &#39;{}&#39;&quot;</span>, url),
Expand All @@ -434,12 +486,9 @@
};
<span class="kw">let </span>client = builder.build().context(<span class="string">&quot;failed to create reqwest client&quot;</span>)<span class="question-mark">?</span>;

<span class="kw">let </span><span class="kw-2">mut </span>b = client
<span class="kw">let </span>b = client
.get(download_url)
.header(http::header::ACCEPT, <span class="string">&quot;application/webc&quot;</span>);
<span class="kw">if let </span><span class="prelude-val">Some</span>(token) = token {
b = b.header(http::header::AUTHORIZATION, <span class="macro">format!</span>(<span class="string">&quot;Bearer {token}&quot;</span>));
};

pb.println(<span class="macro">format!</span>(
<span class="string">&quot;{} {}Downloading package {} ...&quot;</span>,
Expand Down Expand Up @@ -472,7 +521,11 @@
<span class="comment">// Set the length of the progress bar
</span>pb.set_length(webc_total_size);

<span class="kw">let </span><span class="kw-2">mut </span>tmpfile = NamedTempFile::new_in(<span class="self">self</span>.out_path.parent().unwrap())<span class="question-mark">?</span>;
<span class="kw">let </span><span class="kw-2">mut </span>tmpfile = <span class="kw">if let </span><span class="prelude-val">Some</span>(parent) = <span class="self">self</span>.out_path.as_ref().and_then(|p| p.parent()) {
NamedTempFile::new_in(parent)<span class="question-mark">?
</span>} <span class="kw">else </span>{
NamedTempFile::new()<span class="question-mark">?
</span>};
<span class="kw">let </span>accepted_contenttypes = <span class="macro">vec!</span>[
<span class="string">&quot;application/webc&quot;</span>,
<span class="string">&quot;application/octet-stream&quot;</span>,
Expand Down Expand Up @@ -513,10 +566,16 @@
.context(<span class="string">&quot;could not parse downloaded file as a package - invalid download?&quot;</span>)<span class="question-mark">?</span>;
}

tmpfile.persist(<span class="kw-2">&amp;</span><span class="self">self</span>.out_path).with_context(|| {
<span class="kw">let </span>out_path = <span class="kw">if let </span><span class="prelude-val">Some</span>(out_path) = <span class="kw-2">&amp;</span><span class="self">self</span>.out_path {
out_path.clone()
} <span class="kw">else </span>{
current_dir()<span class="question-mark">?</span>.join(filename)
};

tmpfile.persist(<span class="kw-2">&amp;</span>out_path).with_context(|| {
<span class="macro">format!</span>(
<span class="string">&quot;could not persist temporary file to &#39;{}&#39;&quot;</span>,
<span class="self">self</span>.out_path.display()
out_path.display()
)
})<span class="question-mark">?</span>;

Expand All @@ -526,7 +585,7 @@
.bold()
.dim(),
WRITING_PACKAGE_EMOJI,
<span class="self">self</span>.out_path.display()
out_path.display()
));

<span class="comment">// We&#39;re done, so finish the progress bar
Expand All @@ -538,9 +597,8 @@

<span class="attr">#[cfg(test)]
</span><span class="kw">mod </span>tests {
<span class="kw">use </span>wasmer_registry::wasmer_env::WASMER_DIR;

<span class="kw">use super</span>::<span class="kw-2">*</span>;
<span class="kw">use </span>std::str::FromStr;

<span class="doccomment">/// Download a package from the dev registry.
</span><span class="attr">#[test]
Expand All @@ -550,9 +608,13 @@
<span class="kw">let </span>out_path = dir.path().join(<span class="string">&quot;hello.webc&quot;</span>);

<span class="kw">let </span>cmd = PackageDownload {
env: WasmerEnv::new(WASMER_DIR.clone(), <span class="prelude-val">Some</span>(<span class="string">&quot;wasmer.wtf&quot;</span>.into()), <span class="prelude-val">None</span>, <span class="prelude-val">None</span>),
env: WasmerEnv::default(),
api: ApiOpts {
token: <span class="prelude-val">None</span>,
registry: <span class="prelude-val">Some</span>(url::Url::from_str(<span class="string">&quot;https://registry.wasmer.io/graphql&quot;</span>).unwrap()),
},
validate: <span class="bool-val">true</span>,
out_path: out_path.clone(),
out_path: <span class="prelude-val">Some</span>(out_path.clone()),
package: <span class="string">&quot;wasmer/hello@0.1.0&quot;</span>.parse().unwrap(),
quiet: <span class="bool-val">true</span>,
};
Expand Down
Loading

0 comments on commit 2281a52

Please sign in to comment.