Skip to content

Commit fac11e1

Browse files
authored
Merge pull request #2360 from rbtcollins/cli-as-lib
Make the CLI code a library
2 parents 0f19d81 + aa1deeb commit fac11e1

17 files changed

+153
-127
lines changed

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ name = "rustup"
105105
path = "src/lib.rs"
106106
test = false # no unit tests
107107

108-
[[bin]]
109-
name = "rustup-init"
110-
path = "src/cli/main.rs"
111-
112108
[profile.release]
113109
lto = true
114110
codegen-units = 1

src/cli/main.rs renamed to src/bin/rustup-init.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,20 @@
1414
1515
#![recursion_limit = "1024"]
1616

17-
#[macro_use]
18-
mod log;
19-
mod common;
20-
mod download_tracker;
21-
mod errors;
22-
mod help;
23-
mod job;
24-
mod markdown;
25-
mod proxy_mode;
26-
mod rustup_mode;
27-
mod self_update;
28-
mod setup_mode;
29-
mod term2;
30-
mod topical_doc;
31-
32-
use crate::errors::*;
33-
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
34-
use rustup::utils::utils;
35-
3617
use std::env;
3718
use std::path::PathBuf;
3819

3920
use rs_tracing::*;
4021

22+
use rustup::cli::common;
23+
use rustup::cli::errors::*;
24+
use rustup::cli::proxy_mode;
25+
use rustup::cli::rustup_mode;
26+
use rustup::cli::self_update;
27+
use rustup::cli::setup_mode;
28+
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
29+
use rustup::utils::utils;
30+
4131
fn main() {
4232
if let Err(ref e) = run_rustup() {
4333
common::report_error(e);

src/cli.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// The CLI specific code lives in the cli module and sub-modules.
2+
#[macro_use]
3+
pub mod log;
4+
pub mod common;
5+
mod download_tracker;
6+
pub mod errors;
7+
pub mod help;
8+
mod job;
9+
mod markdown;
10+
pub mod proxy_mode;
11+
pub mod rustup_mode;
12+
pub mod self_update;
13+
pub mod setup_mode;
14+
mod term2;
15+
mod topical_doc;

src/cli/common.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
//! Just a dumping ground for cli stuff
22
3-
use crate::errors::*;
4-
use crate::self_update;
5-
use crate::term2;
6-
use git_testament::{git_testament, render_testament};
7-
use lazy_static::lazy_static;
8-
use rustup::dist::notifications as dist_notifications;
9-
use rustup::toolchain::DistributableToolchain;
10-
use rustup::utils::notifications as util_notifications;
11-
use rustup::utils::notify::NotificationLevel;
12-
use rustup::utils::utils;
13-
use rustup::{Cfg, Notification, Toolchain, UpdateStatus};
143
use std::fs;
154
use std::io::{BufRead, ErrorKind, Write};
165
use std::path::Path;
176
use std::sync::Arc;
187
use std::{cmp, env, iter};
8+
9+
use git_testament::{git_testament, render_testament};
10+
use lazy_static::lazy_static;
1911
use term2::Terminal;
2012

13+
use super::errors::*;
14+
use super::self_update;
15+
use super::term2;
16+
use crate::dist::notifications as dist_notifications;
17+
use crate::toolchain::DistributableToolchain;
18+
use crate::utils::notifications as util_notifications;
19+
use crate::utils::notify::NotificationLevel;
20+
use crate::utils::utils;
21+
use crate::{Cfg, Notification, Toolchain, UpdateStatus};
22+
2123
pub const WARN_COMPLETE_PROFILE: &str = "downloading with complete profile isn't recommended unless you are a developer of the rust language";
2224

2325
pub fn confirm(question: &str, default: bool) -> Result<bool> {
@@ -148,9 +150,10 @@ impl NotifyOnConsole {
148150
}
149151

150152
pub fn set_globals(verbose: bool, quiet: bool) -> Result<Cfg> {
151-
use crate::download_tracker::DownloadTracker;
152153
use std::cell::RefCell;
153154

155+
use super::download_tracker::DownloadTracker;
156+
154157
let download_tracker = RefCell::new(DownloadTracker::new().with_display_progress(!quiet));
155158
let console_notifier = RefCell::new(NotifyOnConsole {
156159
verbose,
@@ -168,14 +171,14 @@ pub fn set_globals(verbose: bool, quiet: bool) -> Result<Cfg> {
168171
pub fn show_channel_update(
169172
cfg: &Cfg,
170173
name: &str,
171-
updated: rustup::Result<UpdateStatus>,
174+
updated: crate::Result<UpdateStatus>,
172175
) -> Result<()> {
173176
show_channel_updates(cfg, vec![(name.to_string(), updated)])
174177
}
175178

176179
fn show_channel_updates(
177180
cfg: &Cfg,
178-
toolchains: Vec<(String, rustup::Result<UpdateStatus>)>,
181+
toolchains: Vec<(String, crate::Result<UpdateStatus>)>,
179182
) -> Result<()> {
180183
let data = toolchains.into_iter().map(|(name, result)| {
181184
let toolchain = cfg.get_toolchain(&name, false).unwrap();
@@ -348,7 +351,7 @@ where
348351
pub fn list_targets(toolchain: &Toolchain<'_>) -> Result<()> {
349352
let mut t = term2::stdout();
350353
let distributable = DistributableToolchain::new(&toolchain)
351-
.chain_err(|| rustup::ErrorKind::ComponentsUnsupported(toolchain.name().to_string()))?;
354+
.chain_err(|| crate::ErrorKind::ComponentsUnsupported(toolchain.name().to_string()))?;
352355
let components = distributable.list_components()?;
353356
for component in components {
354357
if component.component.short_name_in_manifest() == "rust-std" {
@@ -373,7 +376,7 @@ pub fn list_targets(toolchain: &Toolchain<'_>) -> Result<()> {
373376
pub fn list_installed_targets(toolchain: &Toolchain<'_>) -> Result<()> {
374377
let mut t = term2::stdout();
375378
let distributable = DistributableToolchain::new(&toolchain)
376-
.chain_err(|| rustup::ErrorKind::ComponentsUnsupported(toolchain.name().to_string()))?;
379+
.chain_err(|| crate::ErrorKind::ComponentsUnsupported(toolchain.name().to_string()))?;
377380
let components = distributable.list_components()?;
378381
for component in components {
379382
if component.component.short_name_in_manifest() == "rust-std" {
@@ -393,7 +396,7 @@ pub fn list_installed_targets(toolchain: &Toolchain<'_>) -> Result<()> {
393396
pub fn list_components(toolchain: &Toolchain<'_>) -> Result<()> {
394397
let mut t = term2::stdout();
395398
let distributable = DistributableToolchain::new(&toolchain)
396-
.chain_err(|| rustup::ErrorKind::ComponentsUnsupported(toolchain.name().to_string()))?;
399+
.chain_err(|| crate::ErrorKind::ComponentsUnsupported(toolchain.name().to_string()))?;
397400
let components = distributable.list_components()?;
398401
for component in components {
399402
let name = component.name;
@@ -412,7 +415,7 @@ pub fn list_components(toolchain: &Toolchain<'_>) -> Result<()> {
412415
pub fn list_installed_components(toolchain: &Toolchain<'_>) -> Result<()> {
413416
let mut t = term2::stdout();
414417
let distributable = DistributableToolchain::new(&toolchain)
415-
.chain_err(|| rustup::ErrorKind::ComponentsUnsupported(toolchain.name().to_string()))?;
418+
.chain_err(|| crate::ErrorKind::ComponentsUnsupported(toolchain.name().to_string()))?;
416419
let components = distributable.list_components()?;
417420
for component in components {
418421
if component.installed {
@@ -586,7 +589,7 @@ pub fn report_error(e: &Error) {
586589
}
587590
}
588591

589-
pub fn ignorable_error(error: crate::errors::Error, no_prompt: bool) -> Result<()> {
592+
pub fn ignorable_error(error: super::errors::Error, no_prompt: bool) -> Result<()> {
590593
report_error(&error);
591594
if no_prompt {
592595
warn!("continuing (because the -y flag is set and the error is ignorable)");

src/cli/download_tracker.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
use crate::term2;
2-
use rustup::dist::Notification as In;
3-
use rustup::utils::tty;
4-
use rustup::utils::units::{Size, Unit, UnitMode};
5-
use rustup::utils::Notification as Un;
6-
use rustup::Notification;
71
use std::collections::VecDeque;
82
use std::fmt;
93
use std::io::Write;
104
use std::time::{Duration, Instant};
5+
116
use term::Terminal;
127

8+
use super::term2;
9+
use crate::dist::Notification as In;
10+
use crate::utils::tty;
11+
use crate::utils::units::{Size, Unit, UnitMode};
12+
use crate::utils::Notification as Un;
13+
use crate::Notification;
14+
1315
/// Keep track of this many past download amounts
1416
const DOWNLOAD_TRACK_COUNT: usize = 5;
1517

src/cli/errors.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
#![allow(dead_code)]
33
#![allow(deprecated)] // because of `Error::description` deprecation in `error_chain`
44

5-
use crate::rustup_mode::CompletionCommand;
6-
75
use std::io;
86
use std::path::PathBuf;
97

108
use clap::Shell;
119
use error_chain::error_chain;
1210
use lazy_static::lazy_static;
1311
use regex::Regex;
14-
use rustup::dist::temp;
1512
use strsim::damerau_levenshtein;
1613

14+
use super::rustup_mode::CompletionCommand;
15+
use crate::dist::temp;
16+
1717
error_chain! {
1818
links {
19-
Rustup(rustup::Error, rustup::ErrorKind);
19+
Rustup(crate::Error, crate::ErrorKind);
2020
}
2121

2222
foreign_links {

src/cli/log.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
use crate::term2;
21
use std::fmt;
32
use std::io::Write;
43
use term2::Terminal;
54

5+
use super::term2;
6+
67
macro_rules! warn {
7-
( $ ( $ arg : tt ) * ) => ( $crate::log::warn_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
8+
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::warn_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
89
}
910
macro_rules! err {
10-
( $ ( $ arg : tt ) * ) => ( $crate::log::err_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
11+
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::err_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
1112
}
1213
macro_rules! info {
13-
( $ ( $ arg : tt ) * ) => ( $crate::log::info_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
14+
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::info_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
1415
}
1516

1617
macro_rules! verbose {
17-
( $ ( $ arg : tt ) * ) => ( $crate::log::verbose_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
18+
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::verbose_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
1819
}
1920

2021
macro_rules! debug {
21-
( $ ( $ arg : tt ) * ) => ( $crate::log::debug_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
22+
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::debug_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
2223
}
2324

2425
pub fn warn_fmt(args: fmt::Arguments<'_>) {

src/cli/markdown.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Write Markdown to the terminal
22

3-
use crate::term2::{color, Attr, Terminal};
43
use std::io;
54

65
use pulldown_cmark::{Event, Tag};
76

7+
use super::term2::{color, Attr, Terminal};
8+
89
// Handles the wrapping of text written to the console
910
struct LineWrapper<'a, T: Terminal> {
1011
indent: u32,

src/cli/proxy_mode.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
use crate::common::set_globals;
2-
use crate::errors::*;
3-
use crate::job;
4-
use rustup::command::run_command_for_dir;
5-
use rustup::utils::utils::{self, ExitCode};
6-
use rustup::Cfg;
71
use std::env;
82
use std::ffi::OsString;
93
use std::path::PathBuf;
104
use std::process;
115

6+
use super::common::set_globals;
7+
use super::errors::*;
8+
use super::job;
9+
use super::self_update;
10+
use crate::command::run_command_for_dir;
11+
use crate::utils::utils::{self, ExitCode};
12+
use crate::Cfg;
13+
1214
pub fn main() -> Result<()> {
13-
crate::self_update::cleanup_self_updater()?;
15+
self_update::cleanup_self_updater()?;
1416

1517
let ExitCode(c) = {
1618
let _setup = job::setup();

0 commit comments

Comments
 (0)