This commit is contained in:
2025-08-16 05:13:18 +02:00
parent 97d506ecbf
commit 1cd8e8c299
10 changed files with 315 additions and 184 deletions

View File

@@ -3,16 +3,26 @@
import freeflowuniverse.herolib.core.playcmds
import freeflowuniverse.herolib.clients.giteaclient
// Configure PostgreSQL client
// heroscript := "
// !!giteaclient.configure
// url: 'git.ourworld.tf'
// user: 'despiegk'
// secret: ''
// "
heroscript := "
!!giteaclient.configure
name: 'default'
url: 'git.ourworld.tf'
user: 'despiegk'
secret: '1'
// // Process the heroscript configuration
// playcmds.play(heroscript: heroscript, emptycheck: false)!
!!giteaclient.configure
name: 'two'
url: 'git.ourworld.tf'
user: 'despiegk2'
secret: '2'
"
// Process the heroscript configuration
playcmds.play(heroscript: heroscript, emptycheck: false)!
println(giteaclient.list()!)
$dbg;
// Get the configured client
mut client := giteaclient.get()!

View File

@@ -14,61 +14,60 @@ __global (
@[params]
pub struct ArgsGet {
pub mut:
name string
name string = "default"
fromdb bool //will load from filesystem
create bool //default will not create if not exist
}
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&GiteaClient {
mut context := base.context()!
mut args := args_get(args_)
pub fn new(args ArgsGet) !&GiteaClient {
mut obj := GiteaClient{
name: args.name
}
if args.name !in giteaclient_global {
if !exists(args)! {
set(obj)!
} else {
name: args.name
}
set(obj)!
return &obj
}
pub fn get(args ArgsGet) !&GiteaClient {
mut context := base.context()!
giteaclient_default = args.name
if args.fromdb || args.name !in giteaclient_global {
if context.hero_config_exists('giteaclient', args.name) {
heroscript := context.hero_config_get('giteaclient', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}else{
if args.create {
new(args)!
}else{
return error("GiteaClient with name '${args.name}' does not exist")
}
}
return get(name: args.name)! //no longer from db nor create
}
return giteaclient_global[args.name] or {
println(giteaclient_global)
// bug if we get here because should be in globals
panic('could not get config for giteaclient with name, is bug:${args.name}')
return error('could not get config for giteaclient with name:${args.name}')
}
}
// register the config for the future
pub fn set(o GiteaClient) ! {
set_in_mem(o)!
giteaclient_default = o.name
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('giteaclient', o.name, heroscript)!
}
// does the config exists?
pub fn exists(args_ ArgsGet) !bool {
pub fn exists(args ArgsGet) !bool {
mut context := base.context()!
mut args := args_get(args_)
return context.hero_config_exists('giteaclient', args.name)
}
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
pub fn delete(args ArgsGet) ! {
mut context := base.context()!
context.hero_config_delete('giteaclient', args.name)!
if args.name in giteaclient_global {
// del giteaclient_global[args.name]
}
giteaclient_global.delete(args.name)
context.hero_config_delete('giteaclient', args.name)!
}
@[params]
@@ -77,12 +76,28 @@ pub mut:
fromdb bool //will load from filesystem
}
pub fn list(args ArgsList) ![]&GiteaClient {
// if fromdb set: load from filesystem, and not from mem, will also reset what is in mem
pub fn list(args ArgsList) ![]&GiteaClient {
mut res := []&GiteaClient{}
if args.name in giteaclient_global {
res << giteaclient_global[o.name]
mut context := base.context()!
if args.fromdb {
// reset what is in mem
giteaclient_global = map[string]&GiteaClient{}
giteaclient_default = ''
}
if args.fromdb {
for name in context.hero_config_list('giteaclient')!{
mut hscript := context.hero_config_get('giteaclient', name)!
mut obj := heroscript_loads(hscript)!
set_in_mem(obj)!
res << &obj
}
return res
} else {
// load from memory
for _, client in giteaclient_global {
res << client
}
}
return res
}

View File

@@ -133,28 +133,37 @@ pub fn (mut self Context) db_config_get() !dbfs.DB {
pub fn (mut self Context) hero_config_set(cat string, name string, content_ string) ! {
mut content := texttools.dedent(content_)
content = rootpath.shell_expansion(content)
path := '${self.path()!.path}/${cat}__${name}.yaml'
mut config_file := pathlib.get_file(path: path)!
path := '${self.path()!.path}/${cat}/${name}.hero'
mut config_file := pathlib.get_file(path: path,create: true)!
config_file.write(content)!
}
pub fn (mut self Context) hero_config_delete(cat string, name string) ! {
path := '${self.path()!.path}/${cat}__${name}.yaml'
path := '${self.path()!.path}/${cat}/${name}.hero'
mut config_file := pathlib.get_file(path: path)!
config_file.delete()!
}
pub fn (mut self Context) hero_config_exists(cat string, name string) bool {
path := '${os.home_dir()}/hero/context/${self.config.name}/${cat}__${name}.yaml'
path := '${os.home_dir()}/hero/context/${self.config.name}/${cat}/${name}.hero'
return os.exists(path)
}
pub fn (mut self Context) hero_config_get(cat string, name string) !string {
path := '${self.path()!.path}/${cat}__${name}.yaml'
path := '${self.path()!.path}/${cat}/${name}.hero'
mut config_file := pathlib.get_file(path: path, create: false)!
return config_file.read()!
}
pub fn (mut self Context) hero_config_list(cat string) ![]string {
path := '${self.path()!.path}/${cat}/*.hero'
mut config_files := os.ls(path)!
println(config_files)
$dbg;
return config_files
}
pub fn (mut self Context) secret_encrypt(txt string) !string {
return aes_symmetric.encrypt_str(txt, self.secret_get()!)
}

View File

@@ -21,103 +21,131 @@ __global (
/////////FACTORY
^^[params]
pub struct ArgsGet{
@[params]
pub struct ArgsGet {
pub mut:
name string
name string = "default"
fromdb bool //will load from filesystem
create bool //default will not create if not exist
}
@if args.hasconfig
fn args_get (args_ ArgsGet) ArgsGet {
mut args:=args_
if args.name == ""{
args.name = "default"
}
return args
pub fn new(args ArgsGet) !&${args.classname} {
mut obj := ${args.classname}{
name: args.name
}
set(obj)!
return &obj
}
pub fn get(args_ ArgsGet) !&${args.classname} {
mut context:=base.context()!
mut args := args_get(args_)
mut obj := ${args.classname}{name:args.name}
if !(args.name in ${args.name}_global) {
if ! exists(args)!{
set(obj)!
}else{
heroscript := context.hero_config_get("${args.name}",args.name)!
mut obj_:=heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
pub fn get(args ArgsGet) !&${args.classname} {
mut context := base.context()!
${args.name}_default = args.name
if args.fromdb || args.name !in ${args.name}_global {
if context.hero_config_exists('${args.name}', args.name) {
heroscript := context.hero_config_get('${args.name}', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}else{
if args.create {
new(args)!
}else{
return error("${args.classname} with name '\${args.name}' does not exist")
}
}
return get(name: args.name)! //no longer from db nor create
}
return ${args.name}_global[args.name] or {
println(${args.name}_global)
//bug if we get here because should be in globals
panic("could not get config for ${args.name} with name, is bug:??{args.name}")
}
return error('could not get config for ${args.name} with name:\${args.name}')
}
}
//register the config for the future
pub fn set(o ${args.classname})! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set("${args.name}", o.name, heroscript)!
// register the config for the future
pub fn set(o ${args.classname}) ! {
set_in_mem(o)!
${args.name}_default = o.name
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('${args.name}', o.name, heroscript)!
}
//does the config exists?
pub fn exists(args_ ArgsGet)! bool {
mut context := base.context()!
mut args := args_get(args_)
return context.hero_config_exists("${args.name}", args.name)
// does the config exists?
pub fn exists(args ArgsGet) !bool {
mut context := base.context()!
return context.hero_config_exists('${args.name}', args.name)
}
pub fn delete(args_ ArgsGet)! {
mut args := args_get(args_)
mut context:=base.context()!
context.hero_config_delete("${args.name}",args.name)!
if args.name in ${args.name}_global {
//del ${args.name}_global[args.name]
}
pub fn delete(args ArgsGet) ! {
mut context := base.context()!
${args.name}_global.delete(args.name)
context.hero_config_delete('${args.name}', args.name)!
}
pub fn list()![]&${args.classname} {
mut args := args_get(args_)
mut res:=[]&${args.classname}
if args.name in ${args.name}_global {
res << ${args.name}_global[o.name]
}
return res
@[params]
pub struct ArgsList {
pub mut:
fromdb bool //will load from filesystem
}
// if fromdb set: load from filesystem, and not from mem, will also reset what is in mem
pub fn list(args ArgsList) ![]&${args.classname} {
mut res := []&${args.classname}{}
mut context := base.context()!
if args.fromdb {
// reset what is in mem
${args.name}_global = map[string]&${args.classname}{}
${args.name}_default = ''
}
if args.fromdb {
for name in context.hero_config_list('${args.name}')!{
mut hscript := context.hero_config_get('${args.name}', name)!
mut obj := heroscript_loads(hscript)!
set_in_mem(obj)!
res << &obj
}
return res
} else {
// load from memory
for _, client in ${args.name}_global {
res << client
}
}
return res
}
//only sets in mem, does not set as config
fn set_in_mem(o ${args.classname})! {
mut o2:=obj_init(o)!
// only sets in mem, does not set as config
fn set_in_mem(o ${args.classname}) ! {
mut o2 := obj_init(o)!
${args.name}_global[o.name] = &o2
${args.name}_default = o.name
${args.name}_default = o.name
}
// switch instance to be used for ${args.name}
pub fn switch(name string) {
${args.name}_default = name
}
@else
pub fn get(args_ ArgsGet) !&${args.classname} {
return &${args.classname}{}
pub fn new(args ArgsGet) !&${args.classname} {
return &${args.classname}{}
}
@end
pub fn play(mut plbook PlayBook) ! {
@if args.hasconfig
mut install_actions := plbook.find(filter: '${args.name}.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript:=install_action.heroscript()
mut obj2:=heroscript_loads(heroscript)!
set(obj2)!
}
}
@end
mut install_actions := plbook.find(filter: '${args.name}.configure')!
if install_actions.len > 0 {
@if args.hasconfig
for install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
@else
panic("can't configure ??{${args.name}_obj}, because no config in this class.")
@end
}
@if args.cat == .installer
mut other_actions := plbook.find(filter: '${args.name}.')!
for other_action in other_actions {
@@ -155,16 +183,17 @@ pub fn play(mut plbook PlayBook) ! {
}
@end
}
@end
@end
}
@if args.cat == .installer
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
@if args.startupmanager
fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManager {
// unknown
// screen
@@ -185,6 +214,7 @@ fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManag
}
}
}
@end
@if args.hasconfig
//load from disk and make sure is properly intialized
@@ -305,11 +335,3 @@ pub fn (mut self ${args.classname}) destroy() ! {
pub fn switch(name string) {
${args.name}_default = name
}
//helpers
^^[params]
pub struct DefaultConfigArgs{
instance string = 'default'
}

View File

@@ -206,11 +206,11 @@ fn (mut repo GitRepo) update_submodules() ! {
fn (repo GitRepo) exec(cmd_ string) !string {
repo_path := repo.path()
if cmd_.starts_with("pull ") || cmd_.starts_with("push ") || cmd_.starts_with("fetch "){
//means we need to be able to fetch from remote repo, check we have a key registered e.g. for gitea
$dbg;
// if cmd_.starts_with("pull ") || cmd_.starts_with("push ") || cmd_.starts_with("fetch "){
// //means we need to be able to fetch from remote repo, check we have a key registered e.g. for gitea
// $dbg;
}
// }
cmd := 'cd ${repo_path} && git ${cmd_}'
// console.print_debug(cmd)
r := os.execute(cmd)

View File

@@ -1,12 +1,13 @@
!!hero_code.generate_installer
name: "python"
classname: "Python"
hasconfig: false
singleton: true
default: true
title: ""
templates: false
build: false
startupmanager: false
supported_platforms: ""
!!hero_code.generate_installer
name:'python'
classname:'PythonInstaller'
singleton:1
templates:0
default:1
title:''
supported_platforms:''
reset:0
startupmanager:0
hasconfig:0
build:0

View File

@@ -1,31 +1,28 @@
module python
import freeflowuniverse.herolib.osal.core as osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core
import freeflowuniverse.herolib.installers.base
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.installers.ulist
import freeflowuniverse.herolib.core.texttools
import os
//////////////////// following actions are not specific to instance of the object
// checks if a certain version or above is installed
fn installed() !bool {
res := os.execute('python3 --version')
res := os.execute('${osal.profile_path_source_and()!} uv self version')
if res.exit_code != 0 {
return false
}
r := res.output.split_into_lines().filter(it.trim_space().len > 0)
if r.len != 1 {
return error("couldn't parse pnpm version.\n${res.output}")
return error("couldn't parse python version.\n${res.output}")
}
if texttools.version(r[0].all_after_first('ython')) >= texttools.version(version) {
r2 := r[0].split(' ')[1] or { return error("couldn't parse python version.\n${res.output}") }
if texttools.version(version) <= texttools.version(r2) {
return true
}
return false
}
@@ -49,29 +46,44 @@ fn install() ! {
osal.package_install('python3')!
pl := core.platform()!
if pl == .arch {
osal.package_install('python-pipx,sqlite')!
} else if pl == .ubuntu {
osal.package_install('pipx,sqlite')!
if pl == .ubuntu {
osal.package_install('python3')!
} else if pl == .osx {
osal.package_install('pipx,sqlite')!
osal.package_install('python@3.12')!
} else {
return error('only support osx, arch & ubuntu.')
return error('only support osx & ubuntu.')
}
osal.execute_silent('curl -LsSf https://astral.sh/uv/install.sh | sh')!
if pl == .ubuntu {
osal.execute_silent('echo \'eval "$(uvx --generate-shell-completion bash)"\' >> ~/.bashrc')!
} else if pl == .osx {
osal.execute_silent('echo \'eval "$(uvx --generate-shell-completion bash)"\' >> ~/.bashrc')!
osal.execute_silent('echo \'eval "$(uvx --generate-shell-completion bash)"\' >> ~/.zshrc')!
} else {
return error('only support osx & ubuntu.')
}
osal.execute_silent('pipx install uv')!
}
fn destroy() ! {
console.print_header('destroy python')
osal.package_remove('python3')!
pl := core.platform()!
if pl == .arch {
osal.package_remove('pipx,sqlite')!
} else if pl == .ubuntu {
osal.package_remove('pipx,sqlite')!
} else if pl == .osx {
osal.package_remove('pipx,sqlite')!
} else {
return error('only support osx, arch & ubuntu.')
}
console.print_header('remove python uv')
// //will remove all paths where go/bin is found
// osal.profile_path_add_remove(paths2delete:"go/bin")!
dir1 := osal.exec_fast(cmd: 'uv python dir', notempty: true)!
dir2 := osal.exec_fast(cmd: 'uv tool dir', notempty: true)!
dir3 := osal.exec_fast(cmd: 'uv cache dir', notempty: true)!
osal.execute_silent('
uv cache clean
rm -rf "${dir1}"
rm -rf "${dir2}"
rm -rf "${dir3}"
rm ~/.local/bin/uv ~/.local/bin/uvx
')!
osal.rm('
uv
uvx
')!
}

View File

@@ -6,7 +6,7 @@ import freeflowuniverse.herolib.osal.startupmanager
import freeflowuniverse.herolib.osal.zinit
__global (
python_global map[string]&Python
python_global map[string]&PythonInstaller
python_default string
)
@@ -18,8 +18,8 @@ pub mut:
name string
}
pub fn get(args_ ArgsGet) !&Python {
return &Python{}
pub fn get(args_ ArgsGet) !&PythonInstaller {
return &PythonInstaller{}
}
pub fn play(mut plbook PlayBook) ! {
@@ -72,14 +72,14 @@ pub mut:
reset bool
}
pub fn (mut self Python) install(args InstallArgs) ! {
pub fn (mut self PythonInstaller) install(args InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
install()!
}
}
pub fn (mut self Python) destroy() ! {
pub fn (mut self PythonInstaller) destroy() ! {
switch(self.name)
destroy()!
}

View File

@@ -1,20 +1,21 @@
module python
import freeflowuniverse.herolib.data.encoderhero
import os
pub const version = '3.12.0'
const singleton = true
pub const version = '0.8.11'
const singleton = false
const default = true
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
@[heap]
pub struct Python {
pub struct PythonInstaller {
pub mut:
name string = 'default'
}
// your checking & initialization code if needed
fn obj_init(mycfg_ Python) !Python {
fn obj_init(mycfg_ PythonInstaller) !PythonInstaller {
mut mycfg := mycfg_
return mycfg
}
@@ -26,11 +27,11 @@ fn configure() ! {
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj Python) !string {
return encoderhero.encode[Python](obj)!
pub fn heroscript_dumps(obj PythonInstaller) !string {
return encoderhero.encode[PythonInstaller](obj)!
}
pub fn heroscript_loads(heroscript string) !Python {
mut obj := encoderhero.decode[Python](heroscript)!
pub fn heroscript_loads(heroscript string) !PythonInstaller {
mut obj := encoderhero.decode[PythonInstaller](heroscript)!
return obj
}

View File

@@ -1,6 +1,6 @@
module core
// import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.texttools
// import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.ui.console
import json
@@ -481,3 +481,64 @@ pub fn exec_string(cmd Command) !string {
job.cmd.scriptpath = cmd_to_script_path(job.cmd)!
return job.cmd.scriptpath
}
@[params]
pub struct CommandFast {
pub mut:
cmd string
ignore_error bool // means if error will just exit and not raise, there will be no error reporting
work_folder string // location where cmd will be executed
environment map[string]string // env variables
ignore_error_codes []int
debug bool // if debug will put +ex in the script which is being executed and will make sure script stays
includeprofile bool
notempty bool
}
//execute something fast, make sure it returns an error if the result is empty
//source the
pub fn exec_fast(cmd_ CommandFast) !string {
mut cmd_str := texttools.dedent(cmd_.cmd)
mut toexecute:=[]string{}
if cmd_.debug {
// Add +ex for debug mode if it's a bash script
// This is a simplification, ideally we'd check if it's a bash script
// For now, assume it's a bash script if debug is true and prepend
toexecute << 'set -ex'
}
if cmd_.includeprofile {
toexecute << profile_path_source_and()!
}
for key,val in cmd_.environment {
toexecute << 'export ${key}=${val}'
}
if cmd_.work_folder.len > 0 {
toexecute << "cd ${cmd_.work_folder}"
}
toexecute << cmd_str
mut cmd_str2 := toexecute.join('\n')
result := os.execute(cmd_str2)
if result.exit_code != 0 {
if !(cmd_.ignore_error || result.exit_code in cmd_.ignore_error_codes) {
return error("couldn't execute '${cmd_.cmd}', result code: ${result.exit_code}\n${result.output}")
}
}
if cmd_.notempty && result.output.len == 0 {
return error("couldn't execute '${cmd_.cmd}', the result is empty but notempty is true")
}
return result.output
}