Load wireless credentials

This commit is contained in:
Invariantspace 2024-10-13 00:25:18 -07:00
parent 62d50fd739
commit fc1105fdc3
No known key found for this signature in database
GPG key ID: EBC4A20067373921
7 changed files with 115 additions and 17 deletions

View file

@ -1,33 +1,58 @@
//! This build script copies the `memory.x` file from the crate root into
//! a directory where the linker can always find it at build time.
//! For many projects this is optional, as the linker always searches the
//! project root directory -- wherever `Cargo.toml` is. However, if you
//! are using a workspace or have a more complicated build setup, this
//! build script becomes required. Additionally, by requesting that
//! Cargo re-run the build script whenever `memory.x` is changed,
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
use serde::Deserialize;
#[derive(Default, Deserialize)]
struct WirelessCredentials {
wireless_credentials: HashMap<String, String>,
}
fn load_wireless_credentials() -> Option<WirelessCredentials> {
let yaml_string = String::from_utf8(
Command::new("sops")
.arg("-d")
.arg("secrets.yaml")
.output()
.ok()?
.stdout,
)
.ok()?;
serde_yaml::from_str(&yaml_string).ok()
}
fn main() {
// Put `memory.x` in our output directory and ensure it's
// on the linker search path.
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("memory.x"))
.unwrap();
println!("cargo:rustc-link-search={}", out.display());
// By default, Cargo will re-run a build script whenever
// any file in the project changes. By specifying `memory.x`
// here, we ensure the build script is only re-run when
// `memory.x` is changed.
let credentials = load_wireless_credentials()
.unwrap_or_default()
.wireless_credentials;
let credential_entries: Vec<String> = credentials
.into_iter()
.map(|(ssid, password)| format!("(\"{ssid}\", \"{password}\")"))
.collect();
let credential_slice_string = format!("[{}]", credential_entries.join(", "));
File::create(out.join("wireless-credentials.rs"))
.unwrap()
.write_all(credential_slice_string.as_bytes())
.unwrap();
println!("cargo:rerun-if-changed=memory.x");
println!("cargo:rerun-if-changed=secrets.yaml");
println!("cargo:rustc-link-arg-bins=--nmagic");
println!("cargo:rustc-link-arg-bins=-Tlink.x");