homeboy 0.80.0

CLI for multi-component deployment and development workflow automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Generic baseline & ratchet primitive for drift detection.
//!
//! Captures a snapshot of "current state" (any set of fingerprintable items)
//! and compares future runs against it. Only NEW items (not in the baseline)
//! trigger a failure — resolved items are celebrated, same-state passes.
//!
//! Zero domain knowledge. The caller decides:
//! - What gets fingerprinted (via [`Fingerprintable`])
//! - What metadata to store (via the `M` type parameter)
//! - What key to use in `homeboy.json` (via [`BaselineConfig`])
//!
//! Baselines are stored in the project's `homeboy.json` under a `baselines`
//! key, keeping all component configuration in a single portable file.
//!
//! # Usage
//!
//! ```ignore
//! use homeboy::baseline::{self, Fingerprintable, BaselineConfig};
//!
//! impl Fingerprintable for MyFinding {
//!     fn fingerprint(&self) -> String {
//!         format!("{}::{}", self.category, self.file)
//!     }
//!     fn description(&self) -> String {
//!         self.message.clone()
//!     }
//!     fn context_label(&self) -> String {
//!         self.category.clone()
//!     }
//! }
//!
//! let config = BaselineConfig::new(source_path, "audit");
//! baseline::save(&config, "my-component", &items, my_metadata)?;
//! if let Some(saved) = baseline::load::<MyMeta>(&config)? {
//!     let comparison = baseline::compare(&items, &saved);
//!     if comparison.drift_increased {
//!         // CI fails — new findings introduced
//!     }
//! }
//! ```

use std::collections::HashSet;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::error::{Error, Result};

pub trait Fingerprintable {
    fn fingerprint(&self) -> String;
    fn description(&self) -> String;
    fn context_label(&self) -> String;
}

pub struct BaselineConfig {
    root: PathBuf,
    key: String,
}

const HOMEBOY_JSON: &str = "homeboy.json";
const BASELINES_KEY: &str = "baselines";

impl BaselineConfig {
    pub fn new(root: impl Into<PathBuf>, key: impl Into<String>) -> Self {
        Self {
            root: root.into(),
            key: key.into(),
        }
    }

    pub fn json_path(&self) -> PathBuf {
        self.root.join(HOMEBOY_JSON)
    }

    pub fn key(&self) -> &str {
        &self.key
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Baseline<M: Serialize> {
    pub created_at: String,
    pub context_id: String,
    pub item_count: usize,
    pub known_fingerprints: Vec<String>,
    pub metadata: M,
}

#[derive(Debug, Clone, Serialize)]
pub struct Comparison {
    pub new_items: Vec<NewItem>,
    pub resolved_fingerprints: Vec<String>,
    pub delta: i64,
    pub drift_increased: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct NewItem {
    pub fingerprint: String,
    pub description: String,
    pub context_label: String,
}

pub fn save<M: Serialize + for<'de> Deserialize<'de>>(
    config: &BaselineConfig,
    context_id: &str,
    items: &[impl Fingerprintable],
    metadata: M,
) -> Result<PathBuf> {
    let mut known_fingerprints: Vec<String> = items.iter().map(|item| item.fingerprint()).collect();
    known_fingerprints.sort();

    if !known_fingerprints.is_empty() {
        if let Ok(Some(existing)) = load::<M>(config) {
            let mut existing_sorted = existing.known_fingerprints.clone();
            existing_sorted.sort();
            if existing_sorted == known_fingerprints {
                return Ok(config.json_path());
            }
        }
    }

    let baseline = Baseline {
        created_at: utc_now_iso8601(),
        context_id: context_id.to_string(),
        item_count: items.len(),
        known_fingerprints,
        metadata,
    };

    let baseline_value = serde_json::to_value(&baseline).map_err(|error| {
        Error::internal_io(
            format!("Failed to serialize baseline: {}", error),
            Some("baseline.save".to_string()),
        )
    })?;

    let json_path = config.json_path();
    let mut root = read_json_or_empty(&json_path)?;

    let baselines = root
        .as_object_mut()
        .ok_or_else(|| {
            Error::internal_io(
                "homeboy.json root is not an object".to_string(),
                Some("baseline.save".to_string()),
            )
        })?
        .entry(BASELINES_KEY)
        .or_insert_with(|| Value::Object(serde_json::Map::new()));

    baselines
        .as_object_mut()
        .ok_or_else(|| {
            Error::internal_io(
                "baselines key is not an object".to_string(),
                Some("baseline.save".to_string()),
            )
        })?
        .insert(config.key.clone(), baseline_value);

    write_json(&json_path, &root)?;

    Ok(json_path)
}

pub fn save_scoped<M: Serialize + for<'de> Deserialize<'de> + Clone>(
    config: &BaselineConfig,
    context_id: &str,
    current_items: &[impl Fingerprintable],
    metadata: M,
    scope: &[String],
    file_from_fingerprint: impl Fn(&str) -> Option<String>,
) -> Result<PathBuf> {
    let json_path = config.json_path();
    let existing: Option<Baseline<M>> = load(config)?;
    let Some(existing) = existing else {
        return save(config, context_id, current_items, metadata);
    };

    let scope_set: HashSet<&str> = scope.iter().map(|value| value.as_str()).collect();
    let existing_fingerprints_snapshot = existing.known_fingerprints.clone();

    let mut merged_fingerprints: Vec<String> = existing
        .known_fingerprints
        .into_iter()
        .filter(|fingerprint| {
            file_from_fingerprint(fingerprint)
                .as_deref()
                .is_none_or(|file| !scope_set.contains(file))
        })
        .collect();

    for item in current_items {
        merged_fingerprints.push(item.fingerprint());
    }

    merged_fingerprints.sort();
    merged_fingerprints.dedup();

    let mut existing_sorted = existing_fingerprints_snapshot.clone();
    existing_sorted.sort();
    if existing_sorted == merged_fingerprints {
        return Ok(json_path);
    }

    let baseline = Baseline {
        created_at: utc_now_iso8601(),
        context_id: context_id.to_string(),
        item_count: merged_fingerprints.len(),
        known_fingerprints: merged_fingerprints,
        metadata,
    };

    let baseline_value = serde_json::to_value(&baseline).map_err(|error| {
        Error::internal_io(
            format!("Failed to serialize scoped baseline: {}", error),
            Some("baseline.save_scoped".to_string()),
        )
    })?;

    let mut root = read_json_or_empty(&json_path)?;
    let baselines = root
        .as_object_mut()
        .ok_or_else(|| {
            Error::internal_io(
                "homeboy.json root is not an object".to_string(),
                Some("baseline.save_scoped".to_string()),
            )
        })?
        .entry(BASELINES_KEY)
        .or_insert_with(|| Value::Object(serde_json::Map::new()));

    baselines
        .as_object_mut()
        .ok_or_else(|| {
            Error::internal_io(
                "baselines key is not an object".to_string(),
                Some("baseline.save_scoped".to_string()),
            )
        })?
        .insert(config.key.clone(), baseline_value);

    write_json(&json_path, &root)?;

    Ok(json_path)
}

pub fn load<M: for<'de> Deserialize<'de> + Serialize>(
    config: &BaselineConfig,
) -> Result<Option<Baseline<M>>> {
    let path = config.json_path();
    if !path.exists() {
        return Ok(None);
    }

    let root = read_json_or_empty(&path)?;
    let baseline_value = root
        .get(BASELINES_KEY)
        .and_then(|baselines| baselines.get(config.key()))
        .cloned();

    let Some(baseline_value) = baseline_value else {
        return Ok(None);
    };

    let baseline = serde_json::from_value(baseline_value).map_err(|error| {
        Error::internal_io(
            format!(
                "Failed to deserialize baseline '{}': {}",
                config.key(),
                error
            ),
            Some("baseline.load".to_string()),
        )
    })?;

    Ok(Some(baseline))
}

pub fn compare<T: Fingerprintable, M: Serialize>(
    current_items: &[T],
    baseline: &Baseline<M>,
) -> Comparison {
    let baseline_set: HashSet<&String> = baseline.known_fingerprints.iter().collect();
    let current_fingerprints: Vec<String> = current_items
        .iter()
        .map(|item| item.fingerprint())
        .collect();
    let current_set: HashSet<&String> = current_fingerprints.iter().collect();

    let new_items = current_items
        .iter()
        .filter(|item| {
            let fingerprint = item.fingerprint();
            !baseline_set.contains(&fingerprint)
        })
        .map(|item| NewItem {
            fingerprint: item.fingerprint(),
            description: item.description(),
            context_label: item.context_label(),
        })
        .collect::<Vec<_>>();

    let resolved_fingerprints = baseline
        .known_fingerprints
        .iter()
        .filter(|fingerprint| !current_set.contains(fingerprint))
        .cloned()
        .collect::<Vec<_>>();

    let delta = current_items.len() as i64 - baseline.item_count as i64;

    Comparison {
        drift_increased: !new_items.is_empty(),
        new_items,
        resolved_fingerprints,
        delta,
    }
}

fn read_json_or_empty(path: &Path) -> Result<Value> {
    if !path.exists() {
        return Ok(Value::Object(serde_json::Map::new()));
    }

    let content = std::fs::read_to_string(path).map_err(|error| {
        Error::internal_io(
            format!("Failed to read {}: {}", path.display(), error),
            Some("baseline.read_json".to_string()),
        )
    })?;

    if content.trim().is_empty() {
        return Ok(Value::Object(serde_json::Map::new()));
    }

    serde_json::from_str(&content).map_err(|error| {
        Error::internal_io(
            format!("Failed to parse {}: {}", path.display(), error),
            Some("baseline.read_json".to_string()),
        )
    })
}

fn write_json(path: &Path, value: &Value) -> Result<()> {
    let content = serde_json::to_string_pretty(value).map_err(|error| {
        Error::internal_io(
            format!("Failed to serialize {}: {}", path.display(), error),
            Some("baseline.write_json".to_string()),
        )
    })?;

    std::fs::write(path, content).map_err(|error| {
        Error::internal_io(
            format!("Failed to write {}: {}", path.display(), error),
            Some("baseline.write_json".to_string()),
        )
    })
}

pub fn load_from_git_ref<M: for<'de> Deserialize<'de> + Serialize>(
    source_path: &str,
    git_ref: &str,
    key: &str,
) -> Option<Baseline<M>> {
    let git_spec = format!("{}:{}", git_ref, HOMEBOY_JSON);
    let content =
        crate::engine::command::run_in_optional(source_path, "git", &["show", &git_spec])?;

    let root: Value = serde_json::from_str(&content).ok()?;
    let value = root.get(BASELINES_KEY)?.get(key)?;
    serde_json::from_value::<Baseline<M>>(value.clone()).ok()
}

fn utc_now_iso8601() -> String {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    let secs_per_day = 86400u64;
    let secs_per_hour = 3600u64;
    let secs_per_min = 60u64;

    let days = now / secs_per_day;
    let remaining = now % secs_per_day;
    let hours = remaining / secs_per_hour;
    let remaining = remaining % secs_per_hour;
    let minutes = remaining / secs_per_min;
    let seconds = remaining % secs_per_min;

    let (year, month, day) = days_to_date(days);

    format!(
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
        year, month, day, hours, minutes, seconds
    )
}

fn days_to_date(mut days: u64) -> (u64, u64, u64) {
    let mut year = 1970u64;

    loop {
        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
        if days < days_in_year {
            break;
        }
        days -= days_in_year;
        year += 1;
    }

    let leap = is_leap_year(year);
    let month_days = [
        31,
        if leap { 29 } else { 28 },
        31,
        30,
        31,
        30,
        31,
        31,
        30,
        31,
        30,
        31,
    ];

    let mut month = 1u64;
    for &month_days in &month_days {
        if days < month_days {
            break;
        }
        days -= month_days;
        month += 1;
    }

    (year, month, days + 1)
}

fn is_leap_year(year: u64) -> bool {
    (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400)
}