Compare commits

...

3 Commits

Author SHA1 Message Date
65c7ed6349 feat: Generate definitions for config
All checks were successful
Build and deploy / build (push) Successful in 9m15s
Build and deploy / Deploy container (push) Has been skipped
2025-10-17 03:15:27 +02:00
a0ed373971 refactor: Move definition writing into separate function
Some checks failed
Build and deploy / Deploy container (push) Blocked by required conditions
Build and deploy / build (push) Has been cancelled
2025-10-17 03:12:40 +02:00
5e13dff2b5 chore: Move main.rs to bin/automation.rs 2025-10-17 03:08:37 +02:00
8 changed files with 61 additions and 19 deletions

1
Cargo.lock generated
View File

@@ -99,6 +99,7 @@ dependencies = [
"git-version",
"google_home",
"inventory",
"lua_typed",
"mlua",
"reqwest",
"rumqttc",

View File

@@ -77,6 +77,7 @@ config = { version = "0.15.15", default-features = false, features = [
] }
git-version = "0.3.9"
google_home = { workspace = true }
lua_typed = { workspace = true }
inventory = { workspace = true }
mlua = { workspace = true }
reqwest = { workspace = true }

View File

@@ -744,6 +744,7 @@ device_manager:schedule("0 0 20 * * *", function()
bedroom_air_filter:set_on(false)
end)
---@type Config
return {
fulfillment = {
openid_url = "https://login.huizinga.dev/api/oidc",

12
definitions/config.lua Normal file
View File

@@ -0,0 +1,12 @@
-- DO NOT MODIFY, FILE IS AUTOMATICALLY GENERATED
---@meta
---@class FulfillmentConfig
---@field openid_url string
---@field ip string?
---@field port integer?
local FulfillmentConfig
---@class Config
---@field fulfillment FulfillmentConfig
local Config

View File

@@ -1,14 +1,14 @@
#![feature(iter_intersperse)]
mod config;
mod secret;
mod version;
mod web;
use std::net::SocketAddr;
use std::path::Path;
use std::process;
use ::config::{Environment, File};
use automation::config::{Config, Setup};
use automation::secret::EnvironmentSecretFile;
use automation::version::VERSION;
use automation::web::{ApiError, User};
use automation_lib::device_manager::DeviceManager;
use axum::extract::{FromRef, State};
use axum::http::StatusCode;
@@ -18,11 +18,6 @@ use google_home::{GoogleHome, Request, Response};
use mlua::LuaSerdeExt;
use tokio::net::TcpListener;
use tracing::{debug, error, info, warn};
use web::{ApiError, User};
use crate::config::{Config, Setup};
use crate::secret::EnvironmentSecretFile;
use crate::version::VERSION;
// Force automation_devices to link so that it gets registered as a module
extern crate automation_devices;

View File

@@ -1,32 +1,57 @@
use std::fs::{self, File};
use std::io::Write;
use automation::config::{Config, FulfillmentConfig};
use automation_lib::Module;
use lua_typed::Typed;
use tracing::{info, warn};
extern crate automation_devices;
fn main() -> std::io::Result<()> {
tracing_subscriber::fmt::init();
fn write_definitions(filename: &str, definitions: &str) -> std::io::Result<()> {
let definitions_directory =
std::path::Path::new(std::env!("CARGO_MANIFEST_DIR")).join("definitions");
fs::create_dir_all(&definitions_directory)?;
let mut file = File::create(definitions_directory.join(filename))?;
file.write_all(b"-- DO NOT MODIFY, FILE IS AUTOMATICALLY GENERATED\n")?;
file.write_all(definitions.as_bytes())?;
// Make sure we have a trailing new line
if !definitions.ends_with("\n") {
file.write_all(b"\n")?;
}
Ok(())
}
fn config_definitions() -> String {
let mut output = "---@meta\n\n".to_string();
output +=
&FulfillmentConfig::generate_full().expect("FulfillmentConfig should have a definition");
output += "\n";
output += &Config::generate_full().expect("FulfillmentConfig should have a definition");
output
}
fn main() -> std::io::Result<()> {
tracing_subscriber::fmt::init();
for module in inventory::iter::<Module> {
if let Some(definitions) = module.definitions() {
info!(name = module.get_name(), "Generating definitions");
let filename = format!("{}.lua", module.get_name());
let mut file = File::create(definitions_directory.join(filename))?;
file.write_all(b"-- DO NOT MODIFY, FILE IS AUTOMATICALLY GENERATED\n")?;
file.write_all(definitions.as_bytes())?;
file.write_all(b"\n")?;
write_definitions(&filename, &definitions)?;
} else {
warn!(name = module.get_name(), "No definitions");
}
}
write_definitions("config.lua", &config_definitions())?;
Ok(())
}

View File

@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::net::{Ipv4Addr, SocketAddr};
use lua_typed::Typed;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
@@ -17,16 +18,18 @@ fn default_entrypoint() -> String {
"./config.lua".into()
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Typed)]
pub struct FulfillmentConfig {
pub openid_url: String,
#[serde(default = "default_fulfillment_ip")]
#[typed(default)]
pub ip: Ipv4Addr,
#[serde(default = "default_fulfillment_port")]
#[typed(default)]
pub port: u16,
}
#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Typed)]
pub struct Config {
pub fulfillment: FulfillmentConfig,
}

4
src/lib.rs Normal file
View File

@@ -0,0 +1,4 @@
pub mod config;
pub mod secret;
pub mod version;
pub mod web;