-
Notifications
You must be signed in to change notification settings - Fork 894
/
rustup_mode.rs
1539 lines (1431 loc) · 57.6 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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::error::Error;
use std::fmt;
use std::io::Write;
use std::iter;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use clap::{App, AppSettings, Arg, ArgGroup, ArgMatches, Shell, SubCommand};
use super::common;
use super::errors::*;
use super::help::*;
use super::self_update;
use super::term2;
use super::term2::Terminal;
use super::topical_doc;
use crate::dist::dist::{PartialTargetTriple, PartialToolchainDesc, Profile, TargetTriple};
use crate::dist::manifest::Component;
use crate::process;
use crate::toolchain::{CustomToolchain, DistributableToolchain};
use crate::utils::utils;
use crate::Notification;
use crate::{command, Cfg, ComponentStatus, Toolchain};
fn handle_epipe(res: Result<utils::ExitCode>) -> Result<utils::ExitCode> {
match res {
Err(Error(ErrorKind::Io(ref err), _)) if err.kind() == std::io::ErrorKind::BrokenPipe => {
Ok(utils::ExitCode(0))
}
res => res,
}
}
fn deprecated<F, B>(instead: &str, cfg: &mut Cfg, matches: B, callee: F) -> Result<utils::ExitCode>
where
F: FnOnce(&mut Cfg, B) -> Result<utils::ExitCode>,
{
(cfg.notify_handler)(Notification::PlainVerboseMessage(
"Use of (currently) unmaintained command line interface.",
));
(cfg.notify_handler)(Notification::PlainVerboseMessage(
"The exact API of this command may change without warning",
));
(cfg.notify_handler)(Notification::PlainVerboseMessage(
"Eventually this command will be a true alias. Until then:",
));
(cfg.notify_handler)(Notification::PlainVerboseMessage(&format!(
" Please use `rustup {}` instead",
instead
)));
callee(cfg, matches)
}
pub fn main() -> Result<utils::ExitCode> {
self_update::cleanup_self_updater()?;
let matches = match cli().get_matches_from_safe(process().args_os()) {
Ok(matches) => Ok(matches),
Err(e)
if e.kind == clap::ErrorKind::HelpDisplayed
|| e.kind == clap::ErrorKind::VersionDisplayed =>
{
writeln!(process().stdout().lock(), "{}", e.message)?;
return Ok(utils::ExitCode(0));
}
Err(e) => Err(e),
}?;
let verbose = matches.is_present("verbose");
let quiet = matches.is_present("quiet");
let cfg = &mut common::set_globals(verbose, quiet)?;
if let Some(t) = matches.value_of("+toolchain") {
cfg.set_toolchain_override(&t[1..]);
}
if maybe_upgrade_data(cfg, &matches)? {
return Ok(utils::ExitCode(0));
}
cfg.check_metadata_version()?;
Ok(match matches.subcommand() {
("dump-testament", _) => common::dump_testament()?,
("show", Some(c)) => match c.subcommand() {
("active-toolchain", Some(_)) => handle_epipe(show_active_toolchain(cfg))?,
("home", Some(_)) => handle_epipe(show_rustup_home(cfg))?,
("profile", Some(_)) => handle_epipe(show_profile(cfg))?,
("keys", Some(_)) => handle_epipe(show_keys(cfg))?,
(_, _) => handle_epipe(show(cfg))?,
},
("install", Some(m)) => deprecated("toolchain install", cfg, m, update)?,
("update", Some(m)) => update(cfg, m)?,
("check", Some(_)) => check_updates(cfg)?,
("uninstall", Some(m)) => deprecated("toolchain uninstall", cfg, m, toolchain_remove)?,
("default", Some(m)) => default_(cfg, m)?,
("toolchain", Some(c)) => match c.subcommand() {
("install", Some(m)) => update(cfg, m)?,
("list", Some(m)) => handle_epipe(toolchain_list(cfg, m))?,
("link", Some(m)) => toolchain_link(cfg, m)?,
("uninstall", Some(m)) => toolchain_remove(cfg, m)?,
(_, _) => unreachable!(),
},
("target", Some(c)) => match c.subcommand() {
("list", Some(m)) => handle_epipe(target_list(cfg, m))?,
("add", Some(m)) => target_add(cfg, m)?,
("remove", Some(m)) => target_remove(cfg, m)?,
(_, _) => unreachable!(),
},
("component", Some(c)) => match c.subcommand() {
("list", Some(m)) => handle_epipe(component_list(cfg, m))?,
("add", Some(m)) => component_add(cfg, m)?,
("remove", Some(m)) => component_remove(cfg, m)?,
(_, _) => unreachable!(),
},
("override", Some(c)) => match c.subcommand() {
("list", Some(_)) => handle_epipe(common::list_overrides(cfg))?,
("set", Some(m)) => override_add(cfg, m)?,
("unset", Some(m)) => override_remove(cfg, m)?,
(_, _) => unreachable!(),
},
("run", Some(m)) => run(cfg, m)?,
("which", Some(m)) => which(cfg, m)?,
("doc", Some(m)) => doc(cfg, m)?,
("man", Some(m)) => man(cfg, m)?,
("self", Some(c)) => match c.subcommand() {
("update", Some(_)) => self_update::update(cfg)?,
("uninstall", Some(m)) => self_uninstall(m)?,
(_, _) => unreachable!(),
},
("set", Some(c)) => match c.subcommand() {
("default-host", Some(m)) => set_default_host_triple(cfg, m)?,
("profile", Some(m)) => set_profile(cfg, m)?,
(_, _) => unreachable!(),
},
("completions", Some(c)) => {
if let Some(shell) = c.value_of("shell") {
output_completion_script(
shell.parse::<Shell>().unwrap(),
c.value_of("command")
.and_then(|cmd| cmd.parse::<CompletionCommand>().ok())
.unwrap_or(CompletionCommand::Rustup),
)?
} else {
unreachable!()
}
}
(_, _) => unreachable!(),
})
}
pub fn cli() -> App<'static, 'static> {
let mut app = 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"),
)
.arg(
Arg::with_name("quiet")
.conflicts_with("verbose")
.help("Disable progress output")
.short("q")
.long("quiet"),
)
.arg(
Arg::with_name("+toolchain")
.help("release channel (e.g. +stable) or custom toolchain to set override")
.validator(|s| {
if s.starts_with('+') {
Ok(())
} else {
Err("Toolchain overrides must begin with '+'".into())
}
}),
)
.subcommand(
SubCommand::with_name("dump-testament")
.about("Dump information about the build")
.setting(AppSettings::Hidden), // Not for users, only CI
)
.subcommand(
SubCommand::with_name("show")
.about("Show the active and installed toolchains or profiles")
.after_help(SHOW_HELP)
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.subcommand(
SubCommand::with_name("active-toolchain")
.about("Show the active toolchain")
.after_help(SHOW_ACTIVE_TOOLCHAIN_HELP),
)
.subcommand(
SubCommand::with_name("home")
.about("Display the computed value of RUSTUP_HOME"),
)
.subcommand(SubCommand::with_name("profile").about("Show the current profile"))
.subcommand(SubCommand::with_name("keys").about("Display the known PGP keys")),
)
.subcommand(
SubCommand::with_name("install")
.about("Update Rust toolchains")
.after_help(INSTALL_HELP)
.setting(AppSettings::Hidden) // synonym for 'toolchain install'
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.required(true)
.multiple(true),
)
.arg(
Arg::with_name("profile")
.long("profile")
.takes_value(true)
.possible_values(Profile::names())
.required(false),
)
.arg(
Arg::with_name("no-self-update")
.help("Don't perform self-update when running the `rustup install` command")
.long("no-self-update")
.takes_value(false),
)
.arg(
Arg::with_name("force")
.help("Force an update, even if some components are missing")
.long("force")
.takes_value(false),
),
)
.subcommand(
SubCommand::with_name("uninstall")
.about("Uninstall Rust toolchains")
.setting(AppSettings::Hidden) // synonym for 'toolchain uninstall'
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.required(true)
.multiple(true),
),
)
.subcommand(
SubCommand::with_name("update")
.about("Update Rust toolchains and rustup")
.after_help(UPDATE_HELP)
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.required(false)
.multiple(true),
)
.arg(
Arg::with_name("no-self-update")
.help("Don't perform self update when running the `rustup update` command")
.long("no-self-update")
.takes_value(false),
)
.arg(
Arg::with_name("force")
.help("Force an update, even if some components are missing")
.long("force")
.takes_value(false),
),
)
.subcommand(SubCommand::with_name("check").about("Check for updates to Rust toolchains"))
.subcommand(
SubCommand::with_name("default")
.about("Set the default toolchain")
.after_help(DEFAULT_HELP)
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.required(false),
),
)
.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")
.arg(
Arg::with_name("verbose")
.help("Enable verbose output with toolchain information")
.takes_value(false)
.short("v")
.long("verbose"),
),
)
.subcommand(
SubCommand::with_name("install")
.about("Install or update a given toolchain")
.aliases(&["update", "add"])
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.required(true)
.multiple(true),
)
.arg(
Arg::with_name("profile")
.long("profile")
.takes_value(true)
.possible_values(Profile::names())
.required(false),
)
.arg(
Arg::with_name("no-self-update")
.help(
"Don't perform self update when running the\
`rustup toolchain install` command",
)
.long("no-self-update")
.takes_value(false),
)
.arg(
Arg::with_name("components")
.help("Add specific components on installation")
.long("component")
.short("c")
.takes_value(true)
.multiple(true)
.use_delimiter(true),
)
.arg(
Arg::with_name("targets")
.help("Add specific targets on installation")
.long("target")
.short("t")
.takes_value(true)
.multiple(true)
.use_delimiter(true),
)
.arg(
Arg::with_name("force")
.help("Force an update, even if some components are missing")
.long("force")
.takes_value(false),
)
.arg(
Arg::with_name("allow-downgrade")
.help("Allow rustup to downgrade the toolchain to satisfy your component choice")
.long("allow-downgrade")
.takes_value(false),
),
)
.subcommand(
SubCommand::with_name("uninstall")
.about("Uninstall a toolchain")
.alias("remove")
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.required(true)
.multiple(true),
),
)
.subcommand(
SubCommand::with_name("link")
.about("Create a custom toolchain by symlinking to a directory")
.after_help(TOOLCHAIN_LINK_HELP)
.arg(
Arg::with_name("toolchain")
.help("Custom toolchain name")
.required(true),
)
.arg(
Arg::with_name("path")
.help("Path to the directory")
.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("installed")
.long("--installed")
.help("List only installed targets"),
)
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.long("toolchain")
.takes_value(true),
),
)
.subcommand(
SubCommand::with_name("add")
.about("Add a target to a Rust toolchain")
.alias("install")
.arg(Arg::with_name("target").required(true).multiple(true).help(
"List of targets to install; \
\"all\" installs all available targets",
))
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.long("toolchain")
.takes_value(true),
),
)
.subcommand(
SubCommand::with_name("remove")
.about("Remove a target from a Rust toolchain")
.alias("uninstall")
.arg(Arg::with_name("target").required(true).multiple(true))
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.long("toolchain")
.takes_value(true),
),
),
)
.subcommand(
SubCommand::with_name("component")
.about("Modify a toolchain's installed components")
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(
SubCommand::with_name("list")
.about("List installed and available components")
.arg(
Arg::with_name("installed")
.long("--installed")
.help("List only installed components"),
)
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.long("toolchain")
.takes_value(true),
),
)
.subcommand(
SubCommand::with_name("add")
.about("Add a component to a Rust toolchain")
.arg(Arg::with_name("component").required(true).multiple(true))
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.long("toolchain")
.takes_value(true),
)
.arg(Arg::with_name("target").long("target").takes_value(true)),
)
.subcommand(
SubCommand::with_name("remove")
.about("Remove a component from a Rust toolchain")
.arg(Arg::with_name("component").required(true).multiple(true))
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.long("toolchain")
.takes_value(true),
)
.arg(Arg::with_name("target").long("target").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")
.alias("add")
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.required(true),
)
.arg(
Arg::with_name("path")
.long("path")
.takes_value(true)
.help("Path to the directory"),
),
)
.subcommand(
SubCommand::with_name("unset")
.about("Remove the override toolchain for a directory")
.after_help(OVERRIDE_UNSET_HELP)
.alias("remove")
.arg(
Arg::with_name("path")
.long("path")
.takes_value(true)
.help("Path to the directory"),
)
.arg(
Arg::with_name("nonexistent")
.long("nonexistent")
.takes_value(false)
.help("Remove override toolchain for all nonexistent directories"),
),
),
)
.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("install")
.help("Install the requested toolchain if needed")
.long("install"),
)
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.required(true),
)
.arg(
Arg::with_name("command")
.required(true)
.multiple(true)
.use_delimiter(false),
),
)
.subcommand(
SubCommand::with_name("which")
.about("Display which binary will be run for a given command")
.arg(Arg::with_name("command").required(true))
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.long("toolchain")
.takes_value(true),
),
)
.subcommand(
SubCommand::with_name("doc")
.alias("docs")
.about("Open the documentation for the current toolchain")
.after_help(DOC_HELP)
.arg(
Arg::with_name("path")
.long("path")
.help("Only print the path to the documentation"),
)
.args(
&DOCS_DATA
.iter()
.map(|(name, help_msg, _)| Arg::with_name(name).long(name).help(help_msg))
.collect::<Vec<_>>(),
)
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.long("toolchain")
.takes_value(true),
)
.group(
ArgGroup::with_name("page").args(
&DOCS_DATA
.iter()
.map(|(name, _, _)| *name)
.collect::<Vec<_>>(),
),
)
.arg(Arg::with_name("topic").help(TOPIC_ARG_HELP)),
);
if cfg!(not(target_os = "windows")) {
app = app.subcommand(
SubCommand::with_name("man")
.about("View the man page for a given command")
.arg(Arg::with_name("command").required(true))
.arg(
Arg::with_name("toolchain")
.help(TOOLCHAIN_ARG_HELP)
.long("toolchain")
.takes_value(true),
),
);
}
app = app
.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("set")
.about("Alter rustup settings")
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(
SubCommand::with_name("default-host")
.about("The triple used to identify toolchains when not specified")
.arg(Arg::with_name("host_triple").required(true)),
)
.subcommand(
SubCommand::with_name("profile")
.about("The default components installed")
.arg(
Arg::with_name("profile-name")
.required(true)
.possible_values(Profile::names())
.default_value(Profile::default_name()),
),
),
);
// Clap provides no good way to say that help should be printed in all
// cases where an argument without a default is not provided. The following
// creates lists out all the conditions where the "shell" argument are
// provided and give the default of "rustup". This way if "shell" is not
// provided then the help will still be printed.
let completion_defaults = Shell::variants()
.iter()
.map(|&shell| ("shell", Some(shell), "rustup"))
.collect::<Vec<_>>();
app.subcommand(
SubCommand::with_name("completions")
.about("Generate tab-completion scripts for your shell")
.after_help(COMPLETIONS_HELP)
.setting(AppSettings::ArgRequiredElseHelp)
.arg(Arg::with_name("shell").possible_values(&Shell::variants()))
.arg(
Arg::with_name("command")
.possible_values(&CompletionCommand::variants())
.default_value_ifs(&completion_defaults[..]),
),
)
}
fn maybe_upgrade_data(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<bool> {
match m.subcommand() {
("self", Some(c)) => match c.subcommand() {
("upgrade-data", Some(_)) => {
cfg.upgrade_data()?;
Ok(true)
}
_ => Ok(false),
},
_ => Ok(false),
}
}
fn update_bare_triple_check(cfg: &Cfg, name: &str) -> Result<()> {
if let Some(triple) = PartialTargetTriple::new(name) {
warn!("(partial) target triple specified instead of toolchain name");
let installed_toolchains = cfg.list_toolchains()?;
let default = cfg.find_default()?;
let default_name = default.map(|t| t.name().to_string()).unwrap_or_default();
let mut candidates = vec![];
for t in installed_toolchains {
if t == default_name {
continue;
}
if let Ok(desc) = PartialToolchainDesc::from_str(&t) {
fn triple_comp_eq(given: &str, from_desc: Option<&String>) -> bool {
from_desc.map_or(false, |s| *s == *given)
}
let triple_matches = triple
.arch
.as_ref()
.map_or(true, |s| triple_comp_eq(s, desc.target.arch.as_ref()))
&& triple
.os
.as_ref()
.map_or(true, |s| triple_comp_eq(s, desc.target.os.as_ref()))
&& triple
.env
.as_ref()
.map_or(true, |s| triple_comp_eq(s, desc.target.env.as_ref()));
if triple_matches {
candidates.push(t);
}
}
}
match candidates.len() {
0 => err!("no candidate toolchains found"),
1 => writeln!(
process().stdout(),
"\nyou may use the following toolchain: {}\n",
candidates[0]
)?,
_ => {
writeln!(
process().stdout(),
"\nyou may use one of the following toolchains:"
)?;
for n in &candidates {
writeln!(process().stdout(), "{}", n)?;
}
writeln!(process().stdout(),)?;
}
}
return Err(ErrorKind::ToolchainNotInstalled(name.to_string()).into());
}
Ok(())
}
fn default_bare_triple_check(cfg: &Cfg, name: &str) -> Result<()> {
if let Some(triple) = PartialTargetTriple::new(name) {
warn!("(partial) target triple specified instead of toolchain name");
let default = cfg.find_default()?;
let default_name = default.map(|t| t.name().to_string()).unwrap_or_default();
if let Ok(mut desc) = PartialToolchainDesc::from_str(&default_name) {
desc.target = triple;
let maybe_toolchain = format!("{}", desc);
let toolchain = cfg.get_toolchain(maybe_toolchain.as_ref(), false)?;
if toolchain.name() == default_name {
warn!(
"(partial) triple '{}' resolves to a toolchain that is already default",
name
);
} else {
writeln!(
process().stdout(),
"\nyou may use the following toolchain: {}\n",
toolchain.name()
)?;
}
return Err(ErrorKind::ToolchainNotInstalled(name.to_string()).into());
}
}
Ok(())
}
fn default_(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
if m.is_present("toolchain") {
let toolchain = m.value_of("toolchain").unwrap();
default_bare_triple_check(cfg, toolchain)?;
let toolchain = cfg.get_toolchain(toolchain, false)?;
let status = if !toolchain.is_custom() {
let distributable = DistributableToolchain::new(&toolchain)?;
Some(distributable.install_from_dist_if_not_installed()?)
} else if !toolchain.exists() {
return Err(ErrorKind::ToolchainNotInstalled(toolchain.name().to_string()).into());
} else {
None
};
toolchain.make_default()?;
if let Some(status) = status {
writeln!(process().stdout())?;
common::show_channel_update(cfg, toolchain.name(), Ok(status))?;
}
let cwd = utils::current_dir()?;
if let Some((toolchain, reason)) = cfg.find_override(&cwd)? {
info!(
"note that the toolchain '{}' is currently in use ({})",
toolchain.name(),
reason
);
}
} else {
let default_toolchain: Result<String> = cfg
.get_default()?
.ok_or_else(|| "no default toolchain configured".into());
writeln!(process().stdout(), "{} (default)", default_toolchain?)?;
}
Ok(utils::ExitCode(0))
}
fn check_updates(cfg: &Cfg) -> Result<utils::ExitCode> {
let mut t = term2::stdout();
let channels = cfg.list_channels()?;
for channel in channels {
match channel {
(ref name, Ok(ref toolchain)) => {
let distributable = DistributableToolchain::new(&toolchain)?;
let current_version = distributable.show_version()?;
let dist_version = distributable.show_dist_version()?;
let _ = t.attr(term2::Attr::Bold);
write!(t, "{} - ", name)?;
match (current_version, dist_version) {
(None, None) => {
let _ = t.fg(term2::color::RED);
writeln!(t, "Cannot identify installed or update versions")?;
}
(Some(cv), None) => {
let _ = t.fg(term2::color::GREEN);
write!(t, "Up to date")?;
let _ = t.reset();
writeln!(t, " : {}", cv)?;
}
(Some(cv), Some(dv)) => {
let _ = t.fg(term2::color::YELLOW);
write!(t, "Update available")?;
let _ = t.reset();
writeln!(t, " : {} -> {}", cv, dv)?;
}
(None, Some(dv)) => {
let _ = t.fg(term2::color::YELLOW);
write!(t, "Update available")?;
let _ = t.reset();
writeln!(t, " : (Unknown version) -> {}", dv)?;
}
}
}
(_, Err(err)) => return Err(err.into()),
}
}
Ok(utils::ExitCode(0))
}
fn update(cfg: &mut Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
let self_update = !m.is_present("no-self-update") && !self_update::NEVER_SELF_UPDATE;
if let Some(p) = m.value_of("profile") {
let p = Profile::from_str(p)?;
cfg.set_profile_override(p);
}
let cfg = &cfg;
if cfg.get_profile()? == Profile::Complete {
warn!("{}", common::WARN_COMPLETE_PROFILE);
}
if let Some(names) = m.values_of("toolchain") {
for name in names {
update_bare_triple_check(cfg, name)?;
let toolchain = cfg.get_toolchain(name, false)?;
let status = if !toolchain.is_custom() {
let components: Vec<_> = m
.values_of("components")
.map(|v| v.collect())
.unwrap_or_else(Vec::new);
let targets: Vec<_> = m
.values_of("targets")
.map(|v| v.collect())
.unwrap_or_else(Vec::new);
let distributable = DistributableToolchain::new(&toolchain)?;
Some(distributable.install_from_dist(
m.is_present("force"),
m.is_present("allow-downgrade"),
&components,
&targets,
)?)
} else if !toolchain.exists() {
return Err(ErrorKind::InvalidToolchainName(toolchain.name().to_string()).into());
} else {
None
};
if let Some(status) = status.clone() {
writeln!(process().stdout())?;
common::show_channel_update(cfg, toolchain.name(), Ok(status))?;
}
if cfg.get_default()?.is_none() {
use crate::UpdateStatus;
if let Some(UpdateStatus::Installed) = status {
toolchain.make_default()?;
}
}
}
if self_update {
common::self_update(|| Ok(utils::ExitCode(0)))?;
}
} else {
common::update_all_channels(cfg, self_update, m.is_present("force"))?;
info!("cleaning up downloads & tmp directories");
utils::delete_dir_contents(&cfg.download_dir);
cfg.temp_cfg.clean();
}
Ok(utils::ExitCode(0))
}
fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
let toolchain = m.value_of("toolchain").unwrap();
let args = m.values_of("command").unwrap();
let args: Vec<_> = args.collect();
let cmd = cfg.create_command_for_toolchain(toolchain, m.is_present("install"), args[0])?;
let code = command::run_command_for_dir(cmd, args[0], &args[1..])?;
Ok(code)
}
fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
let binary = m.value_of("command").unwrap();
let binary_path = if m.is_present("toolchain") {
let toolchain = m.value_of("toolchain").unwrap();
cfg.which_binary_by_toolchain(toolchain, binary)?
.expect("binary not found")
} else {
cfg.which_binary(&utils::current_dir()?, binary)?
.expect("binary not found")
};
utils::assert_is_file(&binary_path)?;
writeln!(process().stdout(), "{}", binary_path.display())?;
Ok(utils::ExitCode(0))
}
fn show(cfg: &Cfg) -> Result<utils::ExitCode> {
// Print host triple
{
let mut t = term2::stdout();
t.attr(term2::Attr::Bold)?;
write!(t, "Default host: ")?;
t.reset()?;
writeln!(t, "{}", cfg.get_default_host_triple()?)?;
}
// Print rustup home directory
{
let mut t = term2::stdout();
t.attr(term2::Attr::Bold)?;
write!(t, "rustup home: ")?;
t.reset()?;
writeln!(t, "{}", cfg.rustup_dir.display())?;
writeln!(t)?;
}
let cwd = utils::current_dir()?;
let installed_toolchains = cfg.list_toolchains()?;
// XXX: we may want a find_without_install capability for show.
let active_toolchain = cfg.find_or_install_override_toolchain_or_default(&cwd);
// active_toolchain will carry the reason we don't have one in its detail.
let active_targets = if let Ok(ref at) = active_toolchain {
if let Ok(distributable) = DistributableToolchain::new(&at.0) {
match distributable.list_components() {
Ok(cs_vec) => cs_vec
.into_iter()
.filter(|c| c.component.short_name_in_manifest() == "rust-std")
.filter(|c| c.installed)
.collect(),
Err(_) => vec![],
}
} else {
// These three vec![] could perhaps be reduced with and_then on active_toolchain.
vec![]
}
} 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 {
let mut t = term2::stdout();
if show_headers {
print_header(&mut t, "installed toolchains")?;