# 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.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
└── virt/               # Virt module examples
    ├── README.md       # Virt function reference
    ├── 01_buildah.rhai
    ├── 02_nerdctl.rhai
    └── 03_rfs.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)

### Virt Module (`virt/`)

Virtualization and containerization:
- **Buildah**: OCI container image building
- **Nerdctl**: Container lifecycle management
- **RFS**: Remote filesystem mounting and layer management

## 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();
```

## 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
