nsh-cli 0.6.0

The nsh command line tool: build, package and run apps.
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
417
418
419
420
421
422
423
//! `nsh update`: replaces the installed distribution (`~/.nsh`,
//! `%LOCALAPPDATA%\nsh`, wherever the install script put it) with the latest
//! release or nightly build on GitHub Releases, the same archives the install
//! scripts download, checked against their `.sha256`.
//!
//! The channel a build is on is stamped at build time (`NSH_CHANNEL`, set by
//! the release workflow's nightly run), so `nsh update` stays on it;
//! naming a channel switches (`nsh update nightly`, as `rustup update
//! nightly`). The latest release is read from the redirect of
//! `releases/latest` and the nightly's revision from its `nsh-release.json`,
//! neither of them an API call, so no rate limit applies. The directory is
//! swapped by `nsh_updater`'s installation, which also handles Windows'
//! running executable; the components installed beside it
//! (`crate::component`) are added again by the new nsh.

use std::{
    fs,
    io::{Read as _, Write as _},
    path::{Path, PathBuf},
};

use anyhow::{Context as _, bail};
use nsh_updater::install::Installation;
use serde::Deserialize;
use sha2::{Digest as _, Sha256};

use crate::{runtime, ui};

/// Where the releases are published.
const REPOSITORY: &str = "longbridge/nsh";
/// The nightly release's tag, moved to each build.
const NIGHTLY: &str = "nightly";
/// The nightly's description of itself, an asset of the release.
const RELEASE_INFO: &str = "nsh-release.json";

/// A release channel.
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub(crate) enum Channel {
    /// The releases (`vX.Y.Z`).
    Stable,
    /// The latest `main`, built every night it changes.
    Nightly,
}

impl Channel {
    /// The channel this build was published on.
    fn current() -> Channel {
        if env!("NSH_CHANNEL") == "nightly" {
            Channel::Nightly
        } else {
            Channel::Stable
        }
    }

    fn name(self) -> &'static str {
        match self {
            Channel::Stable => "stable",
            Channel::Nightly => "nightly",
        }
    }
}

/// `nsh-release.json`.
#[derive(Debug, Deserialize)]
struct ReleaseInfo {
    /// The workspace version it was built at.
    version: String,
    /// The full revision of `main` it was built from.
    commit: String,
    /// The UTC day it was built, `YYYY-MM-DD`; older descriptions lack it.
    #[serde(default)]
    date: Option<String>,
}

/// What the channel offers.
struct Offer {
    /// The release's tag, for its download URLs.
    tag: String,
    /// How it is shown: `0.5.0`, `0.4.0 (abcdef0 nightly 2026-11-10)`.
    label: String,
    /// Whether it differs from this build in the way that matters: a newer
    /// version on the stable channel, another revision on the nightly one,
    /// anything after a switch.
    newer: bool,
}

/// `nsh update [stable|nightly]`.
pub(crate) fn update(channel: Option<Channel>) -> anyhow::Result<()> {
    let current = Channel::current();
    let channel = channel.unwrap_or(current);
    let exe = runtime::cli_exe()?;
    let root = distribution(&exe)?;
    let installation = Installation::directory(&root, &exe)
        .context("the nsh executable is not inside its distribution")?;
    installation.remove_leftovers();
    if !installation.can_install() {
        bail!(
            "cannot write next to {}; update it as the user who installed it",
            ui::path(&root)
        );
    }
    let asset = asset()?;
    let agent = agent();
    // Replacing the directory drops the components: the new nsh adds them
    // again, from its own release.
    let components = crate::component::installed(&root);

    let step = ui::Step::start(format!("checking the {} channel", channel.name()));
    let offer = match channel {
        Channel::Stable => stable(&agent, current)?,
        Channel::Nightly => nightly(&agent, current)?,
    };
    step.finish();
    if !offer.newer {
        ui::done(format!(
            "nsh {} is the latest on the {} channel",
            env!("NSH_VERSION_TEXT"),
            channel.name()
        ));
        // The CLI is current, but a runtime may be left from an earlier
        // release (an update that could not add it again): bring it along.
        let stale = crate::component::stale(&root);
        if !stale.is_empty() {
            let names: Vec<String> = stale.iter().map(|n| (*n).to_owned()).collect();
            crate::component::add_named(&names)?;
        }
        return Ok(());
    }

    let url = asset_url(&offer.tag, &asset);
    let dir = crate::check::cache()?.join("updates");
    fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
    let archive = dir.join(format!("{}-{asset}", std::process::id()));
    let result = (|| {
        let step = ui::Step::start(format!("downloading nsh {}", offer.label));
        download(&agent, &url, &archive, &step)?;
        step.finish();
        let step = ui::Step::start(format!("installing into {}", ui::path(&root)));
        installation
            .install(&archive)
            .with_context(|| format!("replace {}", root.display()))?;
        step.finish();
        anyhow::Ok(())
    })();
    let _ = fs::remove_file(&archive);
    result?;

    let installed = std::process::Command::new(&exe)
        .arg("--version")
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_owned())
        .unwrap_or_else(|| format!("nsh {}", offer.label));
    ui::done(format!("updated to {installed}"));
    if !components.is_empty() {
        let status = std::process::Command::new(&exe)
            .args(["component", "add"])
            .args(&components)
            .status()
            .context("add the components again")?;
        if !status.success() {
            bail!(
                "could not add the components again: run `nsh component add {}`",
                components.join(" ")
            );
        }
    }
    Ok(())
}

/// The distribution's directory holding `exe` (`<root>/bin/nsh`), or why
/// `nsh update` leaves this nsh to whatever installed it.
fn distribution(exe: &Path) -> anyhow::Result<PathBuf> {
    let text = exe.to_string_lossy().replace('\\', "/");
    let managers = [
        (
            "/Cellar/",
            "Homebrew installed this nsh: run `brew upgrade nsh`",
        ),
        (
            "/scoop/apps/",
            "Scoop installed this nsh: run `scoop update nsh`",
        ),
        (
            "/mise/installs/",
            "mise installed this nsh: run `mise upgrade`",
        ),
    ];
    if let Some((_, hint)) = managers.iter().find(|(part, _)| text.contains(part)) {
        bail!("{hint}");
    }
    let root = exe
        .parent()
        .filter(|bin| bin.file_name().is_some_and(|name| name == "bin"))
        .and_then(Path::parent)
        .filter(|root| root.join("runtime").is_dir());
    match root {
        Some(root) => Ok(root.to_owned()),
        None => bail!(
            "{} is not an installed distribution (built from source?); update the checkout \
             with Git, or install a release: https://nsh.dev/docs/installation",
            ui::path(exe)
        ),
    }
}

/// The archive of this machine's platform.
fn asset() -> anyhow::Result<String> {
    let host = host()?;
    Ok(if cfg!(windows) {
        format!("nsh-{host}.zip")
    } else {
        format!("nsh-{host}.tar.gz")
    })
}

/// This machine's platform as release assets name it (`macos-arm64`).
pub(crate) fn host() -> anyhow::Result<&'static str> {
    Ok(match (std::env::consts::OS, std::env::consts::ARCH) {
        ("macos", "aarch64") => "macos-arm64",
        ("macos", "x86_64") => "macos-x86_64",
        ("linux", "x86_64") => "linux-x86_64",
        ("linux", "aarch64") => "linux-arm64",
        ("windows", "x86_64") => "windows-x86_64",
        ("windows", "aarch64") => "windows-arm64",
        (os, arch) => bail!("no prebuilt nsh for {os} {arch}; build it from source"),
    })
}

/// The release this build was published as: its tag (`v0.4.0`, `nightly`),
/// where its components are.
pub(crate) fn own_tag() -> String {
    match Channel::current() {
        Channel::Nightly => NIGHTLY.to_owned(),
        Channel::Stable => format!("v{}", env!("CARGO_PKG_VERSION")),
    }
}

/// The URL of `asset` of the release `tag`.
pub(crate) fn asset_url(tag: &str, asset: &str) -> String {
    format!("https://github.com/{REPOSITORY}/releases/download/{tag}/{asset}")
}

/// Errors as statuses, redirects not followed: `releases/latest` names the
/// latest release in its `Location`.
pub(crate) fn agent() -> ureq::Agent {
    ureq::Agent::config_builder()
        .http_status_as_error(false)
        .max_redirects(0)
        .build()
        .into()
}

/// Follows redirects (the assets are served from another host).
fn get(agent: &ureq::Agent, url: &str) -> anyhow::Result<ureq::http::Response<ureq::Body>> {
    let mut url = url.to_owned();
    for _ in 0..10 {
        let response = agent
            .get(&url)
            .call()
            .with_context(|| format!("GET {url}"))?;
        let status = response.status();
        if !status.is_redirection() {
            if !status.is_success() {
                bail!("GET {url}: {status}");
            }
            return Ok(response);
        }
        url = location(&response).with_context(|| format!("GET {url}: {status}"))?;
    }
    bail!("GET {url}: too many redirects")
}

fn location(response: &ureq::http::Response<ureq::Body>) -> Option<String> {
    response
        .headers()
        .get("location")?
        .to_str()
        .ok()
        .map(str::to_owned)
}

fn stable(agent: &ureq::Agent, current: Channel) -> anyhow::Result<Offer> {
    let url = format!("https://github.com/{REPOSITORY}/releases/latest");
    let response = agent
        .get(&url)
        .call()
        .with_context(|| format!("GET {url}"))?;
    let tag = location(&response)
        .as_deref()
        .and_then(tag_of)
        .with_context(|| format!("GET {url}: {} names no release", response.status()))?;
    let version = semver::Version::parse(tag.trim_start_matches('v'))
        .with_context(|| format!("the latest release, {tag}, has no version"))?;
    let this = semver::Version::parse(env!("CARGO_PKG_VERSION")).expect("the package version");
    Ok(Offer {
        label: version.to_string(),
        newer: current != Channel::Stable || version > this,
        tag,
    })
}

/// The tag in a release's URL (`…/releases/tag/<tag>`).
fn tag_of(location: &str) -> Option<String> {
    let (_, tag) = location.rsplit_once("/releases/tag/")?;
    let tag = tag.split(['?', '#']).next()?;
    (!tag.is_empty() && !tag.contains('/')).then(|| tag.to_owned())
}

fn nightly(agent: &ureq::Agent, current: Channel) -> anyhow::Result<Offer> {
    let url = format!("https://github.com/{REPOSITORY}/releases/download/{NIGHTLY}/{RELEASE_INFO}");
    let mut body = get(agent, &url)?.into_body();
    let info: ReleaseInfo = serde_json::from_slice(
        &body
            .with_config()
            .limit(64 * 1024)
            .read_to_vec()
            .with_context(|| format!("GET {url}"))?,
    )
    .with_context(|| format!("{url} is not a release description"))?;
    let short = info.commit.get(..7).unwrap_or(&info.commit);
    Ok(Offer {
        tag: NIGHTLY.to_owned(),
        label: match &info.date {
            Some(date) => format!("{} ({short} nightly {date})", info.version),
            None => format!("{} ({short} nightly)", info.version),
        },
        newer: current != Channel::Nightly || !info.commit.starts_with(env!("NSH_GIT_SHA")),
    })
}

/// Downloads `url` to `to`, checked against `url.sha256`.
pub(crate) fn download(
    agent: &ureq::Agent,
    url: &str,
    to: &Path,
    step: &ui::Step,
) -> anyhow::Result<()> {
    let sum_url = format!("{url}.sha256");
    let sum = get(agent, &sum_url)?
        .into_body()
        .read_to_string()
        .with_context(|| format!("GET {sum_url}"))?;
    let expected = sum
        .split_whitespace()
        .next()
        .filter(|hash| hash.len() == 64)
        .with_context(|| format!("{sum_url} holds no SHA-256"))?
        .to_ascii_lowercase();

    let response = get(agent, url)?;
    let total = response
        .headers()
        .get("content-length")
        .and_then(|v| v.to_str().ok())
        .and_then(|v| v.parse::<usize>().ok());
    let mut reader = response.into_body().into_reader();
    let mut file = fs::File::create(to).with_context(|| format!("create {}", to.display()))?;
    let mut hasher = Sha256::new();
    let mut buffer = vec![0; 256 * 1024];
    let mut done = 0;
    loop {
        let n = reader
            .read(&mut buffer)
            .with_context(|| format!("GET {url}"))?;
        if n == 0 {
            break;
        }
        hasher.update(&buffer[..n]);
        file.write_all(&buffer[..n])
            .with_context(|| format!("write {}", to.display()))?;
        done += n;
        step.status(&match total {
            Some(total) => format!("{} of {}", ui::bytes(done), ui::bytes(total)),
            None => ui::bytes(done),
        });
    }
    file.sync_all()?;
    let actual = format!("{:x}", hasher.finalize());
    if actual != expected {
        bail!("the download of {url} does not match its SHA-256");
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_latest_release_is_read_from_its_url() {
        assert_eq!(
            tag_of("https://github.com/longbridge/nsh/releases/tag/v0.4.0").as_deref(),
            Some("v0.4.0")
        );
        assert_eq!(tag_of("https://github.com/longbridge/nsh/releases"), None);
        assert_eq!(
            tag_of("https://github.com/longbridge/nsh/releases/tag/"),
            None
        );
    }

    #[test]
    fn only_a_distribution_updates_itself() {
        let dir = std::env::temp_dir().join(format!("nsh-self-update-{}", std::process::id()));
        let exe = dir.join("nsh/bin/nsh");
        fs::create_dir_all(dir.join("nsh/runtime")).unwrap();
        fs::create_dir_all(exe.parent().unwrap()).unwrap();
        assert_eq!(distribution(&exe).unwrap(), dir.join("nsh"));
        // A checkout's build.
        assert!(distribution(&dir.join("target/release/nsh")).is_err());
        // Package managers update their own.
        let brew = Path::new("/opt/homebrew/Cellar/nsh/0.4.0/bin/nsh");
        assert!(
            distribution(brew)
                .unwrap_err()
                .to_string()
                .contains("brew upgrade")
        );
        let _ = fs::remove_dir_all(&dir);
    }
}