-
Notifications
You must be signed in to change notification settings - Fork 892
/
rustup_mode.rs
554 lines (492 loc) · 20.5 KB
/
rustup_mode.rs
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use clap::{App, Arg, ArgGroup, AppSettings, SubCommand, ArgMatches};
use common;
use rustup::{Cfg, Toolchain, command};
use rustup::telemetry::TelemetryMode;
use errors::*;
use rustup_dist::manifest::Component;
use rustup_dist::dist::TargetTriple;
use rustup_utils::utils;
use self_update;
use std::path::Path;
use std::iter;
use term2;
use std::io::Write;
use help::*;
pub fn main() -> Result<()> {
try!(::self_update::cleanup_self_updater());
let ref matches = cli().get_matches();
let verbose = matches.is_present("verbose");
let ref cfg = try!(common::set_globals(verbose));
if try!(maybe_upgrade_data(cfg, matches)) {
return Ok(())
}
try!(cfg.check_metadata_version());
match matches.subcommand() {
("show", Some(_)) => try!(show(cfg)),
("install", Some(m)) => try!(update(cfg, m)),
("update", Some(m)) => try!(update(cfg, m)),
("default", Some(m)) => try!(default_(cfg, m)),
("toolchain", Some(c)) => {
match c.subcommand() {
("install", Some(m)) => try!(update(cfg, m)),
("list", Some(_)) => try!(common::list_toolchains(cfg)),
("link", Some(m)) => try!(toolchain_link(cfg, m)),
("remove", Some(m)) => try!(toolchain_remove(cfg, m)),
// Synonyms
("update", Some(m)) => try!(update(cfg, m)),
(_, _) => unreachable!(),
}
}
("target", Some(c)) => {
match c.subcommand() {
("list", Some(m)) => try!(target_list(cfg, m)),
("add", Some(m)) => try!(target_add(cfg, m)),
("remove", Some(m)) => try!(target_remove(cfg, m)),
// Synonyms
("install", Some(m)) => try!(target_add(cfg, m)),
("uninstall", Some(m)) => try!(target_remove(cfg, m)),
(_, _) => unreachable!(),
}
}
("override", Some(c)) => {
match c.subcommand() {
("list", Some(_)) => try!(common::list_overrides(cfg)),
("set", Some(m)) => try!(override_add(cfg, m)),
("unset", Some(_)) => try!(override_remove(cfg)),
// Synonyms
("add", Some(m)) => try!(override_add(cfg, m)),
("remove", Some(_)) => try!(override_remove(cfg)),
(_ ,_) => unreachable!(),
}
}
("run", Some(m)) => try!(run(cfg, m)),
("which", Some(m)) => try!(which(cfg, m)),
("doc", Some(m)) => try!(doc(cfg, m)),
("self", Some(c)) => {
match c.subcommand() {
("update", Some(_)) => try!(self_update::update()),
("uninstall", Some(m)) => try!(self_uninstall(m)),
(_ ,_) => unreachable!(),
}
}
("telemetry", Some(c)) => {
match c.subcommand() {
("enable", Some(_)) => try!(set_telemetry(&cfg, TelemetryMode::On)),
("disable", Some(_)) => try!(set_telemetry(&cfg, TelemetryMode::Off)),
("analyze", Some(_)) => try!(analyze_telemetry(&cfg)),
(_, _) => unreachable!(),
}
}
(_, _) => unreachable!(),
}
Ok(())
}
pub fn cli() -> App<'static, 'static> {
App::new("rustup")
.version(common::version())
.about("The Rust toolchain installer")
.after_help(RUSTUP_HELP)
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::SubcommandRequiredElseHelp)
.arg(Arg::with_name("verbose")
.help("Enable verbose output")
.short("v")
.long("verbose"))
.subcommand(SubCommand::with_name("show")
.about("Show the active and installed toolchains")
.after_help(SHOW_HELP))
.subcommand(SubCommand::with_name("install")
.about("Update Rust toolchains")
.setting(AppSettings::Hidden) // synonym for 'toolchain install'
.arg(Arg::with_name("toolchain")
.required(true)))
.subcommand(SubCommand::with_name("update")
.about("Update Rust toolchains")
.after_help(UPDATE_HELP)
.arg(Arg::with_name("toolchain")
.required(false))
.arg(Arg::with_name("no-self-update")
.help("Don't perform self update when running the `rustup` command")
.long("no-self-update")
.takes_value(false)
.hidden(true)))
.subcommand(SubCommand::with_name("default")
.about("Set the default toolchain")
.after_help(DEFAULT_HELP)
.arg(Arg::with_name("toolchain")
.required(true)))
.subcommand(SubCommand::with_name("toolchain")
.about("Modify or query the installed toolchains")
.after_help(TOOLCHAIN_HELP)
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list")
.about("List installed toolchains"))
.subcommand(SubCommand::with_name("install")
.about("Install or update a given toolchain")
.arg(Arg::with_name("toolchain")
.required(true)))
.subcommand(SubCommand::with_name("uninstall")
.about("Uninstall a toolchain")
.arg(Arg::with_name("toolchain")
.required(true)))
.subcommand(SubCommand::with_name("link")
.about("Create a custom toolchain by symlinking to a directory")
.arg(Arg::with_name("toolchain")
.required(true))
.arg(Arg::with_name("path")
.required(true)))
.subcommand(SubCommand::with_name("update")
.setting(AppSettings::Hidden) // synonym for 'install'
.arg(Arg::with_name("toolchain")
.required(true)))
.subcommand(SubCommand::with_name("add")
.setting(AppSettings::Hidden) // synonym for 'install'
.arg(Arg::with_name("toolchain")
.required(true)))
.subcommand(SubCommand::with_name("remove")
.setting(AppSettings::Hidden) // synonym for 'uninstall'
.arg(Arg::with_name("toolchain")
.required(true))))
.subcommand(SubCommand::with_name("target")
.about("Modify a toolchain's supported targets")
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list")
.about("List installed and available targets")
.arg(Arg::with_name("toolchain")
.long("toolchain")
.takes_value(true)))
.subcommand(SubCommand::with_name("add")
.about("Add a target to a Rust toolchain")
.arg(Arg::with_name("target")
.required(true))
.arg(Arg::with_name("toolchain")
.long("toolchain")
.takes_value(true)))
.subcommand(SubCommand::with_name("remove")
.about("Remove a target from a Rust toolchain")
.arg(Arg::with_name("target")
.required(true))
.arg(Arg::with_name("toolchain")
.long("toolchain")
.takes_value(true)))
.subcommand(SubCommand::with_name("install")
.setting(AppSettings::Hidden) // synonym for 'add'
.arg(Arg::with_name("target")
.required(true))
.arg(Arg::with_name("toolchain")
.long("toolchain")
.takes_value(true)))
.subcommand(SubCommand::with_name("uninstall")
.setting(AppSettings::Hidden) // synonym for 'remove'
.arg(Arg::with_name("target")
.required(true))
.arg(Arg::with_name("toolchain")
.long("toolchain")
.takes_value(true))))
.subcommand(SubCommand::with_name("override")
.about("Modify directory toolchain overrides")
.after_help(OVERRIDE_HELP)
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list")
.about("List directory toolchain overrides"))
.subcommand(SubCommand::with_name("set")
.about("Set the override toolchain for a directory")
.arg(Arg::with_name("toolchain")
.required(true)))
.subcommand(SubCommand::with_name("unset")
.about("Remove the override toolchain for a directory"))
.subcommand(SubCommand::with_name("add")
.setting(AppSettings::Hidden) // synonym for 'set'
.arg(Arg::with_name("toolchain")
.required(true)))
.subcommand(SubCommand::with_name("remove")
.setting(AppSettings::Hidden) // synonym for 'unset'
.about("Remove the override toolchain for a directory")))
.subcommand(SubCommand::with_name("run")
.about("Run a command with an environment configured for a given toolchain")
.after_help(RUN_HELP)
.setting(AppSettings::TrailingVarArg)
.arg(Arg::with_name("toolchain")
.required(true))
.arg(Arg::with_name("command")
.required(true).multiple(true)))
.subcommand(SubCommand::with_name("which")
.about("Display which binary will be run for a given command")
.arg(Arg::with_name("command")
.required(true)))
.subcommand(SubCommand::with_name("doc")
.about("Open the documentation for the current toolchain.")
.after_help(DOC_HELP)
.arg(Arg::with_name("book")
.long("book")
.help("The Rust Programming Language book"))
.arg(Arg::with_name("std")
.long("std")
.help("Standard library API documentation"))
.group(ArgGroup::with_name("page")
.args(&["book", "std"])))
.subcommand(SubCommand::with_name("self")
.about("Modify the rustup installation")
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("update")
.about("Download and install updates to rustup"))
.subcommand(SubCommand::with_name("uninstall")
.about("Uninstall rustup.")
.arg(Arg::with_name("no-prompt")
.short("y")))
.subcommand(SubCommand::with_name("upgrade-data")
.about("Upgrade the internal data format.")))
.subcommand(SubCommand::with_name("telemetry")
.about("rustup telemetry commands")
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("enable")
.about("Enable rustup telemetry"))
.subcommand(SubCommand::with_name("disable")
.about("Disable rustup telemetry"))
.subcommand(SubCommand::with_name("analyze")
.about("Analyze stored telemetry")))
}
fn maybe_upgrade_data(cfg: &Cfg, m: &ArgMatches) -> Result<bool> {
match m.subcommand() {
("self", Some(c)) => {
match c.subcommand() {
("upgrade-data", Some(_)) => {
try!(cfg.upgrade_data());
Ok(true)
}
_ => Ok(false),
}
}
_ => Ok(false)
}
}
fn default_(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let ref toolchain = m.value_of("toolchain").expect("");
let ref toolchain = try!(cfg.get_toolchain(toolchain, false));
let status = if !toolchain.is_custom() {
Some(try!(toolchain.install_from_dist_if_not_installed()))
} else if !toolchain.exists() {
return Err(ErrorKind::ToolchainNotInstalled(toolchain.name().to_string()).into());
} else {
None
};
try!(toolchain.make_default());
if let Some(status) = status {
println!("");
try!(common::show_channel_update(cfg, toolchain.name(), Ok(status)));
}
Ok(())
}
fn update(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
if let Some(name) = m.value_of("toolchain") {
let toolchain = try!(cfg.get_toolchain(name, false));
let status = if !toolchain.is_custom() {
Some(try!(toolchain.install_from_dist()))
} else if !toolchain.exists() {
return Err(ErrorKind::ToolchainNotInstalled(toolchain.name().to_string()).into());
} else {
None
};
if let Some(status) = status {
println!("");
try!(common::show_channel_update(cfg, toolchain.name(), Ok(status)));
}
} else {
try!(common::update_all_channels(cfg, !m.is_present("no-self-update")));
}
Ok(())
}
fn run(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let ref toolchain = m.value_of("toolchain").expect("");
let args = m.values_of("command").unwrap();
let args: Vec<_> = args.collect();
let cmd = try!(cfg.create_command_for_toolchain(toolchain, args[0]));
Ok(try!(command::run_command_for_dir(cmd, &args, &cfg)))
}
fn which(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let binary = m.value_of("command").expect("");
let binary_path = try!(cfg.which_binary(&try!(utils::current_dir()), binary))
.expect("binary not found");
try!(utils::assert_is_file(&binary_path));
println!("{}", binary_path.display());
Ok(())
}
fn show(cfg: &Cfg) -> Result<()> {
let ref cwd = try!(utils::current_dir());
let installed_toolchains = try!(cfg.list_toolchains());
let active_toolchain = try!(cfg.find_override_toolchain_or_default(cwd));
let active_targets = if let Some((ref t, _)) = active_toolchain {
try!(t.list_components())
.into_iter()
.filter(|c| c.component.pkg == "rust-std")
.filter(|c| c.installed)
.collect()
} else {
vec![]
};
let show_installed_toolchains = installed_toolchains.len() > 1;
let show_active_targets = active_targets.len() > 1;
let show_active_toolchain = true;
// Only need to display headers if we have multiple sections
let show_headers = [
show_installed_toolchains,
show_active_targets,
show_active_toolchain
].iter().filter(|x| **x).count() > 1;
if show_installed_toolchains {
if show_headers { print_header("installed toolchains") }
let default = try!(cfg.find_default());
let default_name = default.map(|t| t.name().to_string())
.unwrap_or("".into());
for t in installed_toolchains {
if default_name == t {
println!("{} (default)", t);
} else {
println!("{}", t);
}
}
if show_headers { println!("") };
}
if show_active_targets {
if show_headers {
print_header("installed targets for active toolchain");
}
for t in active_targets {
println!("{}", t.component.target);
}
if show_headers { println!("") };
}
if show_active_toolchain {
if show_headers { print_header("active toolchain") }
match active_toolchain {
Some((ref toolchain, Some(ref reason))) => {
println!("{} ({})", toolchain.name(), reason);
println!("{}", common::rustc_version(toolchain));
}
Some((ref toolchain, None)) => {
println!("{} (default)", toolchain.name());
println!("{}", common::rustc_version(toolchain));
}
None => {
println!("no active toolchain");
}
}
if show_headers { println!("") };
}
fn print_header(s: &str) {
let mut t = term2::stdout();
let _ = t.attr(term2::Attr::Bold);
let _ = writeln!(t, "{}", s);
let _ = writeln!(t, "{}", iter::repeat("-").take(s.len()).collect::<String>());
let _ = writeln!(t, "");
let _ = t.reset();
}
Ok(())
}
fn target_list(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let toolchain = try!(explicit_or_dir_toolchain(cfg, m));
common::list_targets(&toolchain)
}
fn target_add(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let toolchain = try!(explicit_or_dir_toolchain(cfg, m));
let target = m.value_of("target").expect("");
let new_component = Component {
pkg: "rust-std".to_string(),
target: TargetTriple::from_str(target),
};
Ok(try!(toolchain.add_component(new_component)))
}
fn target_remove(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let toolchain = try!(explicit_or_dir_toolchain(cfg, m));
let target = m.value_of("target").expect("");
let new_component = Component {
pkg: "rust-std".to_string(),
target: TargetTriple::from_str(target),
};
Ok(try!(toolchain.remove_component(new_component)))
}
fn explicit_or_dir_toolchain<'a>(cfg: &'a Cfg, m: &ArgMatches) -> Result<Toolchain<'a>> {
let toolchain = m.value_of("toolchain");
if let Some(toolchain) = toolchain {
let toolchain = try!(cfg.get_toolchain(toolchain, false));
return Ok(toolchain);
}
let ref cwd = try!(utils::current_dir());
let (toolchain, _) = try!(cfg.toolchain_for_dir(cwd));
Ok(toolchain)
}
fn toolchain_link(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let ref toolchain = m.value_of("toolchain").expect("");
let ref path = m.value_of("path").expect("");
let toolchain = try!(cfg.get_toolchain(toolchain, true));
Ok(try!(toolchain.install_from_dir(Path::new(path), true)))
}
fn toolchain_remove(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let ref toolchain = m.value_of("toolchain").expect("");
let toolchain = try!(cfg.get_toolchain(toolchain, false));
Ok(try!(toolchain.remove()))
}
fn override_add(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let ref toolchain = m.value_of("toolchain").expect("");
let toolchain = try!(cfg.get_toolchain(toolchain, false));
let status = if !toolchain.is_custom() {
Some(try!(toolchain.install_from_dist_if_not_installed()))
} else if !toolchain.exists() {
return Err(ErrorKind::ToolchainNotInstalled(toolchain.name().to_string()).into());
} else {
None
};
try!(toolchain.make_override(&try!(utils::current_dir())));
if let Some(status) = status {
println!("");
try!(common::show_channel_update(cfg, toolchain.name(), Ok(status)));
}
Ok(())
}
fn override_remove(cfg: &Cfg) -> Result<()> {
let ref path = try!(utils::current_dir());
let ref override_db = cfg.override_db;
let notify_handler = cfg.notify_handler.as_ref();
if try!(override_db.find(path, notify_handler)).is_none() {
info!("no override toolchain for '{}'", path.display());
return Ok(());
}
try!(override_db.remove(path, &cfg.temp_cfg, notify_handler));
info!("override toolchain for '{}' removed", path.display());
Ok(())
}
fn doc(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let doc_url = if m.is_present("book") {
"book/index.html"
} else if m.is_present("std") {
"std/index.html"
} else {
"index.html"
};
Ok(try!(cfg.open_docs_for_dir(&try!(utils::current_dir()), doc_url)))
}
fn self_uninstall(m: &ArgMatches) -> Result<()> {
let no_prompt = m.is_present("no-prompt");
self_update::uninstall(no_prompt)
}
fn set_telemetry(cfg: &Cfg, t: TelemetryMode) -> Result<()> {
match t {
TelemetryMode::On => Ok(try!(cfg.set_telemetry(true))),
TelemetryMode::Off => Ok(try!(cfg.set_telemetry(false))),
}
}
fn analyze_telemetry(cfg: &Cfg) -> Result<()> {
let analysis = try!(cfg.analyze_telemetry());
common::show_telemetry(analysis)
}