kto 0.1.5

A generic, flexible web change watcher with AI-powered analysis
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Service management commands: install, uninstall, status, logs

use std::io::Write;
use std::path::PathBuf;
use std::process::{Command, Stdio};

use kto::error::Result;

/// Detect which service manager to use
#[derive(Debug, Clone, Copy, PartialEq)]
#[allow(dead_code)]
pub enum ServiceManager {
    Systemd,
    Launchd,
    Cron,
}

#[allow(dead_code)]
impl ServiceManager {
    pub fn detect() -> Option<Self> {
        #[cfg(target_os = "linux")]
        {
            // Check if systemd is available
            if Command::new("systemctl")
                .arg("--user")
                .arg("--version")
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false)
            {
                return Some(ServiceManager::Systemd);
            }
        }

        #[cfg(target_os = "macos")]
        {
            // launchd is always available on macOS
            return Some(ServiceManager::Launchd);
        }

        #[cfg(not(any(target_os = "linux", target_os = "macos")))]
        {
            None
        }

        #[cfg(any(target_os = "linux", target_os = "macos"))]
        None
    }

    pub fn name(&self) -> &'static str {
        match self {
            ServiceManager::Systemd => "systemd",
            ServiceManager::Launchd => "launchd",
            ServiceManager::Cron => "cron",
        }
    }
}

fn get_kto_binary_path() -> Result<String> {
    std::env::current_exe()
        .map(|p| p.to_string_lossy().to_string())
        .map_err(|e| kto::KtoError::ConfigError(format!("Could not determine kto path: {}", e)))
}

fn systemd_service_path() -> PathBuf {
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    PathBuf::from(home)
        .join(".config")
        .join("systemd")
        .join("user")
        .join("kto.service")
}

fn launchd_plist_path() -> PathBuf {
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    PathBuf::from(home)
        .join("Library")
        .join("LaunchAgents")
        .join("com.kto.daemon.plist")
}

fn generate_systemd_service(kto_path: &str) -> String {
    format!(r#"[Unit]
Description=kto web change watcher daemon
After=network.target

[Service]
Type=simple
ExecStart={kto_path} daemon
Restart=on-failure
RestartSec=10

[Install]
WantedBy=default.target
"#)
}

fn generate_launchd_plist(kto_path: &str) -> String {
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    let log_path = format!("{}/Library/Logs/kto.log", home);
    let err_path = format!("{}/Library/Logs/kto.error.log", home);

    format!(r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.kto.daemon</string>
    <key>ProgramArguments</key>
    <array>
        <string>{kto_path}</string>
        <string>daemon</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>{log_path}</string>
    <key>StandardErrorPath</key>
    <string>{err_path}</string>
</dict>
</plist>
"#)
}

fn install_systemd_service() -> Result<()> {
    let kto_path = get_kto_binary_path()?;
    let service_path = systemd_service_path();

    // Create directory if needed
    if let Some(parent) = service_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    // Write service file
    let service_content = generate_systemd_service(&kto_path);
    std::fs::write(&service_path, service_content)?;

    println!("  Created {}", service_path.display());

    // Reload systemd
    let reload = Command::new("systemctl")
        .args(["--user", "daemon-reload"])
        .status()?;

    if !reload.success() {
        return Err(kto::KtoError::ConfigError("Failed to reload systemd".into()));
    }

    // Enable and start service
    let enable = Command::new("systemctl")
        .args(["--user", "enable", "kto.service"])
        .status()?;

    if !enable.success() {
        return Err(kto::KtoError::ConfigError("Failed to enable service".into()));
    }

    let start = Command::new("systemctl")
        .args(["--user", "start", "kto.service"])
        .status()?;

    if !start.success() {
        return Err(kto::KtoError::ConfigError("Failed to start service".into()));
    }

    println!("  Service enabled and started");
    println!("\n  Commands:");
    println!("    systemctl --user status kto");
    println!("    systemctl --user stop kto");
    println!("    systemctl --user restart kto");
    println!("    journalctl --user -u kto -f");

    Ok(())
}

fn install_launchd_service() -> Result<()> {
    let kto_path = get_kto_binary_path()?;
    let plist_path = launchd_plist_path();

    // Create directory if needed
    if let Some(parent) = plist_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    // Write plist file
    let plist_content = generate_launchd_plist(&kto_path);
    std::fs::write(&plist_path, plist_content)?;

    println!("  Created {}", plist_path.display());

    // Load the service
    let load = Command::new("launchctl")
        .args(["load", "-w", &plist_path.to_string_lossy()])
        .status()?;

    if !load.success() {
        return Err(kto::KtoError::ConfigError("Failed to load service".into()));
    }

    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    println!("  Service loaded and started");
    println!("\n  Commands:");
    println!("    launchctl list | grep kto");
    println!("    launchctl unload ~/Library/LaunchAgents/com.kto.daemon.plist");
    println!("    tail -f ~/Library/Logs/kto.log");
    println!("\n  Logs: {}/Library/Logs/kto.log", home);

    Ok(())
}

fn install_cron_service(interval_mins: u32) -> Result<()> {
    let kto_path = get_kto_binary_path()?;

    // Get current crontab
    let current = Command::new("crontab")
        .arg("-l")
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).to_string())
        .unwrap_or_default();

    // Check if kto is already in crontab and filter out existing entries
    let current = if current.contains("kto run") {
        println!("  kto is already in crontab. Updating...");
        current
            .lines()
            .filter(|line| !line.contains("kto run"))
            .collect::<Vec<&str>>()
            .join("\n")
    } else {
        current
    };

    // Add new cron entry
    let cron_entry = format!("*/{} * * * * {} run >> /tmp/kto-cron.log 2>&1", interval_mins, kto_path);
    let new_crontab = if current.is_empty() {
        cron_entry
    } else {
        format!("{}\n{}", current.trim(), cron_entry)
    };

    // Install new crontab
    let mut child = Command::new("crontab")
        .arg("-")
        .stdin(Stdio::piped())
        .spawn()?;

    if let Some(mut stdin) = child.stdin.take() {
        stdin.write_all(new_crontab.as_bytes())?;
    }

    let status = child.wait()?;
    if !status.success() {
        return Err(kto::KtoError::ConfigError("Failed to install crontab".into()));
    }

    println!("  Added to crontab: run every {} minutes", interval_mins);
    println!("\n  Commands:");
    println!("    crontab -l              # View crontab");
    println!("    crontab -e              # Edit crontab");
    println!("    tail -f /tmp/kto-cron.log  # View logs");

    Ok(())
}

/// Install kto as a background service
pub fn cmd_service_install(use_cron: bool, cron_interval: u32) -> Result<()> {
    println!("\nInstalling kto service...\n");

    if use_cron {
        install_cron_service(cron_interval)?;
    } else {
        match ServiceManager::detect() {
            Some(ServiceManager::Systemd) => {
                println!("  Detected: systemd (Linux)");
                install_systemd_service()?;
            }
            Some(ServiceManager::Launchd) => {
                println!("  Detected: launchd (macOS)");
                install_launchd_service()?;
            }
            _ => {
                println!("  No native service manager detected.");
                println!("  Installing via cron instead...\n");
                install_cron_service(cron_interval)?;
            }
        }
    }

    println!("\n  kto is now running in the background!");
    println!("  Use `kto service status` to check status.");

    Ok(())
}

/// Uninstall the background service
pub fn cmd_service_uninstall() -> Result<()> {
    println!("\nUninstalling kto service...\n");

    let mut uninstalled = false;

    // Try systemd
    let systemd_path = systemd_service_path();
    if systemd_path.exists() {
        println!("  Stopping systemd service...");
        let _ = Command::new("systemctl")
            .args(["--user", "stop", "kto.service"])
            .status();
        let _ = Command::new("systemctl")
            .args(["--user", "disable", "kto.service"])
            .status();
        std::fs::remove_file(&systemd_path)?;
        let _ = Command::new("systemctl")
            .args(["--user", "daemon-reload"])
            .status();
        println!("  Removed systemd service");
        uninstalled = true;
    }

    // Try launchd
    let launchd_path = launchd_plist_path();
    if launchd_path.exists() {
        println!("  Unloading launchd service...");
        let _ = Command::new("launchctl")
            .args(["unload", "-w", &launchd_path.to_string_lossy()])
            .status();
        std::fs::remove_file(&launchd_path)?;
        println!("  Removed launchd service");
        uninstalled = true;
    }

    // Try cron
    if let Ok(output) = Command::new("crontab").arg("-l").output() {
        if output.status.success() {
            let current = String::from_utf8_lossy(&output.stdout);
            if current.contains("kto run") {
                println!("  Removing from crontab...");
                let filtered: Vec<&str> = current
                    .lines()
                    .filter(|line| !line.contains("kto run"))
                    .collect();
                let new_crontab = filtered.join("\n");

                let mut child = Command::new("crontab")
                    .arg("-")
                    .stdin(Stdio::piped())
                    .spawn()?;

                if let Some(mut stdin) = child.stdin.take() {
                    stdin.write_all(new_crontab.as_bytes())?;
                }

                let _ = child.wait();
                println!("  Removed from crontab");
                uninstalled = true;
            }
        }
    }

    if uninstalled {
        println!("\n  kto service uninstalled.");
    } else {
        println!("  No kto service installation found.");
    }

    Ok(())
}

/// Show service status
pub fn cmd_service_status() -> Result<()> {
    println!("\nkto service status:\n");

    let mut found = false;

    // Check systemd
    #[cfg(target_os = "linux")]
    {
        if systemd_service_path().exists() {
            found = true;
            println!("  Type: systemd");
            let output = Command::new("systemctl")
                .args(["--user", "status", "kto.service", "--no-pager"])
                .output()?;
            println!("{}", String::from_utf8_lossy(&output.stdout));
        }
    }

    // Check launchd
    #[cfg(target_os = "macos")]
    {
        if launchd_plist_path().exists() {
            found = true;
            println!("  Type: launchd");
            let output = Command::new("launchctl")
                .args(["list"])
                .output()?;
            let list = String::from_utf8_lossy(&output.stdout);
            for line in list.lines() {
                if line.contains("com.kto.daemon") {
                    println!("  {}", line);
                }
            }

            // Check if actually running
            if list.contains("com.kto.daemon") {
                println!("  Status: Running");
            } else {
                println!("  Status: Not running (plist exists but not loaded)");
            }
        }
    }

    // Check cron
    if let Ok(output) = Command::new("crontab").arg("-l").output() {
        if output.status.success() {
            let current = String::from_utf8_lossy(&output.stdout);
            for line in current.lines() {
                if line.contains("kto run") {
                    found = true;
                    println!("  Type: cron");
                    println!("  Entry: {}", line);
                }
            }
        }
    }

    if !found {
        println!("  No kto service installed.");
        println!("  Run `kto service install` to set up background monitoring.");
    }

    Ok(())
}

/// Show service logs
pub fn cmd_service_logs(lines: usize, follow: bool) -> Result<()> {
    // Determine which service is installed and show appropriate logs

    // Check systemd first
    #[cfg(target_os = "linux")]
    {
        if systemd_service_path().exists() {
            let mut cmd = Command::new("journalctl");
            cmd.args(["--user", "-u", "kto.service", "-n", &lines.to_string()]);
            if follow {
                cmd.arg("-f");
            }
            cmd.status()?;
            return Ok(());
        }
    }

    // Check launchd
    #[cfg(target_os = "macos")]
    {
        if launchd_plist_path().exists() {
            let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
            let log_path = format!("{}/Library/Logs/kto.log", home);

            if std::path::Path::new(&log_path).exists() {
                let mut cmd = if follow {
                    let mut c = Command::new("tail");
                    c.args(["-f", "-n", &lines.to_string(), &log_path]);
                    c
                } else {
                    let mut c = Command::new("tail");
                    c.args(["-n", &lines.to_string(), &log_path]);
                    c
                };
                cmd.status()?;
                return Ok(());
            } else {
                println!("Log file not found: {}", log_path);
                println!("The service may not have started yet.");
                return Ok(());
            }
        }
    }

    // Check cron logs
    let cron_log = "/tmp/kto-cron.log";
    if std::path::Path::new(cron_log).exists() {
        let mut cmd = if follow {
            let mut c = Command::new("tail");
            c.args(["-f", "-n", &lines.to_string(), cron_log]);
            c
        } else {
            let mut c = Command::new("tail");
            c.args(["-n", &lines.to_string(), cron_log]);
            c
        };
        cmd.status()?;
        return Ok(());
    }

    println!("No service logs found.");
    println!("Run `kto service install` to set up background monitoring.");

    Ok(())
}