shuck-run 0.0.31

Managed shell interpreter resolution for shuck
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use std::collections::BTreeMap;
use std::fs;
use std::path::{Component, Path, PathBuf};
use std::time::{Duration, SystemTime};

use anyhow::{Context, Result, anyhow, bail};
use serde::Deserialize;
use tempfile::NamedTempFile;
use url::Url;

use crate::download::fetch_url_to_path;
use crate::{AvailableShell, Environment, Shell, Version, VersionConstraint};

const REGISTRY_MAX_AGE: Duration = Duration::from_secs(24 * 60 * 60);
const REGISTRY_SCHEMA_VERSION: u64 = 2;
const ROOT_KIND: &str = "shuck.shells.index";
const SHELL_KIND: &str = "shuck.shells.versions";
const RELEASE_KIND: &str = "shuck.shells.release";

#[derive(Debug)]
pub(crate) struct RegistryIndex {
    pub(crate) shells: BTreeMap<String, RegistryShell>,
}

#[derive(Debug)]
pub(crate) struct RegistryShell {
    versions: BTreeMap<String, CachedDocumentRef>,
}

#[derive(Debug, Clone)]
struct CachedDocumentRef {
    remote_url: Url,
    cache_path: PathBuf,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct RegistryArtifact {
    pub(crate) url: String,
    pub(crate) sha256: String,
}

#[derive(Debug, Deserialize)]
struct RootDocument {
    version: u64,
    kind: String,
    shells: BTreeMap<String, RootShellDocument>,
}

#[derive(Debug, Deserialize)]
struct RootShellDocument {
    versions_url: String,
}

#[derive(Debug, Deserialize)]
struct ShellDocument {
    version: u64,
    kind: String,
    shell: String,
    versions: BTreeMap<String, ShellVersionDocument>,
}

#[derive(Debug, Deserialize)]
struct ShellVersionDocument {
    manifest_url: String,
}

#[derive(Debug, Deserialize)]
struct ReleaseDocument {
    version: u64,
    kind: String,
    shell: String,
    release: String,
    platforms: BTreeMap<String, RegistryArtifact>,
}

impl CachedDocumentRef {
    fn root(environment: &Environment) -> Result<Self> {
        let mut remote_url = Url::parse(&environment.registry_url)
            .with_context(|| format!("parse registry URL `{}`", environment.registry_url))?;
        if remote_url.path().ends_with('/') {
            remote_url = remote_url
                .join("index.json")
                .context("resolve registry root index URL")?;
        }

        Ok(Self {
            remote_url,
            cache_path: environment.shells_root.join("index.json"),
        })
    }

    fn resolve_relative(&self, reference: &str) -> Result<Self> {
        let remote_url = self
            .remote_url
            .join(reference)
            .with_context(|| format!("resolve registry reference `{reference}`"))?;
        let cache_parent = self
            .cache_path
            .parent()
            .ok_or_else(|| anyhow!("invalid cache path {}", self.cache_path.display()))?;
        let cache_path = resolve_cache_path(cache_parent, reference)?;
        Ok(Self {
            remote_url,
            cache_path,
        })
    }
}

pub(crate) fn available_shells(
    registry: &RegistryIndex,
    shell: Option<Shell>,
) -> Vec<AvailableShell> {
    let shells = shell.into_iter().collect::<Vec<_>>();
    let names = if shells.is_empty() {
        registry
            .shells
            .keys()
            .filter_map(|name| Shell::from_name(name))
            .collect::<Vec<_>>()
    } else {
        shells
    };

    let mut available = names
        .into_iter()
        .filter_map(|shell| {
            registry.shells.get(shell.as_str()).map(|entry| {
                let mut versions = entry
                    .versions
                    .keys()
                    .filter_map(|version| Version::parse(version).ok())
                    .collect::<Vec<_>>();
                versions.sort();
                versions.reverse();
                AvailableShell { shell, versions }
            })
        })
        .collect::<Vec<_>>();
    available.sort_by_key(|entry| entry.shell);
    available
}

pub(crate) fn select_release_for_platform(
    registry: &RegistryIndex,
    shell: Shell,
    constraint: &VersionConstraint,
    platform: &str,
    refresh: bool,
    verbose: bool,
) -> Result<(Version, RegistryArtifact)> {
    let shell_entry = shell_entry(registry, shell)?;
    let mut versions = parsed_versions(shell_entry)?;
    versions.sort();

    let mut matched_constraint = false;
    for version in versions.into_iter().rev() {
        if !constraint.matches(&version) {
            continue;
        }
        matched_constraint = true;
        let manifest_ref = shell_entry.versions.get(version.as_str()).ok_or_else(|| {
            anyhow!(
                "missing release manifest reference for {shell} {}",
                version.as_str()
            )
        })?;
        let release = load_release_document(
            manifest_ref,
            shell.as_str(),
            version.as_str(),
            refresh,
            verbose,
        )?;
        if let Some(artifact) = release.platforms.get(platform) {
            return Ok((version, artifact.clone()));
        }
    }

    if matched_constraint {
        Err(anyhow!(
            "{shell} {} does not have a prebuilt binary for {platform}.",
            constraint.describe()
        ))
    } else {
        Err(version_unavailable_error(shell, constraint))
    }
}

pub(crate) fn load_registry(
    environment: &Environment,
    refresh: bool,
    verbose: bool,
) -> Result<RegistryIndex> {
    fs::create_dir_all(&environment.shells_root)
        .with_context(|| format!("create {}", environment.shells_root.display()))?;

    let root_ref = CachedDocumentRef::root(environment)?;
    let root = load_document(&root_ref, refresh, verbose, parse_root_document)?;

    let mut shells = BTreeMap::new();
    for (shell, entry) in root.shells {
        let shell_ref = root_ref.resolve_relative(&entry.versions_url)?;
        let shell_document = load_document(&shell_ref, refresh, verbose, |source| {
            parse_shell_document(source, &shell)
        })?;
        let versions = shell_document
            .versions
            .into_iter()
            .map(|(version, version_entry)| {
                let manifest_ref = shell_ref.resolve_relative(&version_entry.manifest_url)?;
                Ok((version, manifest_ref))
            })
            .collect::<Result<BTreeMap<_, _>>>()?;
        shells.insert(shell, RegistryShell { versions });
    }

    Ok(RegistryIndex { shells })
}

fn resolve_cache_path(base: &Path, reference: &str) -> Result<PathBuf> {
    let mut resolved = base.to_path_buf();
    for component in Path::new(reference).components() {
        match component {
            Component::CurDir => {}
            Component::Normal(segment) => resolved.push(segment),
            Component::ParentDir => {
                bail!("registry reference `{reference}` escapes the local cache root")
            }
            Component::RootDir | Component::Prefix(_) => {
                bail!("registry reference `{reference}` must be relative")
            }
        }
    }
    Ok(resolved)
}

fn load_release_document(
    manifest_ref: &CachedDocumentRef,
    expected_shell: &str,
    expected_version: &str,
    refresh: bool,
    verbose: bool,
) -> Result<ReleaseDocument> {
    load_document(manifest_ref, refresh, verbose, |source| {
        parse_release_document(source, expected_shell, expected_version)
    })
}

fn load_document<T, F>(
    document_ref: &CachedDocumentRef,
    refresh: bool,
    verbose: bool,
    parse: F,
) -> Result<T>
where
    F: Fn(&str) -> Result<T>,
{
    let should_refresh = refresh
        || !document_ref.cache_path.exists()
        || index_is_stale(&document_ref.cache_path).unwrap_or(true);
    if should_refresh {
        match refresh_document(document_ref, verbose, &parse) {
            Ok(document) => return Ok(document),
            Err(_err) if document_ref.cache_path.exists() && !refresh => {}
            Err(err) => return Err(err),
        }
    }

    match read_document(&document_ref.cache_path, &parse) {
        Ok(document) => Ok(document),
        Err(_err) if !refresh => refresh_document(document_ref, verbose, &parse),
        Err(err) => Err(err),
    }
}

fn refresh_document<T, F>(document_ref: &CachedDocumentRef, verbose: bool, parse: &F) -> Result<T>
where
    F: Fn(&str) -> Result<T>,
{
    let parent = document_ref
        .cache_path
        .parent()
        .ok_or_else(|| anyhow!("invalid cache path {}", document_ref.cache_path.display()))?;
    fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;

    let temp_file = NamedTempFile::new_in(parent)
        .with_context(|| format!("create temp registry document in {}", parent.display()))?;
    fetch_url_to_path(document_ref.remote_url.as_str(), temp_file.path(), verbose)
        .with_context(|| format!("fetch {}", document_ref.remote_url))?;
    let document = read_document(temp_file.path(), parse)?;
    match temp_file.persist(&document_ref.cache_path) {
        Ok(_) => Ok(document),
        Err(err) => {
            Err(err.error).with_context(|| format!("replace {}", document_ref.cache_path.display()))
        }
    }
}

fn read_document<T, F>(path: &Path, parse: &F) -> Result<T>
where
    F: Fn(&str) -> Result<T>,
{
    let source = fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
    parse(&source).with_context(|| format!("parse {}", path.display()))
}

fn parse_root_document(source: &str) -> Result<RootDocument> {
    let document: RootDocument =
        serde_json::from_str(source).context("decode root registry document")?;
    if document.version != REGISTRY_SCHEMA_VERSION {
        bail!(
            "root registry document version {} is unsupported",
            document.version
        );
    }
    if document.kind != ROOT_KIND {
        bail!(
            "root registry document kind `{}` is unsupported",
            document.kind
        );
    }
    Ok(document)
}

fn parse_shell_document(source: &str, expected_shell: &str) -> Result<ShellDocument> {
    let document: ShellDocument =
        serde_json::from_str(source).context("decode shell registry document")?;
    if document.version != REGISTRY_SCHEMA_VERSION {
        bail!(
            "shell registry document for `{expected_shell}` has unsupported version {}",
            document.version
        );
    }
    if document.kind != SHELL_KIND {
        bail!(
            "shell registry document for `{expected_shell}` has unsupported kind `{}`",
            document.kind
        );
    }
    if document.shell != expected_shell {
        bail!(
            "shell registry document expected `{expected_shell}`, found `{}`",
            document.shell
        );
    }
    Ok(document)
}

fn parse_release_document(
    source: &str,
    expected_shell: &str,
    expected_version: &str,
) -> Result<ReleaseDocument> {
    let document: ReleaseDocument =
        serde_json::from_str(source).context("decode release manifest")?;
    if document.version != REGISTRY_SCHEMA_VERSION {
        bail!(
            "release manifest for `{expected_shell}` `{expected_version}` has unsupported version {}",
            document.version
        );
    }
    if document.kind != RELEASE_KIND {
        bail!(
            "release manifest for `{expected_shell}` `{expected_version}` has unsupported kind `{}`",
            document.kind
        );
    }
    if document.shell != expected_shell {
        bail!(
            "release manifest expected shell `{expected_shell}`, found `{}`",
            document.shell
        );
    }
    if document.release != expected_version {
        bail!(
            "release manifest expected version `{expected_version}`, found `{}`",
            document.release
        );
    }
    Ok(document)
}

fn index_is_stale(path: &Path) -> Result<bool> {
    let modified = fs::metadata(path)
        .with_context(|| format!("stat {}", path.display()))?
        .modified()
        .with_context(|| format!("read mtime for {}", path.display()))?;
    let age = SystemTime::now()
        .duration_since(modified)
        .unwrap_or_default();
    Ok(age > REGISTRY_MAX_AGE)
}

fn shell_entry(registry: &RegistryIndex, shell: Shell) -> Result<&RegistryShell> {
    registry.shells.get(shell.as_str()).ok_or_else(|| {
        anyhow!(
            "{} is not available. Run `shuck install --list` to see available shells.",
            shell
        )
    })
}

fn parsed_versions(shell_entry: &RegistryShell) -> Result<Vec<Version>> {
    shell_entry
        .versions
        .keys()
        .map(|version| Version::parse(version))
        .collect()
}

fn version_unavailable_error(shell: Shell, constraint: &VersionConstraint) -> anyhow::Error {
    anyhow!(
        "{shell} {} is not available. Run `shuck install --list {shell}` to see available versions.",
        constraint.describe()
    )
}