postgresql & new docs
This commit is contained in:
@@ -2,16 +2,16 @@ module core
|
||||
|
||||
import base
|
||||
|
||||
//check if we are interactive in current context
|
||||
pub fn interactive()!bool{
|
||||
mut c:=base.context()!
|
||||
if c.config.interactive{
|
||||
// check if we are interactive in current context
|
||||
pub fn interactive() !bool {
|
||||
mut c := base.context()!
|
||||
if c.config.interactive {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
pub fn interactive_set()!{
|
||||
mut c:=base.context()!
|
||||
pub fn interactive_set() ! {
|
||||
mut c := base.context()!
|
||||
c.config.interactive = true
|
||||
}
|
||||
|
||||
@@ -25,4 +25,3 @@ fn test_memdb_exists() {
|
||||
memdb_set('empty_key', '')
|
||||
assert memdb_exists('empty_key') == false
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import os
|
||||
// import freeflowuniverse.herolib.ui.console
|
||||
// Returns the enum value that matches the provided string for PlatformType
|
||||
|
||||
|
||||
pub enum PlatformType {
|
||||
unknown
|
||||
osx
|
||||
@@ -25,7 +24,6 @@ pub fn platform_enum_from_string(platform string) PlatformType {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Returns the enum value that matches the provided string for CPUType
|
||||
pub fn cputype_enum_from_string(cputype string) CPUType {
|
||||
return match cputype.to_lower() {
|
||||
@@ -54,7 +52,7 @@ pub fn cmd_exists(cmd string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
pub fn platform()! PlatformType {
|
||||
pub fn platform() !PlatformType {
|
||||
mut platform_ := PlatformType.unknown
|
||||
platform_ = platform_enum_from_string(memdb_get('platformtype'))
|
||||
if platform_ != PlatformType.unknown {
|
||||
@@ -77,18 +75,18 @@ pub fn platform()! PlatformType {
|
||||
return platform_
|
||||
}
|
||||
|
||||
pub fn cputype()! CPUType {
|
||||
pub fn cputype() !CPUType {
|
||||
mut cputype_ := CPUType.unknown
|
||||
cputype_ = cputype_enum_from_string(memdb_get('cputype'))
|
||||
if cputype_ != CPUType.unknown {
|
||||
return cputype_
|
||||
}
|
||||
res := os.execute('uname -m')
|
||||
if res.exit_code >0{
|
||||
if res.exit_code > 0 {
|
||||
return error("can't execute uname -m")
|
||||
}
|
||||
sys_info := res.output
|
||||
|
||||
|
||||
cputype_ = match sys_info.to_lower().trim_space() {
|
||||
'x86_64' {
|
||||
CPUType.intel
|
||||
@@ -110,37 +108,37 @@ pub fn cputype()! CPUType {
|
||||
return cputype_
|
||||
}
|
||||
|
||||
pub fn is_osx()! bool {
|
||||
pub fn is_osx() !bool {
|
||||
return platform()! == .osx
|
||||
}
|
||||
|
||||
pub fn is_osx_arm()! bool {
|
||||
pub fn is_osx_arm() !bool {
|
||||
return platform()! == .osx && cputype()! == .arm
|
||||
}
|
||||
|
||||
pub fn is_osx_intel()! bool {
|
||||
pub fn is_osx_intel() !bool {
|
||||
return platform()! == .osx && cputype()! == .intel
|
||||
}
|
||||
|
||||
pub fn is_ubuntu()! bool {
|
||||
pub fn is_ubuntu() !bool {
|
||||
return platform()! == .ubuntu
|
||||
}
|
||||
|
||||
pub fn is_linux()! bool {
|
||||
pub fn is_linux() !bool {
|
||||
return platform()! == .ubuntu || platform()! == .arch || platform()! == .suse
|
||||
|| platform()! == .alpine
|
||||
}
|
||||
|
||||
pub fn is_linux_arm()!bool {
|
||||
pub fn is_linux_arm() !bool {
|
||||
// console.print_debug("islinux:${is_linux()!} cputype:${cputype()!}")
|
||||
return is_linux()! && cputype()! == .arm
|
||||
}
|
||||
|
||||
pub fn is_linux_intel()! bool {
|
||||
pub fn is_linux_intel() !bool {
|
||||
return is_linux()! && cputype()! == .intel
|
||||
}
|
||||
|
||||
pub fn hostname()!string {
|
||||
pub fn hostname() !string {
|
||||
res := os.execute('hostname')
|
||||
if res.exit_code > 0 {
|
||||
return error("can't get hostname. Error.")
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
module core
|
||||
|
||||
fn test_platform()! {
|
||||
fn test_platform() ! {
|
||||
assert platform()! != .unknown
|
||||
}
|
||||
|
||||
fn test_cputype()! {
|
||||
fn test_cputype() ! {
|
||||
assert cputype()! != .unknown
|
||||
}
|
||||
|
||||
@@ -50,6 +50,19 @@ And CPU architectures:
|
||||
|
||||
The core module provides essential functionality used by other Hero framework components. Key features include:
|
||||
|
||||
### Interactivity Check & Influence on delete
|
||||
|
||||
```
|
||||
import freeflowuniverse.herolib.installers.lang.golang
|
||||
import freeflowuniverse.herolib.core
|
||||
|
||||
core.interactive_set()! //make sure the sudo works so we can do things even if it requires those rights
|
||||
|
||||
//this will allow files which are in sudo area to still get them removed but its important interactive is set on the context.
|
||||
golang.install(reset:false)!
|
||||
```
|
||||
|
||||
|
||||
### Platform Detection
|
||||
```v
|
||||
// Check platform type
|
||||
@@ -74,13 +87,41 @@ value := core.memdb_get('key')
|
||||
|
||||
### Sudo Operations
|
||||
|
||||
The sudo operations module provides comprehensive permission management and command elevation handling:
|
||||
|
||||
```v
|
||||
// Check sudo requirements
|
||||
// Check if sudo is required for the current user
|
||||
if core.sudo_required()! {
|
||||
// Handle sudo requirements
|
||||
// Returns false if user is root or on macOS
|
||||
// Returns true if user has sudo privileges
|
||||
}
|
||||
|
||||
// Verify path permissions
|
||||
path := core.sudo_path_check('/protected/path', true)!
|
||||
```
|
||||
// Verify path permissions and accessibility
|
||||
path := core.sudo_path_check('/path/to/check')! {
|
||||
// Returns the path if accessible
|
||||
// Errors if path requires sudo rights
|
||||
}
|
||||
|
||||
// Check if a path is accessible without sudo
|
||||
if core.sudo_path_ok('/usr/local/bin')! {
|
||||
// Returns false for protected directories like:
|
||||
// /usr/, /boot, /etc, /root/
|
||||
// Returns true if path is accessible
|
||||
}
|
||||
|
||||
// Check and modify commands that require sudo
|
||||
cmd := core.sudo_cmd_check('ufw enable')! {
|
||||
// Automatically adds 'sudo' prefix if:
|
||||
// 1. Command requires elevated privileges
|
||||
// 2. User doesn't have sudo rights
|
||||
// 3. Running in interactive mode
|
||||
}
|
||||
|
||||
// Check if current process has sudo rights
|
||||
if core.sudo_rights_check()! {
|
||||
// Returns true if:
|
||||
// - Running as root user
|
||||
// - Has necessary sudo privileges
|
||||
}
|
||||
```
|
||||
|
||||
@@ -3,23 +3,23 @@ module core
|
||||
import base
|
||||
import os
|
||||
|
||||
//check path is accessible, e.g. do we need sudo and are we sudo
|
||||
// check path is accessible, e.g. do we need sudo and are we sudo
|
||||
// if ok then will just return the same path string as output
|
||||
pub fn sudo_path_check(path string)!string{
|
||||
if sudo_path_ok(path)!{
|
||||
pub fn sudo_path_check(path string) !string {
|
||||
if sudo_path_ok(path)! {
|
||||
return path
|
||||
}
|
||||
return error("Can't write/delete path:${path} because of no rights.")
|
||||
}
|
||||
|
||||
//return false if we can't work on the path
|
||||
pub fn sudo_path_ok(path string)!bool{
|
||||
if sudo_rights_check()!{
|
||||
// return false if we can't work on the path
|
||||
pub fn sudo_path_ok(path string) !bool {
|
||||
if sudo_rights_check()! {
|
||||
return true
|
||||
}
|
||||
// Check if path is in protected directories
|
||||
for item in ["/usr/","/boot","/etc","/root/"]{
|
||||
if path.starts_with(item){
|
||||
for item in ['/usr/', '/boot', '/etc', '/root/'] {
|
||||
if path.starts_with(item) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -27,23 +27,20 @@ pub fn sudo_path_ok(path string)!bool{
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
//if we know cmd requires sudo rights
|
||||
pub fn sudo_cmd(cmd string)!bool{
|
||||
cmd2:=cmd.split(" ")[0]
|
||||
if cmd2 in [
|
||||
"ufw"
|
||||
] {
|
||||
// if we know cmd requires sudo rights
|
||||
pub fn sudo_cmd(cmd string) !bool {
|
||||
cmd2 := cmd.split(' ')[0]
|
||||
if cmd2 == 'ufw' {
|
||||
return true
|
||||
}
|
||||
//TODO: need many more checks
|
||||
// TODO: need many more checks
|
||||
return false
|
||||
}
|
||||
|
||||
//if sudo required and we are interactive then we will put sudo in front of returned cmd
|
||||
pub fn sudo_cmd_check(cmd string)!string{
|
||||
// if sudo required and we are interactive then we will put sudo in front of returned cmd
|
||||
pub fn sudo_cmd_check(cmd string) !string {
|
||||
// If we have sudo rights, no need to add sudo prefix
|
||||
if sudo_rights_check()!{
|
||||
if sudo_rights_check()! {
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -54,29 +51,27 @@ pub fn sudo_cmd_check(cmd string)!string{
|
||||
return cmd
|
||||
}
|
||||
|
||||
if interactive()!{
|
||||
return "sudo ${cmd}"
|
||||
if interactive()! {
|
||||
return 'sudo ${cmd}'
|
||||
}
|
||||
|
||||
return error("can't execute the cmd, because no sudo rights.\ncmd:'${cmd}'")
|
||||
}
|
||||
|
||||
|
||||
|
||||
//check of we have sudo rights, if yes return true
|
||||
pub fn sudo_rights_check() ! bool {
|
||||
// check of we have sudo rights, if yes return true
|
||||
pub fn sudo_rights_check() !bool {
|
||||
// Check if the user is root
|
||||
if os.getenv('USER') == 'root' {
|
||||
return true
|
||||
}
|
||||
//TOOD: we can do more
|
||||
// TOOD: we can do more
|
||||
return false
|
||||
}
|
||||
|
||||
// Method to check if sudo is required (i.e., if the user is root or has sudo privileges)
|
||||
pub fn sudo_required()!bool {
|
||||
pub fn sudo_required() !bool {
|
||||
// Check if the user is root
|
||||
if sudo_rights_check()!{
|
||||
if sudo_rights_check()! {
|
||||
return false
|
||||
}
|
||||
platform_ := platform()!
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
module core
|
||||
|
||||
import os
|
||||
import base
|
||||
|
||||
@@ -9,7 +10,7 @@ fn init_context() ! {
|
||||
c.save()!
|
||||
}
|
||||
|
||||
fn test_sudo_required()! {
|
||||
fn test_sudo_required() ! {
|
||||
init_context()!
|
||||
// Test if sudo requirement detection works
|
||||
required := sudo_required()!
|
||||
@@ -35,33 +36,30 @@ fn test_sudo_path_ok() {
|
||||
fn test_sudo_path_protected() {
|
||||
init_context()!
|
||||
// Test path permission checks for protected paths
|
||||
p := "/usr/local"
|
||||
|
||||
p := '/usr/local'
|
||||
|
||||
// Protected paths should require sudo
|
||||
assert sudo_path_ok(p)! == false
|
||||
}
|
||||
|
||||
|
||||
fn test_sudo_cmd_check() {
|
||||
init_context()!
|
||||
assert interactive()!, 'interactive mode should be set'
|
||||
|
||||
|
||||
// Test command sudo requirement checking for non-sudo command
|
||||
cmd := 'echo test'
|
||||
|
||||
|
||||
result := sudo_cmd_check(cmd)!
|
||||
assert result == cmd
|
||||
|
||||
}
|
||||
|
||||
fn test_sudo_cmd_check_sudo_required()! {
|
||||
fn test_sudo_cmd_check_sudo_required() ! {
|
||||
init_context()!
|
||||
assert interactive()!, 'interactive mode should be set'
|
||||
|
||||
|
||||
// Test command sudo requirement checking for sudo-required command
|
||||
cmd := 'ufw something'
|
||||
|
||||
|
||||
result := sudo_cmd_check(cmd)!
|
||||
assert result == 'sudo ${cmd}'
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user