Cleanups
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sun, 11 Mar 2018 06:38:59 +0000 (09:38 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sun, 11 Mar 2018 06:38:59 +0000 (09:38 +0300)
src/bin/cargo.rs
src/bin/cli.rs

index 83c0f78cf4a2577448475c79c1cd860388563a0f..5765125a5e73bf50eff2dd4063398c3a8634e0dd 100644 (file)
@@ -13,6 +13,7 @@ extern crate clap;
 use std::env;
 use std::fs;
 use std::path::{Path, PathBuf};
+use std::collections::BTreeSet;
 
 use cargo::core::shell::Shell;
 use cargo::util::{self, CliResult, lev_distance, Config, CargoResult};
@@ -37,7 +38,7 @@ fn main() {
     let result = {
         init_git_transports(&mut config);
         let _token = cargo::util::job::setup();
-        cli::do_main(&mut config)
+        cli::main(&mut config)
     };
 
     match result {
@@ -73,8 +74,45 @@ fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<Str
     result
 }
 
+/// List all runnable commands
+fn list_commands(config: &Config) -> BTreeSet<(String, Option<String>)> {
+    let prefix = "cargo-";
+    let suffix = env::consts::EXE_SUFFIX;
+    let mut commands = BTreeSet::new();
+    for dir in search_directories(config) {
+        let entries = match fs::read_dir(dir) {
+            Ok(entries) => entries,
+            _ => continue,
+        };
+        for entry in entries.filter_map(|e| e.ok()) {
+            let path = entry.path();
+            let filename = match path.file_name().and_then(|s| s.to_str()) {
+                Some(filename) => filename,
+                _ => continue,
+            };
+            if !filename.starts_with(prefix) || !filename.ends_with(suffix) {
+                continue;
+            }
+            if is_executable(entry.path()) {
+                let end = filename.len() - suffix.len();
+                commands.insert(
+                    (filename[prefix.len()..end].to_string(),
+                     Some(path.display().to_string()))
+                );
+            }
+        }
+    }
+
+    for cmd in commands::builtin() {
+        commands.insert((cmd.get_name().to_string(), None));
+    }
+
+    commands
+}
+
+
 fn find_closest(config: &Config, cmd: &str) -> Option<String> {
-    let cmds = cli::list_commands(config);
+    let cmds = list_commands(config);
     // Only consider candidates with a lev_distance of 3 or less so we don't
     // suggest out-of-the-blue options.
     let mut filtered = cmds.iter()
index f0125b0f81556013a57250ce04d037b0f890ad0c..659790f1c5a2cd49bffe82410ff692eaece9bdc5 100644 (file)
@@ -1,11 +1,9 @@
 extern crate clap;
-#[cfg(never)]
-extern crate cargo;
 
-use std::io::{self, Read, BufRead};
 use std::cmp::min;
-use std::fs::File;
 use std::collections::HashMap;
+use std::fs::File;
+use std::io::{self, Read, BufRead};
 use std::process;
 
 use clap::{AppSettings, Arg, ArgMatches};
@@ -13,20 +11,15 @@ use toml;
 
 use cargo::{self, Config, CargoError, CliResult, CliError};
 use cargo::core::{Source, SourceId, GitReference, Package};
-use cargo::util::{ToUrl, CargoResultExt};
 use cargo::ops::{self, CompileMode, OutputMetadataOptions};
 use cargo::sources::{GitSource, RegistrySource};
+use cargo::util::{ToUrl, CargoResultExt};
 
-use std::collections::BTreeSet;
-use std::env;
-use std::fs;
-
-use search_directories;
-use is_executable;
+use super::list_commands;
+use super::commands;
 use command_prelude::*;
-use commands;
 
-pub fn do_main(config: &mut Config) -> CliResult {
+pub fn main(config: &mut Config) -> CliResult {
     let args = cli().get_matches_safe()?;
     let is_verbose = args.occurrences_of("verbose") > 0;
     if args.is_present("version") {
@@ -642,40 +635,3 @@ See 'cargo help <command>' for more information on a specific command."
     ;
     app
 }
-
-
-/// List all runnable commands
-pub fn list_commands(config: &Config) -> BTreeSet<(String, Option<String>)> {
-    let prefix = "cargo-";
-    let suffix = env::consts::EXE_SUFFIX;
-    let mut commands = BTreeSet::new();
-    for dir in search_directories(config) {
-        let entries = match fs::read_dir(dir) {
-            Ok(entries) => entries,
-            _ => continue,
-        };
-        for entry in entries.filter_map(|e| e.ok()) {
-            let path = entry.path();
-            let filename = match path.file_name().and_then(|s| s.to_str()) {
-                Some(filename) => filename,
-                _ => continue,
-            };
-            if !filename.starts_with(prefix) || !filename.ends_with(suffix) {
-                continue;
-            }
-            if is_executable(entry.path()) {
-                let end = filename.len() - suffix.len();
-                commands.insert(
-                    (filename[prefix.len()..end].to_string(),
-                     Some(path.display().to_string()))
-                );
-            }
-        }
-    }
-
-    for cmd in commands::builtin() {
-        commands.insert((cmd.get_name().to_string(), None));
-    }
-
-    commands
-}