sal-os 0.1.2

SAL OS - Operating system interaction utilities with cross-platform abstraction
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
//! Rhai wrappers for OS module functions
//!
//! This module provides Rhai wrappers for the functions in the OS module.

use crate::package::PackHero;
use crate::{download as dl, fs, package};
use rhai::{Array, Engine, EvalAltResult, Position};

/// A trait for converting a Result to a Rhai-compatible error
pub trait ToRhaiError<T> {
    fn to_rhai_error(self) -> Result<T, Box<EvalAltResult>>;
}

impl<T, E: std::error::Error> ToRhaiError<T> for Result<T, E> {
    fn to_rhai_error(self) -> Result<T, Box<EvalAltResult>> {
        self.map_err(|e| {
            Box::new(EvalAltResult::ErrorRuntime(
                e.to_string().into(),
                Position::NONE,
            ))
        })
    }
}

/// Register OS module functions with the Rhai engine
///
/// # Arguments
///
/// * `engine` - The Rhai engine to register the functions with
///
/// # Returns
///
/// * `Result<(), Box<EvalAltResult>>` - Ok if registration was successful, Err otherwise
pub fn register_os_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
    // Register file system functions
    engine.register_fn("copy", copy);
    engine.register_fn("copy_bin", copy_bin);
    engine.register_fn("exist", exist);
    engine.register_fn("find_file", find_file);
    engine.register_fn("find_files", find_files);
    engine.register_fn("find_dir", find_dir);
    engine.register_fn("find_dirs", find_dirs);
    engine.register_fn("delete", delete);
    engine.register_fn("mkdir", mkdir);
    engine.register_fn("file_size", file_size);
    engine.register_fn("rsync", rsync);
    engine.register_fn("chdir", chdir);
    engine.register_fn("file_read", file_read);
    engine.register_fn("file_write", file_write);
    engine.register_fn("file_write_append", file_write_append);

    // Register command check functions
    engine.register_fn("which", which);
    engine.register_fn("cmd_ensure_exists", cmd_ensure_exists);

    // Register download functions
    engine.register_fn("download", download);
    engine.register_fn("download_file", download_file);
    engine.register_fn("download_install", download_install);
    engine.register_fn("chmod_exec", chmod_exec);

    // Register move function
    engine.register_fn("mv", mv);

    // Register package management functions
    engine.register_fn("package_install", package_install);
    engine.register_fn("package_remove", package_remove);
    engine.register_fn("package_update", package_update);
    engine.register_fn("package_upgrade", package_upgrade);
    engine.register_fn("package_list", package_list);
    engine.register_fn("package_search", package_search);
    engine.register_fn("package_is_installed", package_is_installed);
    engine.register_fn("package_set_debug", package_set_debug);
    engine.register_fn("package_platform", package_platform);

    // Register platform detection functions
    engine.register_fn("platform_is_osx", platform_is_osx);
    engine.register_fn("platform_is_linux", platform_is_linux);
    engine.register_fn("platform_is_arm", platform_is_arm);
    engine.register_fn("platform_is_x86", platform_is_x86);
    engine.register_fn("platform_check_linux_x86", platform_check_linux_x86);
    engine.register_fn("platform_check_macos_arm", platform_check_macos_arm);

    Ok(())
}

//
// File System Function Wrappers
//

/// Wrapper for fs::copy
///
/// Recursively copy a file or directory from source to destination.
pub fn copy(src: &str, dest: &str) -> Result<String, Box<EvalAltResult>> {
    fs::copy(src, dest).to_rhai_error()
}

/// Wrapper for fs::copy_bin
///
/// Copy a binary to the correct location based on OS and user privileges.
pub fn copy_bin(src: &str) -> Result<String, Box<EvalAltResult>> {
    fs::copy_bin(src).to_rhai_error()
}

/// Wrapper for fs::exist
///
/// Check if a file or directory exists.
pub fn exist(path: &str) -> bool {
    fs::exist(path)
}

/// Wrapper for fs::find_file
///
/// Find a file in a directory (with support for wildcards).
pub fn find_file(dir: &str, filename: &str) -> Result<String, Box<EvalAltResult>> {
    fs::find_file(dir, filename).to_rhai_error()
}

/// Wrapper for fs::find_files
///
/// Find multiple files in a directory (recursive, with support for wildcards).
pub fn find_files(dir: &str, filename: &str) -> Result<Array, Box<EvalAltResult>> {
    let files = fs::find_files(dir, filename).to_rhai_error()?;

    // Convert Vec<String> to Rhai Array
    let mut array = Array::new();
    for file in files {
        array.push(file.into());
    }

    Ok(array)
}

/// Wrapper for fs::find_dir
///
/// Find a directory in a parent directory (with support for wildcards).
pub fn find_dir(dir: &str, dirname: &str) -> Result<String, Box<EvalAltResult>> {
    fs::find_dir(dir, dirname).to_rhai_error()
}

/// Wrapper for fs::find_dirs
///
/// Find multiple directories in a parent directory (recursive, with support for wildcards).
pub fn find_dirs(dir: &str, dirname: &str) -> Result<Array, Box<EvalAltResult>> {
    let dirs = fs::find_dirs(dir, dirname).to_rhai_error()?;

    // Convert Vec<String> to Rhai Array
    let mut array = Array::new();
    for dir in dirs {
        array.push(dir.into());
    }

    Ok(array)
}

/// Wrapper for fs::delete
///
/// Delete a file or directory (defensive - doesn't error if file doesn't exist).
pub fn delete(path: &str) -> Result<String, Box<EvalAltResult>> {
    fs::delete(path).to_rhai_error()
}

/// Wrapper for fs::mkdir
///
/// Create a directory and all parent directories (defensive - doesn't error if directory exists).
pub fn mkdir(path: &str) -> Result<String, Box<EvalAltResult>> {
    fs::mkdir(path).to_rhai_error()
}

/// Wrapper for fs::file_size
///
/// Get the size of a file in bytes.
pub fn file_size(path: &str) -> Result<i64, Box<EvalAltResult>> {
    fs::file_size(path).to_rhai_error()
}

/// Wrapper for fs::rsync
///
/// Sync directories using rsync (or platform equivalent).
pub fn rsync(src: &str, dest: &str) -> Result<String, Box<EvalAltResult>> {
    fs::rsync(src, dest).to_rhai_error()
}

/// Wrapper for fs::chdir
///
/// Change the current working directory.
pub fn chdir(path: &str) -> Result<String, Box<EvalAltResult>> {
    fs::chdir(path).to_rhai_error()
}

/// Wrapper for fs::file_read
///
/// Read the contents of a file.
pub fn file_read(path: &str) -> Result<String, Box<EvalAltResult>> {
    fs::file_read(path).to_rhai_error()
}

/// Wrapper for fs::file_write
///
/// Write content to a file (creates the file if it doesn't exist, overwrites if it does).
pub fn file_write(path: &str, content: &str) -> Result<String, Box<EvalAltResult>> {
    fs::file_write(path, content).to_rhai_error()
}

/// Wrapper for fs::file_write_append
///
/// Append content to a file (creates the file if it doesn't exist).
pub fn file_write_append(path: &str, content: &str) -> Result<String, Box<EvalAltResult>> {
    fs::file_write_append(path, content).to_rhai_error()
}

/// Wrapper for fs::mv
///
/// Move a file or directory from source to destination.
pub fn mv(src: &str, dest: &str) -> Result<String, Box<EvalAltResult>> {
    fs::mv(src, dest).to_rhai_error()
}

//
// Download Function Wrappers
//

/// Wrapper for os::download
///
/// Download a file from URL to destination using the curl command.
pub fn download(url: &str, dest: &str, min_size_kb: i64) -> Result<String, Box<EvalAltResult>> {
    dl::download(url, dest, min_size_kb).to_rhai_error()
}

/// Wrapper for os::download_file
///
/// Download a file from URL to a specific file destination using the curl command.
pub fn download_file(
    url: &str,
    dest: &str,
    min_size_kb: i64,
) -> Result<String, Box<EvalAltResult>> {
    dl::download_file(url, dest, min_size_kb).to_rhai_error()
}

/// Wrapper for os::download_install
///
/// Download a file and install it if it's a supported package format.
pub fn download_install(url: &str, min_size_kb: i64) -> Result<String, Box<EvalAltResult>> {
    dl::download_install(url, min_size_kb).to_rhai_error()
}

/// Wrapper for os::chmod_exec
///
/// Make a file executable (equivalent to chmod +x).
pub fn chmod_exec(path: &str) -> Result<String, Box<EvalAltResult>> {
    dl::chmod_exec(path).to_rhai_error()
}

/// Wrapper for os::which
///
/// Check if a command exists in the system PATH.
pub fn which(command: &str) -> String {
    fs::which(command)
}

/// Wrapper for os::cmd_ensure_exists
///
/// Ensure that one or more commands exist in the system PATH.
/// If any command doesn't exist, an error is thrown.
pub fn cmd_ensure_exists(commands: &str) -> Result<String, Box<EvalAltResult>> {
    fs::cmd_ensure_exists(commands).to_rhai_error()
}

//
// Package Management Function Wrappers
//

/// Wrapper for os::package::PackHero::install
///
/// Install a package using the system package manager.
pub fn package_install(package: &str) -> Result<String, Box<EvalAltResult>> {
    let hero = PackHero::new();
    hero.install(package)
        .map(|_| format!("Package '{}' installed successfully", package))
        .to_rhai_error()
}

/// Wrapper for os::package::PackHero::remove
///
/// Remove a package using the system package manager.
pub fn package_remove(package: &str) -> Result<String, Box<EvalAltResult>> {
    let hero = PackHero::new();
    hero.remove(package)
        .map(|_| format!("Package '{}' removed successfully", package))
        .to_rhai_error()
}

/// Wrapper for os::package::PackHero::update
///
/// Update package lists using the system package manager.
pub fn package_update() -> Result<String, Box<EvalAltResult>> {
    let hero = PackHero::new();
    hero.update()
        .map(|_| "Package lists updated successfully".to_string())
        .to_rhai_error()
}

/// Wrapper for os::package::PackHero::upgrade
///
/// Upgrade installed packages using the system package manager.
pub fn package_upgrade() -> Result<String, Box<EvalAltResult>> {
    let hero = PackHero::new();
    hero.upgrade()
        .map(|_| "Packages upgraded successfully".to_string())
        .to_rhai_error()
}

/// Wrapper for os::package::PackHero::list_installed
///
/// List installed packages using the system package manager.
pub fn package_list() -> Result<Array, Box<EvalAltResult>> {
    let hero = PackHero::new();
    let packages = hero.list_installed().to_rhai_error()?;

    // Convert Vec<String> to Rhai Array
    let mut array = Array::new();
    for package in packages {
        array.push(package.into());
    }

    Ok(array)
}

/// Wrapper for os::package::PackHero::search
///
/// Search for packages using the system package manager.
pub fn package_search(query: &str) -> Result<Array, Box<EvalAltResult>> {
    let hero = PackHero::new();
    let packages = hero.search(query).to_rhai_error()?;

    // Convert Vec<String> to Rhai Array
    let mut array = Array::new();
    for package in packages {
        array.push(package.into());
    }

    Ok(array)
}

/// Wrapper for os::package::PackHero::is_installed
///
/// Check if a package is installed using the system package manager.
pub fn package_is_installed(package: &str) -> Result<bool, Box<EvalAltResult>> {
    let hero = PackHero::new();
    hero.is_installed(package).to_rhai_error()
}

// Thread-local storage for package debug flag
thread_local! {
    static PACKAGE_DEBUG: std::cell::RefCell<bool> = std::cell::RefCell::new(false);
}

/// Set the debug mode for package management operations
pub fn package_set_debug(debug: bool) -> bool {
    let mut hero = PackHero::new();
    hero.set_debug(debug);

    // Also set the thread-local debug flag
    PACKAGE_DEBUG.with(|cell| {
        *cell.borrow_mut() = debug;
    });

    debug
}

/// Get the current platform name for package management
pub fn package_platform() -> String {
    let hero = PackHero::new();
    match hero.platform() {
        package::Platform::Ubuntu => "Ubuntu".to_string(),
        package::Platform::MacOS => "MacOS".to_string(),
        package::Platform::Unknown => "Unknown".to_string(),
    }
}

//
// Platform Detection Function Wrappers
//

/// Wrapper for platform::is_osx
pub fn platform_is_osx() -> bool {
    crate::platform::is_osx()
}

/// Wrapper for platform::is_linux
pub fn platform_is_linux() -> bool {
    crate::platform::is_linux()
}

/// Wrapper for platform::is_arm
pub fn platform_is_arm() -> bool {
    crate::platform::is_arm()
}

/// Wrapper for platform::is_x86
pub fn platform_is_x86() -> bool {
    crate::platform::is_x86()
}

/// Wrapper for platform::check_linux_x86
pub fn platform_check_linux_x86() -> Result<(), Box<EvalAltResult>> {
    crate::platform::check_linux_x86().map_err(|e| {
        Box::new(EvalAltResult::ErrorRuntime(
            format!("Platform Check Error: {}", e).into(),
            Position::NONE,
        ))
    })
}

/// Wrapper for platform::check_macos_arm
pub fn platform_check_macos_arm() -> Result<(), Box<EvalAltResult>> {
    crate::platform::check_macos_arm().map_err(|e| {
        Box::new(EvalAltResult::ErrorRuntime(
            format!("Platform Check Error: {}", e).into(),
            Position::NONE,
        ))
    })
}