feat: Add logs

This commit is contained in:
Sergio Gasquez 2022-09-17 09:30:06 +02:00
parent ef7e3e6167
commit eda2eb3a23
5 changed files with 56 additions and 14 deletions

32
Cargo.lock generated
View File

@ -348,6 +348,16 @@ dependencies = [
"clap 2.34.0",
]
[[package]]
name = "clap-verbosity-flag"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0636f9c040082f8e161555a305f8cec1a1c2828b3d981c812b8c39f4ac00c42c"
dependencies = [
"clap 3.2.22",
"log",
]
[[package]]
name = "clap_derive"
version = "3.2.18"
@ -652,6 +662,19 @@ dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "env_logger"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "errno"
version = "0.2.8"
@ -680,13 +703,16 @@ dependencies = [
"anyhow",
"clap 3.2.22",
"clap-nested",
"clap-verbosity-flag",
"console",
"dirs",
"env_logger",
"espflash",
"flate2",
"git2",
"guess_host_triple",
"json",
"log",
"md5",
"num_cpus",
"reqwest",
@ -1073,6 +1099,12 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "0.13.10"

View File

@ -27,6 +27,9 @@ xz2 = "0.1.6"
espflash = "1.6.0"
console = "0.15.1"
tempfile = "3.3.0"
clap-verbosity-flag = "1.0.1"
log = "0.4.17"
env_logger = "0.9.0"
[target.'cfg(windows)'.dependencies]
winreg = "0.10.1"

View File

@ -7,6 +7,8 @@ use std::io::Write;
use std::path::{Path, PathBuf};
mod toolchain;
mod utils;
use log::{info, warn};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
// General TODOs:
@ -42,7 +44,7 @@ pub enum SubCommand {
Reinstall(InstallOpts),
}
#[derive(Parser, Debug)]
#[derive(Debug, Parser)]
pub struct InstallOpts {
/// Comma or space separated list of targets [esp32,esp32s2,esp32s3,esp32c3,all].
#[clap(short = 'b', long, default_value = "esp32,esp32s2,esp32s3")]
@ -82,6 +84,9 @@ pub struct InstallOpts {
/// Removes cached distribution files.
#[clap(short = 'x', long)]
pub clear_cache: bool,
/// Verbosity level of the logs.
#[clap(flatten)]
verbose: clap_verbosity_flag::Verbosity,
}
#[derive(Parser, Debug)]
@ -100,6 +105,10 @@ pub struct UninstallOpts {
}
fn install(args: InstallOpts) -> Result<()> {
env_logger::Builder::new()
.filter_level(args.verbose.log_level_filter())
.init();
let arch = guess_host_triple::guess_host_triple().unwrap();
let targets: Vec<Chip> = parse_targets(&args.build_target)?;
let llvm_version = parse_llvm_version(&args.llvm_version).unwrap();
@ -135,7 +144,7 @@ fn install(args: InstallOpts) -> Result<()> {
arch
);
let mut exports: Vec<String> = Vec::new();
info!("{} Installing esp-rs", DISC);
print_arguments(&args, arch, &targets, &llvm_version);
check_rust_installation(&args.nightly_version);
@ -270,17 +279,13 @@ fn install(args: InstallOpts) -> Result<()> {
}
}
install_espidf(&espidf_targets, &espidf_version)?;
exports.push(format!(
"export IDF_TOOLS_PATH=\"{}\"",
get_tools_path()
));
exports.push(format!("export IDF_TOOLS_PATH=\"{}\"", get_tools_path()));
exports.push(format!(
"source {}/export.sh",
get_espidf_path(&espidf_version)
));
// TODO: Install ldproxy
install_extra_crate("ldproxy");
} else {
println!("No esp-idf version provided. Installing gcc for targets");
exports.extend(install_gcc_targets(targets)?.iter().cloned());

View File

@ -1,11 +1,10 @@
use crate::utils::*;
use anyhow::{bail, Result};
use espflash::Chip;
use std::path::Path;
use std::process::Stdio;
use anyhow::{bail, Result};
pub fn check_rust_installation(nightly_version: &str) {
pub fn check_rust_installation(nightly_version: &str) -> Result<()> {
match std::process::Command::new("rustup")
.args(["toolchain", "list"])
.stdout(Stdio::piped())
@ -23,9 +22,10 @@ pub fn check_rust_installation(nightly_version: &str) {
}
Err(e) => {
println!("{}Error: {}", ERROR, e);
install_rustup();
install_rustup()?;
}
}
Ok(())
}
pub fn install_riscv_target(version: &str) {

View File

@ -8,6 +8,7 @@ use std::fs::File;
use std::{fs, io};
// use anyhow::Context;
use anyhow::{bail, Result};
use log::{debug, error, info, warn};
use std::io::{BufReader, Cursor};
use std::path::Path;
use std::process::Stdio;
@ -396,8 +397,9 @@ async fn fetch_url(url: String, output: String) -> Result<(), String> {
}
pub fn print_arguments(args: &InstallOpts, arch: &str, targets: &Vec<Chip>, llvm_version: &str) {
println!(
"{} Installing esp-rs for {} with:
debug!(
"{} Arguments:
- Arch: {}
- Build targets: {:?}
- Cargo home: {:?}
- Clear cache: {:?}
@ -410,7 +412,7 @@ pub fn print_arguments(args: &InstallOpts, arch: &str, targets: &Vec<Chip>, llvm
- Rustup home: {:?}
- Toolchain version: {:?}
- Toolchain destination: {:?}",
DISC,
INFO,
arch,
targets,
&args.cargo_home,