Skip to content

Commit 64d2ffe

Browse files
committed
feat: dump-state --no-net
1 parent 9fd5383 commit 64d2ffe

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

src/elan-cli/elan_mode.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn main() -> Result<()> {
5858
);
5959
}
6060
},
61-
("dump-state", Some(_)) => json_dump::StateDump::new(cfg)?.print()?,
61+
("dump-state", Some(m)) => dump_state(cfg, m)?,
6262
(_, _) => unreachable!(),
6363
}
6464

@@ -178,7 +178,11 @@ pub fn cli() -> App<'static, 'static> {
178178
.about("Display which binary will be run for a given command")
179179
.arg(Arg::with_name("command")
180180
.required(true)))
181-
.subcommand(SubCommand::with_name("dump-state").setting(AppSettings::Hidden))
181+
.subcommand(SubCommand::with_name("dump-state")
182+
.setting(AppSettings::Hidden)
183+
.arg(Arg::with_name("no-net")
184+
.help("Make network operations for resolving channels fail immediately")
185+
.long("no-net")))
182186
/*.subcommand(SubCommand::with_name("doc")
183187
.alias("docs")
184188
.about("Open the documentation for the current toolchain")
@@ -551,3 +555,9 @@ fn self_uninstall(m: &ArgMatches) -> Result<()> {
551555

552556
self_update::uninstall(no_prompt)
553557
}
558+
559+
fn dump_state(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
560+
let no_net = m.is_present("no-net");
561+
562+
Ok(json_dump::StateDump::new(cfg, no_net)?.print()?)
563+
}

src/elan-cli/json_dump.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use elan::{lookup_toolchain_desc, utils::{self, fetch_latest_release_tag}, Cfg, Toolchain};
1+
use elan::{lookup_toolchain_desc_ext, utils::{self, fetch_latest_release_tag}, Cfg, Toolchain};
22
use std::{io, path::PathBuf};
33

44
use serde_derive::Serialize;
@@ -50,8 +50,8 @@ pub struct StateDump {
5050
}
5151

5252
impl StateDump {
53-
pub fn new(cfg: &Cfg) -> crate::Result<StateDump> {
54-
let newest = fetch_latest_release_tag("leanprover/elan", None);
53+
pub fn new(cfg: &Cfg, no_net: bool) -> crate::Result<StateDump> {
54+
let newest = fetch_latest_release_tag("leanprover/elan", None, no_net);
5555
let ref cwd = utils::current_dir()?;
5656
let active_override = cfg.find_override(cwd)?;
5757
let default = cfg.get_default()?;
@@ -69,15 +69,15 @@ impl StateDump {
6969
}).collect(),
7070
default: default.as_ref().map(|default| DefaultToolchain {
7171
unresolved: default.clone(),
72-
resolved: lookup_toolchain_desc(cfg, &default)
72+
resolved: lookup_toolchain_desc_ext(cfg, &default, no_net)
7373
.map(|t| t.to_string())
7474
.map_err(|e| e.to_string()),
7575
}),
7676
active_override: active_override.as_ref().map(|p| p.1.clone()),
7777
resolved_active: active_override
7878
.map(|p| p.0.desc.to_string())
7979
.or(default)
80-
.map(|t| lookup_toolchain_desc(cfg, &t)
80+
.map(|t| lookup_toolchain_desc_ext(cfg, &t, no_net)
8181
.map(|tc| tc.to_string())
8282
.map_err(|e| e.to_string()))
8383
},

src/elan-utils/src/utils.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,17 @@ pub fn fetch_url(url: &str) -> Result<String> {
471471
pub fn fetch_latest_release_tag(
472472
repo_slug: &str,
473473
notify_handler: Option<&dyn Fn(Notification)>,
474+
no_net: bool
474475
) -> Result<String> {
475476
use regex::Regex;
476477

477478
let latest_url = format!("https://github.com/{}/releases/latest", repo_slug);
478-
match fetch_url(&latest_url) {
479+
let res = if no_net {
480+
Err(Error::from("Cannot fetch latest release tag under `--no-net`"))
481+
} else {
482+
fetch_url(&latest_url)
483+
};
484+
match res {
479485
Ok(redirect) => {
480486
let re = Regex::new(r#"/tag/([-a-z0-9.]+)"#).unwrap();
481487
let capture = re.captures(&redirect);

src/elan/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn check_self_update() -> Result<Option<String>> {
2626
// Get current version
2727
let current_version = env!("CARGO_PKG_VERSION");
2828

29-
let tag = fetch_latest_release_tag("leanprover/elan", None)?;
29+
let tag = fetch_latest_release_tag("leanprover/elan", None, false)?;
3030
let available_version = &tag[1..];
3131

3232
Ok(if available_version == current_version {

src/elan/toolchain.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub struct ComponentStatus {
3636
pub available: bool,
3737
}
3838

39-
pub fn lookup_toolchain_desc(cfg: &Cfg, name: &str) -> Result<ToolchainDesc> {
39+
pub fn lookup_toolchain_desc_ext(cfg: &Cfg, name: &str, no_net: bool) -> Result<ToolchainDesc> {
4040
let pattern = r"^(?:([a-zA-Z0-9-]+[/][a-zA-Z0-9-]+)[:])?([a-zA-Z0-9-.]+)$";
4141

4242
let re = Regex::new(pattern).unwrap();
@@ -64,14 +64,15 @@ pub fn lookup_toolchain_desc(cfg: &Cfg, name: &str) -> Result<ToolchainDesc> {
6464
"https://raw.githubusercontent.com/{}/HEAD/lean-toolchain",
6565
origin
6666
);
67-
return lookup_toolchain_desc(cfg, fetch_url(&toolchain_url)?.trim());
67+
return lookup_toolchain_desc_ext(cfg, fetch_url(&toolchain_url)?.trim(), no_net);
6868
}
6969
let mut from_channel = None;
7070
if release == "stable" || release == "beta" || release == "nightly" {
7171
from_channel = Some(release);
7272
release = utils::fetch_latest_release_tag(
7373
&origin,
7474
Some(&move |n| (cfg.notify_handler)(n.into())),
75+
no_net
7576
)?;
7677
}
7778
if release.starts_with(char::is_numeric) {
@@ -83,6 +84,10 @@ pub fn lookup_toolchain_desc(cfg: &Cfg, name: &str) -> Result<ToolchainDesc> {
8384
}
8485
}
8586

87+
pub fn lookup_toolchain_desc(cfg: &Cfg, name: &str) -> Result<ToolchainDesc> {
88+
lookup_toolchain_desc_ext(cfg, name, false)
89+
}
90+
8691
impl<'a> Toolchain<'a> {
8792
pub fn from(cfg: &'a Cfg, desc: &ToolchainDesc) -> Self {
8893
//We need to replace ":" and "/" with "-" in the toolchain name in order to make a name which is a valid

0 commit comments

Comments
 (0)