herolib-os
Unified system utilities for Rust and Rhai scripting, providing OS operations, process management, git, and network utilities.
Documentation
Features
- OS Module - Filesystem operations, platform detection, package management, file downloads
- Process Module - Command execution with builder pattern, process management
- Git Module - Repository management, URL parsing, git tree organization
- Network Module - TCP connectivity, HTTP/HTTPS operations, SSH support with key management and file transfers
- Rhai Integration - Full scripting support for all modules
- Interactive REPL -
hero-osbinary for testing and scripting
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Feature Flags
| Feature | Description |
|---|---|
rhai |
Rhai scripting support (default) |
repl |
Interactive REPL binary |
full |
All features enabled |
# With REPL support
= { = "0.1.0", = ["repl"] }
# All features
= { = "0.1.0", = ["full"] }
Quick Start
Rust API
use ;
use run;
Rhai Scripting
// Execute commands
let output = run("ls -la");
print(output);
// File operations
if exist("/tmp/myfile.txt") {
let content = file_read("/tmp/myfile.txt");
print(content);
}
// Platform detection
print("OS: " + os_name());
print("Arch: " + os_arch());
// TCP and HTTP connectivity checks
let is_open = net_tcp_check("example.com", 443);
let is_reachable = net_http_check("https://example.com");
// Network operations
let is_pingable = net_tcp_ping("example.com");
Interactive REPL
Build and run the interactive REPL:
REPL features:
- Tab completion for all functions
- Syntax highlighting
- Command history
- Script loading with
:load filename.rhai - Scope inspection with
:scope
╔═══════════════════════════════════════════════════════════════════╗
║ HERO-OS ║
║ System Utilities REPL v0.1.0 ║
╚═══════════════════════════════════════════════════════════════════╝
Type :help for available commands, :quit to exit
hero> run("uname -a")
Darwin myhost 24.6.0 Darwin Kernel Version 24.6.0 ...
hero> os_name()
macos
hero> /load rhaiexamples/os/01_filesystem.rhai
Module Reference
OS Module
| Function | Description |
|---|---|
exist(path) |
Check if file/directory exists |
file_read(path) |
Read file contents |
file_write(path, content) |
Write content to file |
file_size(path) |
Get file size in bytes |
delete(path) |
Delete file or directory |
mkdir(path) |
Create directory |
copy(src, dest) |
Copy file or directory |
mv(src, dest) |
Move/rename file or directory |
os_name() |
Get OS name (linux, macos, windows) |
os_arch() |
Get architecture (x86_64, aarch64) |
which(cmd) |
Find command in PATH |
env_get(name) |
Get environment variable |
env_set(name, value) |
Set environment variable |
Process Module
| Function | Description |
|---|---|
run(cmd) |
Execute command, return stdout |
cmd(cmd) |
Create command builder |
process_list() |
List running processes |
process_get(pid) |
Get process by PID |
process_kill(pid) |
Kill process by PID |
Command Builder Methods:
cmd("curl")
.arg("-s") // Add argument
.args(["-H", "Accept: application/json"])
.env("TOKEN", "xxx") // Set environment variable
.cwd("/tmp") // Set working directory
.silent() // Suppress output
.ignore_error() // Don't fail on non-zero exit
.execute() // Run and return CommandResult
Git Module
| Function | Description |
|---|---|
parse_git_url_extended(url) |
Parse git URL into components |
git_tree_new(path) |
Create git tree manager |
GitTree Methods:
let tree = git_tree_new("/repos");
tree.list() // List all repositories
tree.find("*pattern*") // Find matching repos
tree.clone_repo(url) // Clone repository
tree.get_path(url) // Clone and return path
tree.get_path_pull(url) // Clone, pull, return path
GitRepo Methods:
repo.path() // Get local path
repo.has_changes() // Check for uncommitted changes
repo.pull() // Pull from remote
repo.reset() // Reset local changes
repo.commit(msg) // Commit changes
repo.push() // Push to remote
Network Module
| Function | Description |
|---|---|
net_tcp_check(host, port) |
Check if a TCP port is open |
net_tcp_ping(host) |
Check if a host is reachable |
net_http_check(url) |
Check if a URL is reachable |
net_http_status(url) |
Get HTTP status code for a URL |
SSH Connection:
let conn = SshConnection::new()
.host("example.com")
.user("ubuntu")
.port(22)
.connect();
let (code, output) = conn.execute("whoami");
SSH Key Management:
let keygen = SshKeyGenerator::new("/tmp/my_key")
.key_type(Ed25519);
keygen.generate();
let auth_mgr = AuthorizedKeysManager::new(conn);
auth_mgr.add_key("/path/to/id_rsa.pub");
File Transfer (SSH/Rsync):
let transfer = FileTransferOps::new(conn);
transfer.upload("/local/file", "/remote/path");
transfer.download("/remote/file", "/local/path");
Examples
See the rhaiexamples/ directory for comprehensive examples:
rhaiexamples/
├── os/ # Filesystem, platform detection, package management
├── process/ # Command execution and process management
├── git/ # Repository management and URL handling
├── net/ # TCP, HTTP, and network connectivity
└── ssh/ # SSH connections and file transfer
Run examples with the REPL:
Building
# Build with REPL support
# Build with all features
# Run tests
# Generate documentation
Requirements
- Rust 1.92.0 or later
- Edition 2024
Optional Runtime Dependencies
- Git - For repository operations and cloning
- SSH/OpenSSH - For SSH operations and file transfers
- curl - For HTTP operations (recommended)
- rsync - For efficient SSH file transfers (recommended)
License
Apache-2.0