generator code for herolib

This commit is contained in:
2024-12-25 17:52:57 +01:00
parent 63660bbb90
commit 04e18f439b
16 changed files with 376 additions and 446 deletions

View File

@@ -2,4 +2,37 @@
import freeflowuniverse.herolib.code.generator.generic
mut fp := flag.new_flag_parser(os.args)
fp.application('generate.vsh')
fp.version('v0.1.0')
fp.description('Generate code')
fp.skip_executable()
path := fp.string('path', `p`, "", 'Path where to generate a module, if not mentioned will scan over all installers & clients')
reset := fp.bool('reset', `r`, false, 'If we want to reset')
is_installer := fp.bool('installer', `i`, false, 'If we want an installer, otherwise will be client')
help_requested := fp.bool('help', `h`, false, 'Show help message')
if help_requested {
println(fp.usage())
exit(0)
}
additional_args := fp.finalize() or {
eprintln(err)
println(fp.usage())
exit(1)
}
if additional_args.len > 0 {
eprintln('Unexpected arguments: ${additional_args.join(' ')}')
println(fp.usage())
exit(1)
}
if path!=""{
//TODO: create path
}
generic.scan(path:"~/code/github/freeflowuniverse/herolib/lib/installers",force:true, add:true)!

View File

@@ -0,0 +1,61 @@
module generic
import freeflowuniverse.herolib.ui.console
import os
import freeflowuniverse.herolib.core.pathlib
// will ask questions & create the .heroscript
pub fn ask(args_ GenerateArgs) ! {
mut myconsole := console.new()
mut args := args_
if args.model.name == '' {
args.model.name = os.base(args.path)
}
console.clear()
console.print_header('Configure generation of code for a module on path:')
console.print_green('Path: ${args.path}')
console.lf()
if args.path.contains("clients"){
args.model.cat == .client
}else{
args.model.cat == .installer
}
args.model.classname = myconsole.ask_question(
description: 'Class name of the ${args.model.cat}'
question: 'What is the class name of the generator e.g. MyClass ?'
warning: 'Please provide a valid class name for the generator'
minlen: 4
)!
args.model.title = myconsole.ask_question(
description: 'Title of the ${args.model.cat} (optional)'
)!
args.model.singleton = !myconsole.ask_yesno(
description: 'Can there be multiple instances (normally yes)?'
)!
if args.model.cat == .installer {
args.model.templates = myconsole.ask_yesno(
description: 'Will there be templates available for your installer?'
)!
args.model.startupmanager = myconsole.ask_yesno(
description: 'Is this an installer which will be managed by a startup mananger?'
)!
args.model.build = myconsole.ask_yesno(
description: 'Are there builders for the installers (compilation)'
)!
}
gen_model_set(args.model)!
}

View File

@@ -2,209 +2,38 @@ module generic
import freeflowuniverse.herolib.ui.console
import os
import freeflowuniverse.herolib.core.pathlib
@[params]
pub struct GenerateArgs {
pub mut:
reset bool // regenerate all, dangerous !!!
interactive bool //if we want to ask
path string
model GenModel
}
// will ask questions when not in force mode
// & generate the module
pub fn generate(args_ GeneratorArgs) ! {
mut myconsole := console.new()
pub fn generate(args_ GenerateArgs) ! {
mut args := args_
console.print_header('Generate code for path: ${args.path} (reset:${args.force}, force:${args.force})')
console.print_debug(args)
if args.path == '' {
args.path = os.getwd()
}
if args.name == '' {
args.name = os.base(args.path)
if args.model.name == '' {
args.model.name = os.base(args.path)
}
if args.force {
mut config_path0 := pathlib.get_file(path: '${args.path}/.heroscript', create: false)!
if !config_path0.exists() {
return error("can't generate in force mode (non interactive) if ${config_path0.path} not found.")
}
generate_exec(args.path, args.reset)!
return
console.print_header('Generate code for path: ${args.path} (reset:${args.reset}, interactive:${args.interactive})')
console.print_debug(args)
if args.interactive{
ask(args)!
}
console.clear()
console.print_header('Configure generation of code for a module on path:')
console.print_green('Path: ${args.path}')
console.lf()
args.model = gen_model_get(args.path)!
mut config_path := pathlib.get_file(path: '${args.path}/.heroscript', create: false)!
mut pathok := false
if config_path.exists() {
console.print_stdout(config_path.read()!)
console.lf()
myyes := myconsole.ask_yesno(
description: 'We found this heroscript, do you want to make a new one?'
)!
if myyes {
config_path.delete()!
pathok = true
} else {
myyes2 := myconsole.ask_yesno(description: 'Do you want to run it?')!
if myyes2 {
generate_exec(args.path, args.reset)!
} else {
console.print_stderr('Generation aborted.')
}
return
}
}
if pathok == false {
yesno := myconsole.ask_yesno(description: 'Is this path ok?')!
if !yesno {
return error("can't continue without a valid path")
}
}
mycat := myconsole.ask_dropdown(
description: 'Category of the generator'
question: 'What is the category of the generator?'
items: [
'installer',
'client',
]
warning: 'Please select a category'
)!
if mycat == 'installer' {
args.cat = .installer
} else {
args.cat = .client
}
// if args.name==""{
// yesno := myconsole.ask_yesno(description: 'Are you happy with name ${args.name}?')!
// if !yesno {
// return error("can't continue without a valid name, rename the directory you operate in.")
// }
// }
args.classname = myconsole.ask_question(
description: 'Class name of the ${mycat}'
question: 'What is the class name of the generator e.g. MyClass ?'
warning: 'Please provide a valid class name for the generator'
minlen: 4
)!
args.title = myconsole.ask_question(
description: 'Title of the ${mycat} (optional)'
)!
if args.cat == .installer {
args.hasconfig = myconsole.ask_yesno(
description: 'Does your installer have a config (normally yes)?'
)!
}
if args.hasconfig {
args.default = myconsole.ask_yesno(
description: 'Is it ok when doing new() that a default is created (normally yes)?'
)!
args.singleton = !myconsole.ask_yesno(
description: 'Can there be multiple instances (normally yes)?'
)!
}
// args.supported_platforms = myconsole.ask_dropdown_multiple(
// description: 'Supported platforms'
// question: 'Which platforms are supported?'
// items: [
// 'osx',
// 'ubuntu',
// 'arch',
// ]
// warning: 'Please select one or more platforms'
// )!
if args.cat == .installer {
args.templates = myconsole.ask_yesno(
description: 'Will there be templates available for your installer?'
)!
args.startupmanager = myconsole.ask_yesno(
description: 'Is this an installer which will be managed by a startup mananger?'
)!
args.build = myconsole.ask_yesno(
description: 'Are there builders for the installers (compilation)'
)!
}
// args.reset = myconsole.ask_yesno(
// description: 'Reset, overwrite code.'
// question: 'This will overwrite all files in your existing dir, be carefule?'
// )!
create_heroscript(args)!
generate_exec(args.path, true)!
}
pub fn create_heroscript(args GeneratorArgs) ! {
mut script := ''
if args.cat == .installer {
script = "
!!hero_code.generate_installer
name:'${args.name}'
classname:'${args.classname}'
singleton:${if args.singleton {
'1'
} else {
'0'
}}
templates:${if args.templates { '1' } else { '0' }}
default:${if args.default {
'1'
} else {
'0'
}}
title:'${args.title}'
supported_platforms:''
reset:${if args.reset {
'1'
} else {
'0'
}}
startupmanager:${if args.startupmanager { '1' } else { '0' }}
hasconfig:${if args.hasconfig {
'1'
} else {
'0'
}}
build:${if args.build {
'1'
} else {
'0'
}}"
} else {
script = "
!!hero_code.generate_client
name:'${args.name}'
classname:'${args.classname}'
singleton:${if args.singleton {
'1'
} else {
'0'
}}
default:${if args.default { '1' } else { '0' }}
hasconfig:${if args.hasconfig {
'1'
} else {
'0'
}}
reset:${if args.reset {
'1'
} else {
'0'
}}"
}
if !os.exists(args.path) {
os.mkdir(args.path)!
}
os.write_file('${args.path}/.heroscript', script)!
generate(args)!
}

View File

@@ -0,0 +1,83 @@
module generic
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.pathlib
//generate based on filled in args, ask has to be done before
fn generate(args GenerateArgs) ! {
console.print_debug('generate code for path: ${args.path}')
//as used in the templates
model :=args.model
mut path_actions := pathlib.get(args.path + '/${args.model.name}_actions.v')
if args.reset {
path_actions.delete()!
}
if !path_actions.exists() && args.model.cat == .installer {
console.print_debug('write installer actions')
mut templ_1 := $tmpl('templates/objname_actions.vtemplate')
pathlib.template_write(templ_1, '${args.path}/${args.model.name}_actions.v', true)!
}
mut templ_2 := $tmpl('templates/objname_factory_.vtemplate')
pathlib.template_write(templ_2, '${args.path}/${args.model.name}_factory_.v', true)!
mut path_model := pathlib.get(args.path + '/${args.model.name}_model.v')
if args.reset || !path_model.exists() {
console.print_debug('write model.')
mut templ_3 := $tmpl('templates/objname_model.vtemplate')
pathlib.template_write(templ_3, '${args.path}/${args.model.name}_model.v', true)!
}
// TODO: check case sensistivity for delete
mut path_readme := pathlib.get(args.path + '/readme.md')
if args.reset || !path_readme.exists() {
mut templ_readme := $tmpl('templates/readme.md')
pathlib.template_write(templ_readme, '${args.path}/readme.md', true)!
}
mut path_templ_dir := pathlib.get_dir(path: args.path + '/templates', create: false)!
if args.reset {
path_templ_dir.delete()!
}
if args.model.templates {
if !path_templ_dir.exists() {
mut templ_6 := $tmpl('templates/atemplate.yaml')
pathlib.template_write(templ_6, '${args.path}/templates/atemplate.yaml', true)!
}
}
}
// fn platform_check(args GenModel) ! {
// ok := 'osx,ubuntu,arch'
// ok2 := ok.split(',')
// for i in args.supported_platforms {
// if i !in ok2 {
// return error('cannot find ${i} in choices for supported_platforms. Valid ones are ${ok}')
// }
// }
// }
// pub fn (args GenModel) platform_check_str() string {
// mut out := ''
// if 'osx' in args.supported_platforms {
// out += 'myplatform == .osx || '
// }
// if 'ubuntu' in args.supported_platforms {
// out += 'myplatform == .ubuntu ||'
// }
// if 'arch' in args.supported_platforms {
// out += 'myplatform == .arch ||'
// }
// out = out.trim_right('|')
// return out
// }

View File

@@ -1,78 +0,0 @@
module generic
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.pathlib
fn generate_exec(path string, reset bool) ! {
mut args := args_get(path)!
console.print_debug('generate code for path: ${path}')
if reset {
args.reset = true
}
mut path_actions := pathlib.get(args.path + '/${args.name}_actions.v')
if args.reset {
path_actions.delete()!
}
if !path_actions.exists() && args.cat == .installer {
console.print_debug('write installer actions')
mut templ_1 := $tmpl('templates/objname_actions.vtemplate')
pathlib.template_write(templ_1, '${args.path}/${args.name}_actions.v', true)!
}
mut templ_2 := $tmpl('templates/objname_factory_.vtemplate')
pathlib.template_write(templ_2, '${args.path}/${args.name}_factory_.v', true)!
mut path_model := pathlib.get(args.path + '/${args.name}_model.v')
if args.reset || !path_model.exists() {
console.print_debug('write model.')
mut templ_3 := $tmpl('templates/objname_model.vtemplate')
pathlib.template_write(templ_3, '${args.path}/${args.name}_model.v', true)!
}
// TODO: check case sensistivity for delete
mut path_readme := pathlib.get(args.path + '/readme.md')
if args.reset || !path_readme.exists() {
mut templ_readme := $tmpl('templates/readme.md')
pathlib.template_write(templ_readme, '${args.path}/readme.md', true)!
}
mut path_templ_dir := pathlib.get_dir(path: args.path + '/templates', create: false)!
if args.reset {
path_templ_dir.delete()!
}
if args.templates {
if !path_templ_dir.exists() {
mut templ_6 := $tmpl('templates/atemplate.yaml')
pathlib.template_write(templ_6, '${args.path}/templates/atemplate.yaml', true)!
}
}
}
fn platform_check(args GeneratorArgs) ! {
ok := 'osx,ubuntu,arch'
ok2 := ok.split(',')
for i in args.supported_platforms {
if i !in ok2 {
return error('cannot find ${i} in choices for supported_platforms. Valid ones are ${ok}')
}
}
}
pub fn (args GeneratorArgs) platform_check_str() string {
mut out := ''
if 'osx' in args.supported_platforms {
out += 'myplatform == .osx || '
}
if 'ubuntu' in args.supported_platforms {
out += 'myplatform == .ubuntu ||'
}
if 'arch' in args.supported_platforms {
out += 'myplatform == .arch ||'
}
out = out.trim_right('|')
return out
}

View File

@@ -4,7 +4,9 @@ import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
pub struct GeneratorArgs {
pub struct GenModel {
pub mut:
name string
classname string
@@ -14,20 +16,27 @@ pub mut:
singleton bool // means there can only be one
templates bool // means we will use templates in the installer, client doesn't do this'
reset bool // regenerate all, dangerous !!!
interactive bool //if we want to ask
startupmanager bool = true
build bool
cat Cat
path string
force bool
hasconfig bool = true
cat Cat = .client
}
pub enum Cat {
unknown
installer
client
}
fn args_get(path string) !GeneratorArgs {
pub fn gen_model_set(args GenModel) ! {
heroscript_templ := $tmpl('templates/heroscript')
pathlib.template_write(heroscript_templ, '${args.path}/templates/.heroscript', true)!
}
pub fn gen_model_get(path string) !GenModel {
console.print_debug('play installer code for path: ${path}')
mut config_path := pathlib.get_file(path: '${path}/.heroscript', create: false)!
@@ -42,7 +51,7 @@ fn args_get(path string) !GeneratorArgs {
if install_actions.len > 0 {
for install_action in install_actions {
mut p := install_action.params
mut args := GeneratorArgs{
mut args := GenModel{
name: p.get('name')!
classname: p.get('classname')!
title: p.get_default('title', '')!
@@ -54,9 +63,7 @@ fn args_get(path string) !GeneratorArgs {
startupmanager: p.get_default_true('startupmanager')
hasconfig: p.get_default_true('hasconfig')
build: p.get_default_false('build')
force: p.get_default_false('force')
cat: .installer
path: path
}
return args
}
@@ -66,7 +73,7 @@ fn args_get(path string) !GeneratorArgs {
if client_actions.len > 0 {
for client_action in client_actions {
mut p := client_action.params
args := GeneratorArgs{
args := GenModel{
name: p.get('name')!
classname: p.get('classname')!
title: p.get_default('title', '')!
@@ -74,11 +81,10 @@ fn args_get(path string) !GeneratorArgs {
singleton: p.get_default_false('singleton')
reset: p.get_default_false('reset')
cat: .client
path: path
}
return args
}
}
return error("can't find hero_code.generate_client or hero_code.generate_installer in ${path}")
// return GeneratorArgs{}
// return GenModel{}
}

View File

@@ -1,4 +1,4 @@
# generation framework
# generation framework for clients & installers
```bash
#will ask questions if .heroscript is not there yet
@@ -22,10 +22,7 @@ there will be a ```.heroscript``` in the director you want to generate for, the
classname:'DaguServer'
singleton:1 //there can only be 1 object in the globals, is called 'default'
templates:1 //are there templates for the installer
default:1 //can we create a default when the factory is used
title:''
supported_platforms:'' //osx, ... (empty means all)
reset:0 // regenerate all, dangerous !!!
startupmanager:1 //managed by a startup manager, default true
build:1 //will we also build the component
@@ -35,8 +32,6 @@ there will be a ```.heroscript``` in the director you want to generate for, the
name:'mail'
classname:'MailClient'
singleton:0 //default is 0
default:1 //can we create a default when the factory is used
reset:0 // regenerate all, dangerous !!!
```

View File

@@ -5,7 +5,7 @@ import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.ui.console
// scan over a set of directories call the play where
pub fn scan(args_ GeneratorArgs) ! {
pub fn scan(args_ GenModel) ! {
mut args := args_
console.print_header('Scan for generation of code for path: ${args.path} (reset:${args.force}, force:${args.force})')

View File

@@ -1,5 +1,5 @@
name: ??{cfg.configpath}
name: ??{model.name}

View File

@@ -0,0 +1,11 @@
!!hero_code.generate_${if model.cat == .installer { "installer" } else { "client" }}
name:"${model.name}"
classname:"${model.classname}"
singleton:${if model.singleton { "1" } else { "0" }}
default:${if model.default { "1" } else { "0" }}
title:\"${model.title}\"
${if model.cat == .installer {
templates:${if model.templates { "1" } else { "0" }}
build:${if model.build { "1" } else { "0" }}"
startupmanager:${if model.startupmanager { "1" } else { "0" }}
}

View File

@@ -1,13 +0,0 @@
!!hero_code.generate_installer
name:'daguserver'
classname:'DaguServer'
singleton:1 //there can only be 1 object in the globals, is called 'default'
default:1 //can we create a default when the factory is used
title:''
supported_platforms:'' //osx, ... (empty means all)
reset:0 // regenerate all, dangerous !!!
//next only relevant for installer
startupmanager:1 //managed by a startup manager, default true
templates:1 //are there templates for the installer
build:1 //will we also build the component

View File

@@ -1,16 +1,16 @@
module ${args.name}
module ${model.name}
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.core.pathlib
@if args.startupmanager
@if model.startupmanager
import freeflowuniverse.herolib.osal.systemd
import freeflowuniverse.herolib.osal.zinit
@end
@if args.build
@if model.build
import freeflowuniverse.herolib.installers.ulist
import freeflowuniverse.herolib.installers.lang.golang
import freeflowuniverse.herolib.installers.lang.rust
@@ -19,14 +19,14 @@ import freeflowuniverse.herolib.installers.lang.python
import os
@if args.startupmanager
@if model.startupmanager
fn startupcmd () ![]zinit.ZProcessNewArgs{
mut installer := get()!
mut res := []zinit.ZProcessNewArgs{}
//THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// res << zinit.ZProcessNewArgs{
// name: '${args.name}'
// cmd: '${args.name} server'
// name: '${model.name}'
// cmd: '${model.name} server'
// env: {
// 'HOME': '/root'
// }
@@ -39,10 +39,10 @@ fn startupcmd () ![]zinit.ZProcessNewArgs{
fn running() !bool {
mut installer := get()!
//THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// this checks health of ${args.name}
// this checks health of ${model.name}
// curl http://localhost:3333/api/v1/s --oauth2-bearer 1234 works
// url:='http://127.0.0.1:??{cfg.port}/api/v1'
// mut conn := httpconnection.new(name: '${args.name}', url: url)!
// mut conn := httpconnection.new(name: '${model.name}', url: url)!
// if cfg.secret.len > 0 {
// conn.default_header.add(.authorization, 'Bearer ??{cfg.secret}')
@@ -54,7 +54,7 @@ fn running() !bool {
// if true{panic("ssss")}
// tags := r['Tags'] or { return false }
// console.print_debug(tags)
// console.print_debug('${args.name} is answering.')
// console.print_debug('${model.name} is answering.')
return false
}
@@ -78,17 +78,17 @@ fn stop_post()!{
//////////////////// following actions are not specific to instance of the object
@if args.cat == .installer
@if model.cat == .installer
// checks if a certain version or above is installed
fn installed() !bool {
//THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// res := os.execute('??{osal.profile_path_source_and()} ${args.name} version')
// res := os.execute('??{osal.profile_path_source_and()} ${model.name} 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 ${args.name} version.\n??{res.output}")
// return error("couldn't parse ${model.name} version.\n??{res.output}")
// }
// if texttools.version(version) == texttools.version(r[0]) {
// return true
@@ -105,24 +105,24 @@ fn ulist_get() !ulist.UList {
//uploads to S3 server if configured
fn upload() ! {
// installers.upload(
// cmdname: '${args.name}'
// source: '??{gitpath}/target/x86_64-unknown-linux-musl/release/${args.name}'
// cmdname: '${model.name}'
// source: '??{gitpath}/target/x86_64-unknown-linux-musl/release/${model.name}'
// )!
}
fn install() ! {
console.print_header('install ${args.name}')
console.print_header('install ${model.name}')
//THIS IS EXAMPLE CODEAND NEEDS TO BE CHANGED
// mut url := ''
// if osal.is_linux_arm() {
// url = 'https://github.com/${args.name}-dev/${args.name}/releases/download/v??{version}/${args.name}_??{version}_linux_arm64.tar.gz'
// url = 'https://github.com/${model.name}-dev/${model.name}/releases/download/v??{version}/${model.name}_??{version}_linux_arm64.tar.gz'
// } else if osal.is_linux_intel() {
// url = 'https://github.com/${args.name}-dev/${args.name}/releases/download/v??{version}/${args.name}_??{version}_linux_amd64.tar.gz'
// url = 'https://github.com/${model.name}-dev/${model.name}/releases/download/v??{version}/${model.name}_??{version}_linux_amd64.tar.gz'
// } else if osal.is_osx_arm() {
// url = 'https://github.com/${args.name}-dev/${args.name}/releases/download/v??{version}/${args.name}_??{version}_darwin_arm64.tar.gz'
// url = 'https://github.com/${model.name}-dev/${model.name}/releases/download/v??{version}/${model.name}_??{version}_darwin_arm64.tar.gz'
// } else if osal.is_osx_intel() {
// url = 'https://github.com/${args.name}-dev/${args.name}/releases/download/v??{version}/${args.name}_??{version}_darwin_amd64.tar.gz'
// url = 'https://github.com/${model.name}-dev/${model.name}/releases/download/v??{version}/${model.name}_??{version}_darwin_amd64.tar.gz'
// } else {
// return error('unsported platform')
// }
@@ -130,21 +130,21 @@ fn install() ! {
// mut dest := osal.download(
// url: url
// minsize_kb: 9000
// expand_dir: '/tmp/${args.name}'
// expand_dir: '/tmp/${model.name}'
// )!
// //dest.moveup_single_subdir()!
// mut binpath := dest.file_get('${args.name}')!
// mut binpath := dest.file_get('${model.name}')!
// osal.cmd_add(
// cmdname: '${args.name}'
// cmdname: '${model.name}'
// source: binpath.path
// )!
}
@if args.build
@if model.build
fn build() ! {
//url := 'https://github.com/threefoldtech/${args.name}'
//url := 'https://github.com/threefoldtech/${model.name}'
// make sure we install base on the node
// if osal.platform() != .ubuntu {
@@ -152,7 +152,7 @@ fn build() ! {
// }
// golang.install()!
// console.print_header('build ${args.name}')
// console.print_header('build ${model.name}')
// gitpath := gittools.get_repo(coderoot: '/tmp/builder', url: url, reset: true, pull: true)!

View File

@@ -1,19 +1,19 @@
module ${args.name}
module ${model.name}
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
@if args.cat == .installer
@if model.cat == .installer
import freeflowuniverse.herolib.sysadmin.startupmanager
import freeflowuniverse.herolib.osal.zinit
import time
@end
__global (
${args.name}_global map[string]&${args.classname}
${args.name}_default string
${model.name}_global map[string]&${model.classname}
${model.name}_default string
)
/////////FACTORY
@@ -24,68 +24,68 @@ pub mut:
name string
}
@if args.hasconfig
@if model.hasconfig
fn args_get (args_ ArgsGet) ArgsGet {
mut args:=args_
if args.name == ""{
args.name = ${args.name}_default
mut model:=args_
if model.name == ""{
model.name = ${model.name}_default
}
if args.name == ""{
args.name = "default"
if model.name == ""{
model.name = "default"
}
return args
return model
}
pub fn get(args_ ArgsGet) !&${args.classname} {
mut args := args_get(args_)
if !(args.name in ${args.name}_global) {
if args.name=="default"{
if ! config_exists(args){
pub fn get(args_ ArgsGet) !&${model.classname} {
mut model := args_get(args_)
if !(model.name in ${model.name}_global) {
if model.name=="default"{
if ! config_exists(model){
if default{
config_save(args)!
config_save(model)!
}
}
config_load(args)!
config_load(model)!
}
}
return ${args.name}_global[args.name] or {
println(${args.name}_global)
panic("could not get config for ${args.name} with name:??{args.name}")
return ${model.name}_global[model.name] or {
println(${model.name}_global)
panic("could not get config for ${model.name} with name:??{model.name}")
}
}
@else
pub fn get(args_ ArgsGet) !&${args.classname} {
return &${args.classname}{}
pub fn get(args_ ArgsGet) !&${model.classname} {
return &${model.classname}{}
}
@end
@if args.hasconfig
@if model.hasconfig
fn config_exists(args_ ArgsGet) bool {
mut args := args_get(args_)
mut model := args_get(args_)
mut context:=base.context() or { panic("bug") }
return context.hero_config_exists("${args.name}",args.name)
return context.hero_config_exists("${model.name}",model.name)
}
fn config_load(args_ ArgsGet) ! {
mut args := args_get(args_)
mut model := args_get(args_)
mut context:=base.context()!
mut heroscript := context.hero_config_get("${args.name}",args.name)!
mut heroscript := context.hero_config_get("${model.name}",model.name)!
play(heroscript:heroscript)!
}
fn config_save(args_ ArgsGet) ! {
mut args := args_get(args_)
mut model := args_get(args_)
mut context:=base.context()!
context.hero_config_set("${args.name}",args.name,heroscript_default()!)!
context.hero_config_set("${model.name}",model.name,heroscript_default()!)!
}
fn set(o ${args.classname})! {
fn set(o ${model.classname})! {
mut o2:=obj_init(o)!
${args.name}_global[o.name] = &o2
${args.name}_default = o.name
${model.name}_global[o.name] = &o2
${model.name}_default = o.name
}
@@ -99,62 +99,62 @@ pub mut:
pub fn play(args_ PlayArgs) ! {
mut args:=args_
mut model:=args_
@if args.hasconfig
if args.heroscript == "" {
args.heroscript = heroscript_default()!
@if model.hasconfig
if model.heroscript == "" {
model.heroscript = heroscript_default()!
}
@end
mut plbook := args.plbook or {
playbook.new(text: args.heroscript)!
mut plbook := model.plbook or {
playbook.new(text: model.heroscript)!
}
@if args.hasconfig
mut install_actions := plbook.find(filter: '${args.name}.configure')!
@if model.hasconfig
mut install_actions := plbook.find(filter: '${model.name}.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
mut p := install_action.params
mycfg:=cfg_play(p)!
console.print_debug("install action ${args.name}.configure\n??{mycfg}")
console.print_debug("install action ${model.name}.configure\n??{mycfg}")
set(mycfg)!
}
}
@end
@if args.cat == .installer
mut other_actions := plbook.find(filter: '${args.name}.')!
@if model.cat == .installer
mut other_actions := plbook.find(filter: '${model.name}.')!
for other_action in other_actions {
if other_action.name in ["destroy","install","build"]{
mut p := other_action.params
reset:=p.get_default_false("reset")
if other_action.name == "destroy" || reset{
console.print_debug("install action ${args.name}.destroy")
console.print_debug("install action ${model.name}.destroy")
destroy()!
}
if other_action.name == "install"{
console.print_debug("install action ${args.name}.install")
console.print_debug("install action ${model.name}.install")
install()!
}
}
@if args.startupmanager
@if model.startupmanager
if other_action.name in ["start","stop","restart"]{
mut p := other_action.params
name := p.get('name')!
mut ${args.name}_obj:=get(name:name)!
console.print_debug("action object:\n??{${args.name}_obj}")
mut ${model.name}_obj:=get(name:name)!
console.print_debug("action object:\n??{${model.name}_obj}")
if other_action.name == "start"{
console.print_debug("install action ${args.name}.??{other_action.name}")
${args.name}_obj.start()!
console.print_debug("install action ${model.name}.??{other_action.name}")
${model.name}_obj.start()!
}
if other_action.name == "stop"{
console.print_debug("install action ${args.name}.??{other_action.name}")
${args.name}_obj.stop()!
console.print_debug("install action ${model.name}.??{other_action.name}")
${model.name}_obj.stop()!
}
if other_action.name == "restart"{
console.print_debug("install action ${args.name}.??{other_action.name}")
${args.name}_obj.restart()!
console.print_debug("install action ${model.name}.??{other_action.name}")
${model.name}_obj.restart()!
}
}
@end
@@ -165,7 +165,7 @@ pub fn play(args_ PlayArgs) ! {
@end
@if args.cat == .installer
@if model.cat == .installer
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////# LIVE CYCLE MANAGEMENT FOR INSTALLERS ///////////////////////////////////
@@ -192,22 +192,22 @@ fn startupmanager_get(cat zinit.StartupManagerType) !startupmanager.StartupManag
}
}
@if args.hasconfig
@if model.hasconfig
//load from disk and make sure is properly intialized
pub fn (mut self ${args.classname}) reload() ! {
pub fn (mut self ${model.classname}) reload() ! {
switch(self.name)
self=obj_init(self)!
}
@end
@if args.startupmanager
pub fn (mut self ${args.classname}) start() ! {
@if model.startupmanager
pub fn (mut self ${model.classname}) start() ! {
switch(self.name)
if self.running()!{
return
}
console.print_header('${args.name} start')
console.print_header('${model.name} start')
if ! installed()!{
install()!
@@ -220,7 +220,7 @@ pub fn (mut self ${args.classname}) start() ! {
for zprocess in startupcmd()!{
mut sm:=startupmanager_get(zprocess.startuptype)!
console.print_debug('starting ${args.name} with ??{zprocess.startuptype}...')
console.print_debug('starting ${model.name} with ??{zprocess.startuptype}...')
sm.new(zprocess)!
@@ -235,17 +235,17 @@ pub fn (mut self ${args.classname}) start() ! {
}
time.sleep(100 * time.millisecond)
}
return error('${args.name} did not install properly.')
return error('${model.name} did not install properly.')
}
pub fn (mut self ${args.classname}) install_start(args InstallArgs) ! {
pub fn (mut self ${model.classname}) install_start(model InstallArgs) ! {
switch(self.name)
self.install(args)!
self.install(model)!
self.start()!
}
pub fn (mut self ${args.classname}) stop() ! {
pub fn (mut self ${model.classname}) stop() ! {
switch(self.name)
stop_pre()!
for zprocess in startupcmd()!{
@@ -255,13 +255,13 @@ pub fn (mut self ${args.classname}) stop() ! {
stop_post()!
}
pub fn (mut self ${args.classname}) restart() ! {
pub fn (mut self ${model.classname}) restart() ! {
switch(self.name)
self.stop()!
self.start()!
}
pub fn (mut self ${args.classname}) running() !bool {
pub fn (mut self ${model.classname}) running() !bool {
switch(self.name)
//walk over the generic processes, if not running return
@@ -282,23 +282,23 @@ pub mut:
reset bool
}
pub fn (mut self ${args.classname}) install(args InstallArgs) ! {
pub fn (mut self ${model.classname}) install(model InstallArgs) ! {
switch(self.name)
if args.reset || (!installed()!) {
if model.reset || (!installed()!) {
install()!
}
}
@if args.build
pub fn (mut self ${args.classname}) build() ! {
@if model.build
pub fn (mut self ${model.classname}) build() ! {
switch(self.name)
build()!
}
@end
pub fn (mut self ${args.classname}) destroy() ! {
pub fn (mut self ${model.classname}) destroy() ! {
switch(self.name)
@if args.startupmanager
@if model.startupmanager
self.stop() or {}
@end
destroy()!
@@ -307,7 +307,7 @@ pub fn (mut self ${args.classname}) destroy() ! {
@end
//switch instance to be used for ${args.name}
//switch instance to be used for ${model.name}
pub fn switch(name string) {
${args.name}_default = name
${model.name}_default = name
}

View File

@@ -1,20 +1,20 @@
module ${args.name}
module ${model.name}
import freeflowuniverse.herolib.data.paramsparser
import os
pub const version = '1.14.3'
const singleton = ${args.singleton}
const default = ${args.default}
const singleton = ${model.singleton}
const default = ${model.default}
@if args.hasconfig
@if model.hasconfig
//TODO: THIS IS EXAMPLE CODE AND NEEDS TO BE CHANGED IN LINE TO STRUCT BELOW, IS STRUCTURED AS HEROSCRIPT
pub fn heroscript_default() !string {
@if args.cat == .installer
@if model.cat == .installer
heroscript:="
!!${args.name}.configure
name:'${args.name}'
homedir: '{HOME}/hero/var/${args.name}'
configpath: '{HOME}/.config/${args.name}/admin.yaml'
!!${model.name}.configure
name:'${model.name}'
homedir: '{HOME}/hero/var/${model.name}'
configpath: '{HOME}/.config/${model.name}/admin.yaml'
username: 'admin'
password: 'secretpassword'
secret: ''
@@ -25,8 +25,8 @@ pub fn heroscript_default() !string {
"
@else
heroscript:="
!!${args.name}.configure
name:'${args.name}'
!!${model.name}.configure
name:'${model.name}'
mail_from: 'info@@example.com'
mail_password: 'secretpassword'
mail_port: 587
@@ -60,12 +60,12 @@ pub fn heroscript_default() !string {
@end
//THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
@if args.cat == .installer
@if model.cat == .installer
@[heap]
pub struct ${args.classname} {
pub struct ${model.classname} {
pub mut:
name string = 'default'
@if args.hasconfig
@if model.hasconfig
homedir string
configpath string
username string
@@ -76,13 +76,13 @@ pub mut:
port int
@end
}
@if args.hasconfig
fn cfg_play(p paramsparser.Params) !${args.classname} {
@if model.hasconfig
fn cfg_play(p paramsparser.Params) !${model.classname} {
//THIS IS EXAMPLE CODE AND NEEDS TO BE CHANGED IN LINE WITH struct above
mut mycfg := ${args.classname}{
mut mycfg := ${model.classname}{
name: p.get_default('name', 'default')!
homedir: p.get_default('homedir', '{HOME}/hero/var/${args.name}')!
configpath: p.get_default('configpath', '{HOME}/hero/var/${args.name}/admin.yaml')!
homedir: p.get_default('homedir', '{HOME}/hero/var/${model.name}')!
configpath: p.get_default('configpath', '{HOME}/hero/var/${model.name}/admin.yaml')!
username: p.get_default('username', 'admin')!
password: p.get_default('password', '')!
secret: p.get_default('secret', '')!
@@ -92,7 +92,7 @@ fn cfg_play(p paramsparser.Params) !${args.classname} {
}
if mycfg.password == '' && mycfg.secret == '' {
return error('password or secret needs to be filled in for ${args.name}')
return error('password or secret needs to be filled in for ${model.name}')
}
return mycfg
}
@@ -101,7 +101,7 @@ fn cfg_play(p paramsparser.Params) !${args.classname} {
@else
@[heap]
pub struct ${args.classname} {
pub struct ${model.classname} {
pub mut:
name string = 'default'
mail_from string
@@ -111,10 +111,10 @@ pub mut:
mail_username string
}
@if args.hasconfig
@if model.hasconfig
fn cfg_play(p paramsparser.Params) ! {
//THIS IS EXAMPLE CODE AND NEEDS TO BE CHANGED IN LINE WITH struct above
mut mycfg := ${args.classname}{
mut mycfg := ${model.classname}{
name: p.get_default('name', 'default')!
mail_from: p.get('mail_from')!
mail_password: p.get('mail_password')!
@@ -128,21 +128,21 @@ fn cfg_play(p paramsparser.Params) ! {
@end
fn obj_init(obj_ ${args.classname})!${args.classname}{
fn obj_init(obj_ ${model.classname})!${model.classname}{
//never call get here, only thing we can do here is work on object itself
mut obj:=obj_
return obj
}
@if args.cat == .installer
@if model.cat == .installer
//called before start if done
fn configure() ! {
@if args.cat == .installer
@if model.cat == .installer
//mut installer := get()!
@else
//mut client := get()!
@end
@if args.templates
@if model.templates
// mut mycode := ??tmpl('templates/atemplate.yaml')
// mut path := pathlib.get_file(path: cfg.configpath, create: true)!
// path.write(mycode)!

View File

@@ -1,34 +1,34 @@
# ${args.name}
# ${model.name}
${args.title}
${model.title}
To get started
```vlang
@if args.cat == .installer
@if model.cat == .installer
import freeflowuniverse.herolib.installers.something.${args.name} as ${args.name}_installer
import freeflowuniverse.herolib.installers.something.${model.name} as ${model.name}_installer
heroscript:="
!!${args.name}.configure name:'test'
!!${model.name}.configure name:'test'
password: '1234'
port: 7701
!!${args.name}.start name:'test' reset:1
!!${model.name}.start name:'test' reset:1
"
${args.name}_installer.play(heroscript=heroscript)!
${model.name}_installer.play(heroscript=heroscript)!
//or we can call the default and do a start with reset
//mut installer:= ${args.name}_installer.get()!
//mut installer:= ${model.name}_installer.get()!
//installer.start(reset:true)!
@else
import freeflowuniverse.herolib.clients. ${args.name}
import freeflowuniverse.herolib.clients. ${model.name}
mut client:= ${args.name}.get()!
mut client:= ${model.name}.get()!
client...
@@ -40,10 +40,10 @@ client...
## example heroscript
@if args.cat == .installer
@if model.cat == .installer
```hero
!!${args.name}.configure
homedir: '/home/user/${args.name}'
!!${model.name}.configure
homedir: '/home/user/${model.name}'
username: 'admin'
password: 'secretpassword'
title: 'Some Title'
@@ -53,7 +53,7 @@ client...
```
@else
```hero
!!${args.name}.configure
!!${model.name}.configure
secret: '...'
host: 'localhost'
port: 8888

View File

@@ -26,10 +26,13 @@ pub fn (mut c UIConsole) ask_yesno(args YesNoArgs) !bool {
cprintln(foreground: .red, text: args.warning + '\n')
}
if question == '' {
question = 'Yes or No, default is Yes.'
question = 'Yes or No, default is Yes (just press enter).'
}
print_debug('${question} (y/n) : ')
choice := os.get_raw_line().trim(' \n').to_lower()
if choice.trim_space() == "" {
return true
}
if choice.starts_with('y') {
return true
}