# Rhai Examples for herolib-os

This directory contains Rhai script examples demonstrating the functionality of each module in the herolib-os package.

## Directory Structure

```
rhaiexamples/
├── README.md           # This file
├── os/                 # OS module examples
│   ├── README.md       # OS function reference
│   ├── 01_filesystem.rhai
│   ├── 02_platform.rhai
│   ├── 03_commands_which.rhai
│   ├── 04_download.rhai
│   └── 05_package.rhai
├── process/            # Process module examples
│   ├── README.md       # Process function reference
│   ├── 01_run_commands.rhai
│   ├── 02_command_builder.rhai
│   └── 03_process_management.rhai
├── git/                # Git module examples
│   ├── README.md       # Git function reference
│   ├── 01_url_parsing.rhai
│   ├── 02_git_tree.rhai
│   ├── 03_git_repo.rhai
│   └── git_basic.rhai
├── net/                # Network module examples
│   ├── README.md       # Network function reference
│   ├── 01_tcp_check.rhai
│   ├── 02_http_check.rhai
│   ├── 04_advanced_port_scanning.rhai
│   ├── 05_http_health_checks.rhai
│   └── 06_network_diagnostics.rhai
├── ssh/                # SSH module examples
│   ├── README.md       # SSH function reference
│   ├── 03_ssh_builder_basics.rhai
│   ├── 08_ssh_fs.rhai
│   └── 09_ssh_practical.rhai
└── profiling/          # Profiling module examples
    ├── README.md       # Profiling function reference
    ├── 01_system_benchmark.rhai
    ├── 02_quick_check.rhai
    ├── 03_stress_test.rhai
    └── 04_report_generation.rhai
```

## Running Examples

Use the `hero-system` REPL to run these examples:

```bash
# Start the REPL
hero-system

# Load and run an example
hero> :load rhaiexamples/os/01_filesystem.rhai

# Or run directly from command line
hero-system rhaiexamples/os/01_filesystem.rhai
```

## Module Overview

### OS Module (`os/`)

Filesystem operations and platform detection:
- File/directory existence checks
- File reading and writing
- Directory creation and deletion
- Path manipulation
- Platform detection (OS, architecture)
- Command availability checking

### Process Module (`process/`)

Command execution and process management:
- `run(cmd)` - Simple command execution
- `cmd(cmd)` - Builder pattern for advanced control
- Process listing and management
- Environment variable access

### Git Module (`git/`)

Git repository management:
- URL parsing for GitHub, GitLab, Gitea
- Git tree management (organize multiple repos)
- Repository operations (clone, pull, push, commit)

### Profiling Module (`profiling/`)

System performance benchmarking and monitoring:
- Complete system benchmarking (CPU, memory, disk, network)
- Quick performance health checks
- Stress testing and thermal monitoring
- Report generation in multiple formats
- Resource utilization monitoring

## Quick Reference

### Most Common Functions

```rhai
// Execute a command and get output
let output = run("ls -la");

// Check if file exists
if exist("/path/to/file") {
    let content = file_read("/path/to/file");
}

// Get platform info
let os = os_name();           // "linux", "macos", "windows"
let arch = os_arch();         // "x86_64", "aarch64"

// Clone a git repository
let repo = git_clone("https://github.com/user/repo");

// Create a container
let container = nerdctl_container_from_image("myapp", "nginx:latest")
    .with_port("8080:80")
    .build();

// Run system benchmark
let profile = SystemProfile();
let cpu_score = measure_single_thread_performance().combined_score;
let mem_bandwidth = measure_memory_bandwidth().read_bandwidth_gbps;
print("CPU Score: " + cpu_score + ", Memory: " + mem_bandwidth + " GB/s");
```

## Notes

- Some examples contain commented-out code that performs actual operations
- Uncomment to run real commands (be careful with destructive operations)
- Each module's README contains a complete function reference
