* development: (21 commits) ... chore: Add FsListArg struct and update imports refactor: Rename module and import path refactor: Use snake_case for object names and constants fix: fix conflicts refactor: Improve struct decoding and skip logic .. ... ... ... ... feat: Enhance params parsing and encoding ... test: Add tests for struct encoding and decoding feat: improve build and serialization logic refactor: Improve code structure and logging feat: Improve herolib installation logic in CI refactor: Improve herolib installation script fix: Rename freeflowuniverse to incubaid added fedora for the package install during hero execution ... # Conflicts: # install_herolib.vsh
88 lines
2.2 KiB
V
88 lines
2.2 KiB
V
module core
|
|
|
|
import incubaid.herolib.core.texttools
|
|
import incubaid.herolib.core
|
|
import incubaid.herolib.ui.console
|
|
import os
|
|
|
|
pub fn file_write(path string, text string) ! {
|
|
return os.write_file(path, text)
|
|
}
|
|
|
|
pub fn file_read(path string) !string {
|
|
return os.read_file(path)
|
|
}
|
|
|
|
// remove all if it exists
|
|
pub fn dir_ensure(path string) ! {
|
|
if !os.exists(path) {
|
|
os.mkdir_all(path)!
|
|
}
|
|
}
|
|
|
|
// remove all if it exists
|
|
pub fn dir_delete(path string) ! {
|
|
if os.exists(path) {
|
|
return os.rmdir_all(path)
|
|
}
|
|
}
|
|
|
|
// remove all if it exists
|
|
// and then (re-)create
|
|
pub fn dir_reset(path string) ! {
|
|
os.rmdir_all(path)!
|
|
os.mkdir_all(path)!
|
|
}
|
|
|
|
// can be list of dirs, files
|
|
// ~ supported
|
|
// can be \n or , separated
|
|
pub fn rm(todelete_ string) ! {
|
|
for mut item in texttools.to_array(todelete_) {
|
|
if item.trim_space() == '' || item.trim_space().starts_with('#') {
|
|
continue
|
|
}
|
|
if item.len < 2 {
|
|
return error('not allowed to remove anything with less than 2 chars. ${item}')
|
|
}
|
|
|
|
item = item.replace('~', os.home_dir())
|
|
|
|
if item.starts_with('/') {
|
|
if os.exists(item) {
|
|
console.print_debug(' - rm: ${item}')
|
|
if os.is_dir(item) {
|
|
if core.sudo_path_ok(item)! || whoami()! == 'root' {
|
|
// console.print_debug("rm deletedir: ${item}")
|
|
os.rmdir_all(item)!
|
|
} else {
|
|
if core.interactive()! {
|
|
execute_silent('sudo rm -rf ${item}')!
|
|
} else {
|
|
return error("can't remove ${item} as sudo because non interactive")
|
|
}
|
|
}
|
|
} else {
|
|
// console.print_debug("rm delete file: ${item}")
|
|
if core.sudo_path_ok(item)! || whoami()! == 'root' {
|
|
os.rm(item)!
|
|
} else {
|
|
if core.interactive()! {
|
|
execute_silent('sudo rm -f ${item}')!
|
|
} else {
|
|
return error("can't remove ${item} as sudo because non interactive")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
// Handle relative paths - they can contain '/' as long as they're valid relative paths
|
|
if item.contains('/') && !item.starts_with('./') && !item.starts_with('../')
|
|
&& item.contains('..') {
|
|
return error('relative paths with .. are not allowed for security: ${item}')
|
|
}
|
|
cmd_delete(item)! // look for the command, if will be removed if found
|
|
}
|
|
}
|
|
}
|