refactor: Extract heroscript path handling logic
- Add helper function to expand and validate file paths - Add helper function to validate heroscript content - Add helper function to run heroscript from file - Inline scripts now validated before execution - File-based scripts now use the new run_from_file helper
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
module herocmds
|
||||
|
||||
import incubaid.herolib.ui.console
|
||||
import incubaid.herolib.core.playcmds
|
||||
import os
|
||||
import cli { Command, Flag }
|
||||
@@ -49,6 +48,51 @@ USAGE:
|
||||
cmdroot.add_command(cmd_run)
|
||||
}
|
||||
|
||||
// Helper function to expand and validate file path
|
||||
fn expand_and_validate_path(path_ string) !string {
|
||||
mut path := path_
|
||||
|
||||
// Handle "." as current working directory
|
||||
if path == '.' {
|
||||
path = os.getwd()
|
||||
} else {
|
||||
// Expand home directory
|
||||
path = path.replace('~', os.home_dir())
|
||||
|
||||
// Validate that path exists
|
||||
if !os.exists(path) {
|
||||
return error('File does not exist: ${path}')
|
||||
}
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
// Helper function to validate that heroscript content contains at least one action
|
||||
fn validate_heroscript_content(content string) ! {
|
||||
has_action := content.split_into_lines().any(it.trim_space().starts_with('!!'))
|
||||
if !has_action {
|
||||
return error('Invalid heroscript: No actions found. Heroscript must contain at least one action starting with !!')
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to run heroscript from file path
|
||||
fn run_from_file(path string, reset bool) ! {
|
||||
// Expand and validate path
|
||||
expanded_path := expand_and_validate_path(path)!
|
||||
|
||||
// Read and validate content
|
||||
content := os.read_file(expanded_path) or {
|
||||
return error('Failed to read file ${expanded_path}: ${err}')
|
||||
}
|
||||
validate_heroscript_content(content)!
|
||||
|
||||
// Execute heroscript
|
||||
playcmds.run(heroscript_path: expanded_path, reset: reset) or {
|
||||
return error('Heroscript execution failed for ${expanded_path}: ${err}')
|
||||
}
|
||||
}
|
||||
|
||||
fn cmd_run_execute(cmd Command) ! {
|
||||
mut reset := cmd.flags.get_bool('reset') or { false }
|
||||
mut inline_script := cmd.flags.get_string('script') or { '' }
|
||||
@@ -76,68 +120,22 @@ fn cmd_run_execute(cmd Command) ! {
|
||||
|
||||
// If inline script is provided via -s flag
|
||||
if inline_script != '' {
|
||||
console.print_header('Running inline heroscript...')
|
||||
|
||||
// Create a temporary file to hold the heroscript
|
||||
temp_dir := os.temp_dir()
|
||||
temp_file := '${temp_dir}/hero_inline_${os.getpid()}.heroscript'
|
||||
|
||||
// Write the inline script to temp file
|
||||
os.write_file(temp_file, inline_script) or {
|
||||
return error('Failed to write temporary heroscript file: ${err}')
|
||||
validate_heroscript_content(inline_script)!
|
||||
playcmds.run(heroscript: inline_script, reset: reset) or {
|
||||
return error('Heroscript execution failed: ${err}')
|
||||
}
|
||||
|
||||
// Ensure cleanup
|
||||
defer {
|
||||
os.rm(temp_file) or {}
|
||||
}
|
||||
|
||||
// Run the heroscript
|
||||
playcmds.run(heroscript_path: temp_file, reset: reset)!
|
||||
return
|
||||
}
|
||||
|
||||
// If path is provided via -p flag
|
||||
if path_flag != '' {
|
||||
mut path := path_flag
|
||||
|
||||
// Handle "." as current working directory
|
||||
if path == '.' {
|
||||
path = os.getwd()
|
||||
} else {
|
||||
// Expand home directory
|
||||
path = path.replace('~', os.home_dir())
|
||||
|
||||
// Validate that path exists
|
||||
if !os.exists(path) {
|
||||
return error('File does not exist: ${path}')
|
||||
}
|
||||
}
|
||||
|
||||
console.print_header('Running heroscript from file: ${path}')
|
||||
playcmds.run(heroscript_path: path, reset: reset)!
|
||||
run_from_file(path_flag, reset)!
|
||||
return
|
||||
}
|
||||
|
||||
// If file path is provided as argument
|
||||
if cmd.args.len > 0 {
|
||||
mut path := cmd.args[0]
|
||||
|
||||
// Handle "." as current working directory
|
||||
if path == '.' {
|
||||
path = os.getwd()
|
||||
} else {
|
||||
// Expand home directory
|
||||
path = path.replace('~', os.home_dir())
|
||||
|
||||
// Validate that path exists
|
||||
if !os.exists(path) {
|
||||
return error('File does not exist: ${path}')
|
||||
}
|
||||
}
|
||||
|
||||
console.print_header('Running heroscript from file: ${path}')
|
||||
playcmds.run(heroscript_path: path, reset: reset)!
|
||||
run_from_file(cmd.args[0], reset)!
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user