Merge pull request #18 from freeflowuniverse/development_actions

Development actions
This commit is contained in:
2025-01-13 18:59:22 +02:00
committed by GitHub
26 changed files with 667 additions and 321 deletions

View File

@@ -70,8 +70,8 @@ pub fn (mut self Context) redis() !&redisclient.Redis {
// make sure we are on the right db
r.selectdb(int(self.config.id))!
}
self.redis_ = &r
&r
self.redis_ = r
r
}
return r2

View File

@@ -81,16 +81,25 @@ fn test_logger() {
create: false
)!
println('/tmp/testlogs/${files[0]}')
content := file.read()!.trim_space()
items := logger.search()!
assert items.len == 6 // still wrong: TODO
items_stdout := logger.search(
timestamp_from: ourtime.new('2022-11-1 20:14:35')!
timestamp_to: ourtime.new('2025-11-1 20:14:35')!
logtype: .stdout
)!
assert items_stdout.len == 2
items_error := logger.search(
timestamp_from: ourtime.new('2022-11-1 20:14:35')!
timestamp_to: ourtime.new('2025-11-1 20:14:35')!
logtype: .error
)!
assert items_error.len == 4
}
fn testsuite_end() {
if os.exists('/tmp/testlogs') {
os.rmdir_all('/tmp/testlogs')!
}
// if os.exists('/tmp/testlogs') {
// os.rmdir_all('/tmp/testlogs')!
// }
}

View File

@@ -30,7 +30,6 @@ pub fn (mut l Logger) search(args_ SearchArgs) ![]LogItem {
// Get time range
from_time := timestamp_from.unix()
to_time := timestamp_to.unix()
if from_time > to_time {
return error('from_time cannot be after to_time: ${from_time} < ${to_time}')
}
@@ -82,20 +81,30 @@ pub fn (mut l Logger) search(args_ SearchArgs) ![]LogItem {
continue
}
if collecting && line.len > 14 && line[13] == `-` {
process(mut result, current_item, current_time, args, from_time, to_time)!
collecting = false
}
// Parse log line
is_error := line.starts_with('E')
if !collecting {
// Start new item
current_item = LogItem{
timestamp: current_time
cat: line_trim[2..12].trim_space()
log: line_trim[15..].trim_space()
cat: line[2..12].trim_space()
log: line[15..].trim_space()
logtype: if is_error { .error } else { .stdout }
}
// println('new current item: ${current_item}')
collecting = true
} else {
// Continuation line
current_item.log += '\n' + line_trim[15..]
if line_trim.len < 16 {
current_item.log += '\n'
} else {
current_item.log += '\n' + line[15..]
}
}
}

View File

@@ -29,7 +29,7 @@ pub mut:
pub fn new(args_ PlayBookNewArgs) !PlayBook {
mut args := args_
mut c := base.context()!
mut c := base.context() or { return error('failed to get context: ${err}') }
mut s := c.session_new()!

View File

@@ -27,5 +27,5 @@ const dagu_script = "
fn test_play_dagu() ! {
mut plbook := playbook.new(text: dagu_script)!
play_dagu(mut plbook)!
panic('s')
// panic('s')
}

View File

@@ -1,135 +0,0 @@
module playcmds
import freeflowuniverse.herolib.web.mdbook
import freeflowuniverse.herolib.data.doctree
import freeflowuniverse.herolib.core.playbook
import os
pub fn play_mdbook(mut plbook playbook.PlayBook) ! {
mut buildroot := '${os.home_dir()}/hero/var/mdbuild'
mut publishroot := '${os.home_dir()}/hero/www/info'
mut coderoot := ''
// mut install := false
mut reset := false
mut pull := false
// check if any actions for doctree, if not then nothing to do here
// dtactions := plbook.find(filter: 'doctree.')!
// if dtactions.len == 0 {
// console.print_debug("can't find doctree.add statements, nothing to do")
// return
// }
mut config_actions := plbook.find(filter: 'books:configure')!
if config_actions.len > 1 {
return error('can only have 1 config action for books')
} else if config_actions.len == 1 {
mut p := config_actions[0].params
if p.exists('buildroot') {
buildroot = p.get('buildroot')!
}
if p.exists('coderoot') {
coderoot = p.get('coderoot')!
}
if p.exists('publishroot') {
publishroot = p.get('publishroot')!
}
if p.exists('reset') {
reset = p.get_default_false('reset')
}
config_actions[0].done = true
}
mut trees := map[string]&doctree.Tree{}
for mut action in plbook.find(filter: 'doctree:new')! {
mut p := action.params
name := p.get('name')!
fail_on_error := p.get_default_false('fail_on_error')
if name in trees {
return error('tree with name ${name} already exists')
}
tree := doctree.new(name: name, fail_on_error: fail_on_error)!
trees[name] = tree
}
for mut action in plbook.find(filter: 'doctree:add')! {
mut p := action.params
url := p.get_default('url', '')!
path := p.get_default('path', '')!
name := p.get_default('name', '')!
if trees.len == 0 {
return error('no tree found')
}
mut tree := if name != '' {
trees[name] or { return error('tree ${name} not found') }
} else {
trees.values()[0]
}
tree.scan(
path: path
git_url: url
git_reset: reset
git_root: coderoot
git_pull: pull
)!
action.done = true
}
for mut action in plbook.find(filter: 'doctree:export')! {
mut p := action.params
build_path := p.get('path')!
toreplace := p.get_default('replace', '')!
reset2 := p.get_default_false('reset')
name := p.get('name')!
mut tree := trees[name] or { return error('tree: ${name} not found') }
tree.export(
destination: build_path
reset: reset2
toreplace: toreplace
)!
action.done = true
}
for mut action in plbook.find(filter: 'mdbook:export')! {
mut p := action.params
name := p.get('name')!
summary_url := p.get_default('summary_url', '')!
summary_path := p.get_default('summary_path', '')!
title := p.get_default('title', name)!
publish_path := p.get_default('publish_path', '${publishroot}/${name}')!
build_path := p.get_default('build_path', '${buildroot}/${name}')!
printbook := p.get_default_false('printbook')
foldlevel := p.get_int_default('foldlevel', 0)!
production := p.get_default_false('production')
reset3 := p.get_default_true('reset')
collections := p.get_list_default('collections', [])!
if summary_url == '' && summary_path == '' {
return error('Both summary url and path cannot be empty at the same time')
}
mut mdbooks := mdbook.get()!
mdbooks.path_build = buildroot
mdbooks.path_publish = publishroot
mdbooks.generate(
name: name
title: title
summary_path: summary_path
publish_path: publish_path
build_path: build_path
printbook: printbook
foldlevel: foldlevel
production: production
collections: collections
)!
action.done = true
}
}

View File

@@ -1,85 +0,0 @@
module playcmds
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.core.playcmds
import freeflowuniverse.herolib.core.pathlib
fn test_play_mdbook() {
mut summary_path := pathlib.get_file(path: '/tmp/mdbook_test/SUMMARY.md', create: true)!
summar_content := '
- [Page number 1](fruits/apple.md)
- [fruit intro](fruits/intro.md)
- [rpc page](rpc/tfchain.md)
- [vegies](test_vegetables/tomato.md)
'
summary_path.write(summar_content)!
mut p := pathlib.get_file(path: '/tmp/heroscript/do.hero', create: true)!
// script := "
// !!doctree.new
// name: 'tree1'
// !!doctree.add
// name: 'tree1'
// url:'https://github.com/freeflowuniverse/herolib/tree/development_doctree4/herolib/data/doctree/testdata/actions'
// !!doctree.add
// name: 'tree1'
// url: 'https://github.com/freeflowuniverse/herolib/tree/development_doctree4/herolib/data/doctree/testdata/fruits'
// !!doctree.add
// name: 'tree1'
// url: 'https://github.com/freeflowuniverse/herolib/tree/development_doctree4/herolib/data/doctree/testdata/rpc'
// !!doctree.export
// name: 'tree1'
// path: '/tmp/export_tree1'
// !!doctree.new
// name: 'tree2'
// fail_on_error: true
// !!doctree.add
// name: 'tree2'
// url: 'https://github.com/freeflowuniverse/herolib/tree/development_doctree4/herolib/data/doctree/testdata/vegetables'
// !!doctree.export
// name: 'tree2'
// path: '/tmp/export_tree2'
// !!mdbook.export
// title:'ThreeFold Technology'
// name:'tech'
// summary_path:'${summary_path.path}'
// collections:'/tmp/export_tree1,/tmp/export_tree2'
// dest: '/tmp/mdbook_export'
// production:0 //means we put it in summary
// "
s2 := "
!!doctree.new
name: 'info_tfgrid'
fail_on_error: false
!!doctree.add
name:'info_tfgrid'
url:'https://git.ourworld.tf/tfgrid/info_tfgrid/src/branch/main/collections'
!!doctree.export
name:'info_tfgrid'
path:'~/hero/var/collections/info_tfgrid'
!!mdbook.export
title:'ThreeFold Technology'
name:'tech'
summary_url:'https://git.ourworld.tf/tfgrid/info_tfgrid/src/branch/development/books/tech/SUMMARY.md'
collections:'~/hero/var/collections/info_tfgrid'
production:0 //means we put it in summary
"
p.write(s2)!
mut plbook := playbook.new(path: '/tmp/heroscript')!
playcmds.play_mdbook(mut plbook)!
}

View File

@@ -0,0 +1,8 @@
module playcmds
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.hero.publishing
pub fn play_publisher(mut plbook playbook.PlayBook) ! {
publishing.play(mut plbook)!
}

View File

@@ -0,0 +1,34 @@
module playcmds
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.core.playcmds
import freeflowuniverse.herolib.core.pathlib
import os
fn test_play_publisher() {
mut p := pathlib.get_file(path: '/tmp/heroscript/do.hero', create: true)!
s2 := "
!!publisher.new_collection
url:'https://git.ourworld.tf/tfgrid/info_tfgrid/src/branch/main/collections'
reset: false
pull: true
!!book.define
name:'info_tfgrid'
summary_url:'https://git.ourworld.tf/tfgrid/info_tfgrid/src/branch/development/books/tech/SUMMARY.md'
title:'ThreeFold Technology'
collections: 'about,dashboard,farmers,library,partners_utilization,tech,p2p'
!!book.publish
name:'tech'
production: false
"
p.write(s2)!
mut plbook := playbook.new(path: '/tmp/heroscript')!
playcmds.play_publisher(mut plbook)!
}

View File

@@ -3,11 +3,11 @@ module redisclient
// original code see https://github.com/patrickpissurno/vredis/blob/master/vredis_test.v
// credits see there as well (-:
import net
// import sync
import sync
// import strconv
__global (
redis_connections []Redis
redis_connections []&Redis
)
const default_read_timeout = net.infinite_timeout
@@ -18,15 +18,16 @@ pub:
addr string
mut:
socket net.TcpConn
mtx sync.RwMutex
}
// https://redis.io/topics/protocol
// examples:
// localhost:6379
// /tmp/redis-default.sock
pub fn new(addr string) !Redis {
// lock redis_connections {
for mut conn in redis_connections {
pub fn new(addr string) !&Redis {
// lock redis_cowritennections {
for conn in redis_connections {
if conn.addr == addr {
return conn
}
@@ -34,10 +35,13 @@ pub fn new(addr string) !Redis {
// means there is no connection yet
mut r := Redis{
addr: addr
mtx: sync.RwMutex{}
}
r.mtx.init()
r.socket_connect()!
redis_connections << r
return r
redis_connections << &r
return &r
//}
// panic("bug")
}
@@ -47,7 +51,7 @@ pub fn reset() ! {
for mut conn in redis_connections {
conn.disconnect()
}
redis_connections = []Redis{}
redis_connections = []&Redis{}
//}
}

View File

@@ -18,7 +18,7 @@ pub fn get_redis_url(url string) !RedisURL {
}
}
pub fn core_get(url RedisURL) !Redis {
pub fn core_get(url RedisURL) !&Redis {
mut r := new('${url.address}:${url.port}')!
return r
}

View File

@@ -2,7 +2,7 @@ module redisclient
import freeflowuniverse.herolib.data.resp
pub fn (mut r Redis) get_response() !resp.RValue {
fn (mut r Redis) get_response() !resp.RValue {
line := r.read_line()!
if line.starts_with('-') {
@@ -63,7 +63,7 @@ pub fn (mut r Redis) get_response() !resp.RValue {
// TODO: needs to use the resp library
pub fn (mut r Redis) get_int() !int {
fn (mut r Redis) get_int() !int {
line := r.read_line()!
if line.starts_with(':') {
return line[1..].int()
@@ -72,7 +72,7 @@ pub fn (mut r Redis) get_int() !int {
}
}
pub fn (mut r Redis) get_list_int() ![]int {
fn (mut r Redis) get_list_int() ![]int {
line := r.read_line()!
mut res := []int{}
@@ -89,7 +89,7 @@ pub fn (mut r Redis) get_list_int() ![]int {
}
}
pub fn (mut r Redis) get_list_str() ![]string {
fn (mut r Redis) get_list_str() ![]string {
line := r.read_line()!
mut res := []string{}
@@ -106,7 +106,7 @@ pub fn (mut r Redis) get_list_str() ![]string {
}
}
pub fn (mut r Redis) get_string() !string {
fn (mut r Redis) get_string() !string {
line := r.read_line()!
if line.starts_with('+') {
// console.print_debug("getstring:'${line[1..]}'")
@@ -120,12 +120,12 @@ pub fn (mut r Redis) get_string() !string {
}
}
pub fn (mut r Redis) get_string_nil() !string {
fn (mut r Redis) get_string_nil() !string {
r2 := r.get_bytes_nil()!
return r2.bytestr()
}
pub fn (mut r Redis) get_bytes_nil() ![]u8 {
fn (mut r Redis) get_bytes_nil() ![]u8 {
line := r.read_line()!
if line.starts_with('+') {
return line[1..].bytes()
@@ -140,12 +140,12 @@ pub fn (mut r Redis) get_bytes_nil() ![]u8 {
}
}
pub fn (mut r Redis) get_bool() !bool {
fn (mut r Redis) get_bool() !bool {
i := r.get_int()!
return i == 1
}
pub fn (mut r Redis) get_bytes() ![]u8 {
fn (mut r Redis) get_bytes() ![]u8 {
line := r.read_line()!
if line.starts_with('$') {
return r.get_bytes_from_line(line)

View File

@@ -53,7 +53,7 @@ fn (mut r Redis) socket_check() ! {
}
}
pub fn (mut r Redis) read_line() !string {
fn (mut r Redis) read_line() !string {
return r.socket.read_line().trim_right('\r\n')
}
@@ -99,7 +99,7 @@ fn (mut r Redis) write_line(data []u8) ! {
}
// write resp value to the redis channel
pub fn (mut r Redis) write_rval(val resp.RValue) ! {
fn (mut r Redis) write_rval(val resp.RValue) ! {
r.write(val.encode())!
}

View File

@@ -3,7 +3,7 @@ import freeflowuniverse.herolib.core.redisclient
fn setup() !&redisclient.Redis {
mut redis := redisclient.core_get()!
redis.selectdb(10) or { panic(err) }
return &redis
return redis
}
fn cleanup(mut redis redisclient.Redis) ! {

View File

@@ -5,6 +5,10 @@ import freeflowuniverse.herolib.ui.console
// send list of strings, expect OK back
pub fn (mut r Redis) send_expect_ok(items []string) ! {
r.mtx.lock()
defer {
r.mtx.unlock()
}
r.write_cmds(items)!
res := r.get_string()!
if res != 'OK' {
@@ -15,23 +19,39 @@ pub fn (mut r Redis) send_expect_ok(items []string) ! {
// send list of strings, expect int back
pub fn (mut r Redis) send_expect_int(items []string) !int {
r.mtx.lock()
defer {
r.mtx.unlock()
}
r.write_cmds(items)!
return r.get_int()
}
pub fn (mut r Redis) send_expect_bool(items []string) !bool {
r.mtx.lock()
defer {
r.mtx.unlock()
}
r.write_cmds(items)!
return r.get_bool()
}
// send list of strings, expect string back
pub fn (mut r Redis) send_expect_str(items []string) !string {
r.mtx.lock()
defer {
r.mtx.unlock()
}
r.write_cmds(items)!
return r.get_string()
}
// send list of strings, expect string or nil back
pub fn (mut r Redis) send_expect_strnil(items []string) !string {
r.mtx.lock()
defer {
r.mtx.unlock()
}
r.write_cmds(items)!
d := r.get_string_nil()!
return d
@@ -39,16 +59,28 @@ pub fn (mut r Redis) send_expect_strnil(items []string) !string {
// send list of strings, expect list of strings back
pub fn (mut r Redis) send_expect_list_str(items []string) ![]string {
r.mtx.lock()
defer {
r.mtx.unlock()
}
r.write_cmds(items)!
return r.get_list_str()
}
pub fn (mut r Redis) send_expect_list_int(items []string) ![]int {
r.mtx.lock()
defer {
r.mtx.unlock()
}
r.write_cmds(items)!
return r.get_list_int()
}
pub fn (mut r Redis) send_expect_list(items []string) ![]resp.RValue {
r.mtx.lock()
defer {
r.mtx.unlock()
}
r.write_cmds(items)!
res := r.get_response()!
return resp.get_redis_array(res)

View File

@@ -8,7 +8,7 @@ fn setup() !&redisclient.Redis {
mut redis := redisclient.core_get()!
// Select db 10 to be away from default one '0'
redis.selectdb(10) or { panic(err) }
return &redis
return redis
}
fn cleanup(mut redis redisclient.Redis) ! {