Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration file #31

Merged
merged 11 commits into from
Oct 26, 2022
66 changes: 62 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ env_logger = "0.9.0"
embuild = { version = "0.30.4", features = ["espidf", "git"] }
strum = { version = "0.24", features = ["derive"] }
strum_macros = "0.24.3"
toml = "0.5.9"
directories-next = "2.0.0"
serde = { version = "1.0.146", features = ["derive"] }
miette = "5.3.0"
regex = "1.6.0"

[target.aarch64-unknown-linux-gnu.dependencies]
openssl = { version = "0.10", features = ["vendored"] }
Expand All @@ -39,5 +44,5 @@ bin-dir = "{ bin }{ binary-ext }"
pkg-fmt = "zip"

[profile.release]
lto = "thin"
lto = "thin"
strip = true
61 changes: 61 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::{host_triple::HostTriple, targets::Target, toolchain::rust_toolchain::RustToolchain};
use directories_next::ProjectDirs;
use miette::{ErrReport, IntoDiagnostic, Result, WrapErr};
use serde::{Deserialize, Serialize};
use std::{
collections::HashSet,
fs::{create_dir_all, read, write},
path::PathBuf,
};

/// Deserialized contents of a configuration file
#[derive(Debug, Deserialize, Serialize, Default, Clone)]
pub struct Config {
// /// ESP-IDF version
pub espidf_version: Option<String>,
/// Destination of the generated export file.
pub export_file: PathBuf,
/// Extra crates to installed.
pub extra_crates: HashSet<String>,
/// Host triple
pub host_triple: HostTriple,
/// LLVM toolchain path.
pub llvm_path: PathBuf,
/// Nightly Rust toolchain version.
pub nightly_version: String,
/// List of targets instaled.
pub targets: HashSet<Target>,
/// Xtensa Rust toolchain.
pub xtensa_toolchain: RustToolchain,
}

impl Config {
/// Load the config from config file
pub fn load() -> Result<Self> {
let dirs = ProjectDirs::from("rs", "esp", "espup").unwrap();
let file = dirs.config_dir().join("espup.toml");

let config = if let Ok(data) = read(&file) {
toml::from_slice(&data).into_diagnostic()?
} else {
return Err(ErrReport::msg("No config file found"));
};
Ok(config)
}

/// Save the config to file
pub fn save(&self) -> Result<()> {
let dirs = ProjectDirs::from("rs", "esp", "espup").unwrap();
let file = dirs.config_dir().join("espup.toml");

let serialized = toml::to_string(&self.clone())
.into_diagnostic()
.wrap_err("Failed to serialize config")?;
create_dir_all(file.parent().unwrap())
.into_diagnostic()
.wrap_err("Failed to create config directory")?;
write(&file, serialized)
.into_diagnostic()
.wrap_err_with(|| format!("Failed to write config to {}", file.display()))
}
}
6 changes: 4 additions & 2 deletions src/host_triple.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::emoji;
use anyhow::{Context, Result};
use guess_host_triple::guess_host_triple;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use strum::Display;
use strum_macros::EnumString;

#[derive(Display, Debug, Clone, EnumString)]
#[derive(Display, Debug, Clone, EnumString, Deserialize, Serialize, Default)]
pub enum HostTriple {
/// 64-bit Linux
#[strum(serialize = "x86_64-unknown-linux-gnu")]
X86_64UnknownLinuxGnu = 0,
#[default]
X86_64UnknownLinuxGnu,
/// ARM64 Linux
#[strum(serialize = "aarch64-unknown-linux-gnu")]
Aarch64UnknownLinuxGnu,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod config;
pub mod emoji;
pub mod host_triple;
pub mod targets;
Expand Down
Loading