From ad11b2adb2bea792cca6dfae7078afcc0c6a4c83 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Mon, 5 Sep 2022 09:55:15 -0600
Subject: [PATCH] Ensure config and cookbook key are installed

---
 src/bin/installer.rs | 30 +++++++++++++++++++++++++-----
 src/lib.rs           |  4 ++--
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/src/bin/installer.rs b/src/bin/installer.rs
index 8da1b04..bda5d90 100644
--- a/src/bin/installer.rs
+++ b/src/bin/installer.rs
@@ -3,8 +3,7 @@ extern crate redox_installer;
 extern crate serde;
 extern crate toml;
 
-use std::{env, io, process};
-use std::fs::File;
+use std::{env, fs, io, process};
 use std::io::{Read, Write};
 use std::path::Path;
 
@@ -21,10 +20,10 @@ fn main() {
         .add_flag(&["live"]);
     parser.parse(env::args());
 
-    let config = if let Some(path) = parser.get_opt("config") {
-        match File::open(&path) {
+    let mut config_data = String::new();
+    let mut config = if let Some(path) = parser.get_opt("config") {
+        match fs::File::open(&path) {
             Ok(mut config_file) => {
-                let mut config_data = String::new();
                 match config_file.read_to_string(&mut config_data) {
                     Ok(_) => {
                         match toml::from_str(&config_data) {
@@ -52,12 +51,33 @@ fn main() {
         redox_installer::Config::default()
     };
 
+    // Add filesystem.toml to config
+    config.files.push(redox_installer::FileConfig {
+        path: "filesystem.toml".to_string(),
+        data: config_data,
+        ..Default::default()
+    });
+
     let cookbook = if let Some(path) = parser.get_opt("cookbook") {
         if ! Path::new(&path).is_dir() {
             writeln!(stderr, "installer: {}: cookbook not found", path).unwrap();
             process::exit(1);
         }
 
+        // Add cookbook key to config
+        let key_path = Path::new(&path).join("build/id_ed25519.pub.toml");
+        match fs::read_to_string(&key_path) {
+            Ok(data) => config.files.push(redox_installer::FileConfig {
+                path: "pkg/id_ed25519.pub.toml".to_string(),
+                data: data,
+                ..Default::default()
+            }),
+            Err(err) => {
+                writeln!(stderr, "installer: {}: failed to read cookbook key: {}", key_path.display(), err).unwrap();
+                process::exit(1);
+            }
+        }
+
         Some(path)
     } else {
         None
diff --git a/src/lib.rs b/src/lib.rs
index 29cb806..14503df 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -14,8 +14,8 @@ extern crate termion;
 mod config;
 
 pub use config::Config;
-use config::file::FileConfig;
-use config::package::PackageConfig;
+pub use config::file::FileConfig;
+pub use config::package::PackageConfig;
 
 use failure::{Error, err_msg};
 use rand::{RngCore, rngs::OsRng};
-- 
GitLab