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
mod app;
mod core;
mod event;
mod tui;
mod ui;
use anyhow::Result;
use app::{App, Field, Mode, Modal};
use core::agent::{self, AgentClient};
use core::executor;
use core::init::Locker;
use core::store::SecretsStore;
use crossterm::event::{Event, KeyCode};
use zeroize::Zeroize;
fn main() -> Result<()> {
let args: Vec<String> = std::env::args().collect();
// Mode CLI
if args.len() >= 2 {
match args[1].as_str() {
"run" if args.len() >= 3 => return run_with_secrets(&args[2..]),
"agent" => return run_agent_mode(&args[2..]),
"status" => return show_status(),
"stop" => return stop_agent(),
"help" | "--help" | "-h" => {
print_help();
return Ok(());
}
_ => {}
}
}
// Mode TUI standard
run_tui()
}
fn print_help() {
println!("lazy-locker - Secure secrets manager");
println!();
println!("USAGE:");
println!(" lazy-locker Opens the TUI interface");
println!(" lazy-locker run <cmd> Executes a command with injected secrets");
println!(" lazy-locker status Shows agent status");
println!(" lazy-locker stop Stops the agent");
println!();
println!("EXAMPLES:");
println!(" lazy-locker run python script.py");
println!(" lazy-locker run uv run app.py");
println!(" lazy-locker run bun run index.ts");
}
/// Agent mode (called by the daemon)
fn run_agent_mode(args: &[String]) -> Result<()> {
let mut key_hex = String::new();
let mut store_path = String::new();
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"--key" if i + 1 < args.len() => {
key_hex = args[i + 1].clone();
i += 2;
}
"--store" if i + 1 < args.len() => {
store_path = args[i + 1].clone();
i += 2;
}
_ => i += 1,
}
}
if key_hex.is_empty() || store_path.is_empty() {
return Err(anyhow::anyhow!("Usage: lazy-locker agent --key <key_hex> --store <path>"));
}
agent::run_agent(&key_hex, &store_path)
}
/// Shows agent status
fn show_status() -> Result<()> {
match AgentClient::status() {
Ok(data) => {
println!("✅ Agent active");
if let Some(uptime) = data.get("uptime_secs").and_then(|v| v.as_u64()) {
let hours = uptime / 3600;
let mins = (uptime % 3600) / 60;
println!(" Uptime: {}h {:02}m", hours, mins);
}
if let Some(remaining) = data.get("ttl_remaining_secs").and_then(|v| v.as_u64()) {
let hours = remaining / 3600;
let mins = (remaining % 3600) / 60;
println!(" TTL remaining: {}h {:02}m", hours, mins);
}
}
Err(_) => {
println!("❌ Agent not started");
println!(" Run lazy-locker to start the agent");
}
}
Ok(())
}
/// Stops the agent
fn stop_agent() -> Result<()> {
let socket_path = agent::get_socket_path()?;
if socket_path.exists() {
use std::io::{BufRead, BufReader, Write};
use std::os::unix::net::UnixStream;
if let Ok(mut stream) = UnixStream::connect(&socket_path) {
writeln!(stream, r#"{{"action":"shutdown"}}"#)?;
stream.flush()?;
let mut reader = BufReader::new(&stream);
let mut response = String::new();
reader.read_line(&mut response)?;
println!("✅ Agent stopped");
}
} else {
println!("ℹ️ Agent not started");
}
Ok(())
}
/// Executes a command with secrets injected as environment variables
fn run_with_secrets(command_args: &[String]) -> Result<()> {
// First, try via the agent (no passphrase needed)
if agent::is_agent_running() {
let secrets = AgentClient::get_secrets()?;
// Exécuter la commande avec les secrets
use std::process::{Command, Stdio};
let command = command_args.join(" ");
let output = Command::new("sh")
.arg("-c")
.arg(&command)
.envs(&secrets)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.status()?;
if !output.success() {
std::process::exit(output.code().unwrap_or(1));
}
return Ok(());
}
// Fallback: ask for passphrase
use std::io::Write;
print!("Passphrase: ");
std::io::stdout().flush()?;
let passphrase = rpassword::read_password()?;
let locker = Locker::init_or_load_with_passphrase(&passphrase)?;
let key = locker.get_key()
.ok_or_else(|| anyhow::anyhow!("Error loading key"))?;
let store = SecretsStore::load(locker.base_dir(), key)?;
let command = command_args.join(" ");
let output = executor::execute_with_secrets(&command, &store, key)?;
std::io::stdout().write_all(&output.stdout)?;
std::io::stderr().write_all(&output.stderr)?;
if !output.status.success() {
std::process::exit(output.status.code().unwrap_or(1));
}
Ok(())
}
fn run_tui() -> Result<()> {
let mut terminal = tui::init()?;
let mut app = App::new();
let mut locker: Option<Locker> = None;
let work_dir = std::env::current_dir()?;
// First, check if the agent is already active (no passphrase needed)
if agent::is_agent_running() {
match AgentClient::get_secrets() {
Ok(secrets) => {
// Agent is active with secrets - use agent mode
app.initialized = true;
app.mode = Mode::Normal;
app.agent_mode = true;
app.agent_secrets = Some(secrets.clone());
app.set_status(format!("✅ Agent active ({} secrets)", secrets.len()));
}
Err(_) => {
// Agent running but can't get secrets, fall back to passphrase
app.enter_init_mode();
}
}
} else {
// No agent running, need passphrase
app.enter_init_mode();
}
// Update usages at startup
app.update_token_usages(&work_dir);
loop {
terminal.draw(|frame| ui::render(&app, frame))?;
// Use 100ms poll timeout for better compatibility with various terminals (e.g., Ghostty)
if event::poll(std::time::Duration::from_millis(100))? {
if let Event::Key(key) = event::read()? {
// Clear status message on any key press
app.clear_status();
let prev_selected = app.selected_index;
// Handle special actions before general key handling
let handled = match (&app.mode, &app.modal, key.code) {
// Passphrase validation
(Mode::InitPassphrase, _, KeyCode::Enter) => {
let passphrase_str = String::from_utf8_lossy(&app.passphrase);
match Locker::init_or_load_with_passphrase(&passphrase_str) {
Ok(l) => {
locker = Some(l);
app.initialized = true;
app.mode = Mode::Normal;
if let Some(ref l) = locker {
if let Some(key) = l.get_key() {
let store = SecretsStore::load(l.base_dir(), key)?;
// Start agent in background if not already active
if !agent::is_agent_running() {
if let Err(e) = agent::start_daemon(key.to_vec(), store.clone()) {
app.set_status(format!("⚠️ Agent: {}", e));
} else {
app.set_status("✅ Agent started (8h)".to_string());
}
} else {
app.set_status("✅ Agent already active".to_string());
}
app.secrets_store = Some(store);
}
}
app.passphrase.zeroize();
app.update_token_usages(&work_dir);
}
Err(e) => app.set_error(e.to_string()),
}
true
}
// Add secret - only validate when on Expiration field and Enter pressed
(Mode::Normal, Modal::AddSecret, KeyCode::Enter) if app.current_field == Field::Expiration => {
if !app.new_secret_name.is_empty() && !app.new_secret_value.is_empty() {
let expiration_days = app.get_expiration_days();
let name = app.new_secret_name.clone();
let value = app.new_secret_value.clone();
if let Some(ref mut store) = app.secrets_store {
if let Some(ref l) = locker {
if let Some(key) = l.get_key() {
match store.add_secret(
name,
value,
expiration_days,
l.base_dir(),
key,
) {
Ok(_) => {
app.new_secret_name.clear();
app.new_secret_value.zeroize();
app.new_secret_expiration.clear();
app.close_modal();
app.set_status("✓ Secret added successfully".to_string());
app.update_token_usages(&work_dir);
}
Err(e) => app.set_error(e.to_string()),
}
}
}
}
}
true
}
// Delete confirmation
(Mode::Normal, Modal::DeleteConfirm, KeyCode::Char('y'))
| (Mode::Normal, Modal::DeleteConfirm, KeyCode::Enter) => {
if let Some(secret_name) = app.get_selected_secret_name() {
if let Some(ref mut store) = app.secrets_store {
if let Some(ref l) = locker {
if let Some(key) = l.get_key() {
match store.delete_secret(&secret_name, l.base_dir(), key) {
Ok(_) => {
let count = app.secrets_count();
if count > 0 && app.selected_index >= count {
app.selected_index = count - 1;
}
app.close_modal();
app.set_status("✓ Secret deleted".to_string());
app.update_token_usages(&work_dir);
}
Err(e) => app.set_error(e.to_string()),
}
}
}
}
}
true
}
// Reveal secret with 'e'
(Mode::Normal, Modal::None, KeyCode::Char('e')) => {
if let Some(secret_name) = app.get_selected_secret_name() {
if app.revealed_secret.is_some() {
if let Some(ref mut revealed) = app.revealed_secret {
revealed.zeroize();
}
app.revealed_secret = None;
} else {
if let Some(ref store) = app.secrets_store {
if let Some(ref l) = locker {
if let Some(key) = l.get_key() {
match store.decrypt_secret(&secret_name, key) {
Ok(decrypted) => {
app.revealed_secret = Some(decrypted);
}
Err(e) => app.set_error(e.to_string()),
}
}
}
}
}
}
true
}
// Copy to clipboard with 'y'
(Mode::Normal, Modal::None, KeyCode::Char('y')) => {
if let Some(secret_name) = app.get_selected_secret_name() {
if let Some(ref store) = app.secrets_store {
if let Some(ref l) = locker {
if let Some(key) = l.get_key() {
match store.decrypt_secret(&secret_name, key) {
Ok(mut decrypted) => {
match executor::copy_to_clipboard(&decrypted) {
Ok(_) => {
app.set_status(format!("✓ '{}' copied to clipboard", secret_name));
}
Err(e) => app.set_error(format!("Clipboard error: {}", e)),
}
decrypted.zeroize();
}
Err(e) => app.set_error(e.to_string()),
}
}
}
}
}
true
}
// Generate .env.ll reference file with 'r'
(Mode::Normal, Modal::None, KeyCode::Char('r')) => {
if let Some(ref store) = app.secrets_store {
let env_path = work_dir.join(".env.ll");
match executor::generate_env_reference(store, &env_path) {
Ok(_) => {
app.set_status(format!("✓ .env.ll file generated: {}", env_path.display()));
}
Err(e) => app.set_error(format!("Error generating .env.ll: {}", e)),
}
}
true
}
_ => false,
};
if !handled {
app.handle_key(key.code);
}
// Update usages if selection has changed
if app.selected_index != prev_selected {
app.update_token_usages(&work_dir);
}
}
}
if app.should_quit {
break;
}
}
tui::restore()?;
println!("Closing Lazy Locker.");
Ok(())
}