From da2881f9738651fb793092ea73e2fc4db5f331fd Mon Sep 17 00:00:00 2001 From: mariobassem Date: Sun, 12 Jan 2025 18:31:16 +0200 Subject: [PATCH] refactor: improve rclone client factory - Refactor the rclone client factory to improve its organization and functionality. - Add support for multiple rclone instances with named instances. Co-authored-by: mahmmoud.hassanein --- lib/clients/rclone/.heroscript | 13 ++-- lib/clients/rclone/rclone_factory_.v | 97 +++++++++++++++++++--------- lib/core/playcmds/play_dagu_test.v | 2 +- 3 files changed, 74 insertions(+), 38 deletions(-) diff --git a/lib/clients/rclone/.heroscript b/lib/clients/rclone/.heroscript index c24d5dbf..29957e85 100644 --- a/lib/clients/rclone/.heroscript +++ b/lib/clients/rclone/.heroscript @@ -1,7 +1,8 @@ + !!hero_code.generate_client - name: "rclone" - classname: "RCloneClient" - hasconfig: true - singleton: true - default: true - title: "" + name:'rclone' + classname:'RCloneClient' + singleton:1 + default:1 + hasconfig:1 + reset:0 \ No newline at end of file diff --git a/lib/clients/rclone/rclone_factory_.v b/lib/clients/rclone/rclone_factory_.v index 637e462a..10bf5bd3 100644 --- a/lib/clients/rclone/rclone_factory_.v +++ b/lib/clients/rclone/rclone_factory_.v @@ -2,8 +2,6 @@ module rclone import freeflowuniverse.herolib.core.base import freeflowuniverse.herolib.core.playbook -import freeflowuniverse.herolib.ui.console -import freeflowuniverse.herolib.data.encoderhero __global ( rclone_global map[string]&RCloneClient @@ -12,59 +10,96 @@ __global ( /////////FACTORY -// set the model in mem and the config on the filesystem -pub fn set(o RCloneClient) ! { - mut o2 := obj_init(o)! - rclone_global[o.name] = &o2 - rclone_default = o.name +@[params] +pub struct ArgsGet { +pub mut: + name string = 'default' } -// check we find the config on the filesystem -pub fn exists(args_ ArgsGet) bool { - mut model := args_get(args_) +fn args_get(args_ ArgsGet) ArgsGet { + mut args := args_ + if args.name == '' { + args.name = rclone_default + } + if args.name == '' { + args.name = 'default' + } + return args +} + +pub fn get(args_ ArgsGet) !&RCloneClient { + mut args := args_get(args_) + if args.name !in rclone_global { + if !config_exists() { + if default { + config_save()! + } + } + config_load()! + } + return rclone_global[args.name] or { + println(rclone_global) + panic('bug in get from factory: ') + } +} + +fn config_exists(args_ ArgsGet) bool { + mut args := args_get(args_) mut context := base.context() or { panic('bug') } - return context.hero_config_exists('rclone', model.name) + return context.hero_config_exists('rclone', args.name) } -// load the config error if it doesn't exist -pub fn load(args_ ArgsGet) ! { - mut model := args_get(args_) +fn config_load(args_ ArgsGet) ! { + mut args := args_get(args_) mut context := base.context()! - mut heroscript := context.hero_config_get('rclone', model.name)! + mut heroscript := context.hero_config_get('rclone', args.name)! play(heroscript: heroscript)! } -// save the config to the filesystem in the context -pub fn save(o RCloneClient) ! { +fn config_save(args_ ArgsGet) ! { + mut args := args_get(args_) mut context := base.context()! - heroscript := encoderhero.encode[RCloneClient](o)! - context.hero_config_set('rclone', model.name, heroscript)! + context.hero_config_set('rclone', args.name, heroscript_default()!)! +} + +fn set(o RCloneClient) ! { + mut o2 := obj_init(o)! + rclone_global['default'] = &o2 } @[params] pub struct PlayArgs { pub mut: + name string = 'default' heroscript string // if filled in then plbook will be made out of it plbook ?playbook.PlayBook reset bool + + start bool + stop bool + restart bool + delete bool + configure bool // make sure there is at least one installed } pub fn play(args_ PlayArgs) ! { - mut model := args_ + mut args := args_ - if model.heroscript == '' { - model.heroscript = heroscript_default()! + if args.heroscript == '' { + args.heroscript = heroscript_default()! } - mut plbook := model.plbook or { playbook.new(text: model.heroscript)! } + mut plbook := args.plbook or { playbook.new(text: args.heroscript)! } - mut configure_actions := plbook.find(filter: 'rclone.configure')! - if configure_actions.len > 0 { - for config_action in configure_actions { - mut p := config_action.params - mycfg := cfg_play(p)! - console.print_debug('install action rclone.configure\n${mycfg}') - set(mycfg)! - save(mycfg)! + mut install_actions := plbook.find(filter: 'rclone.configure')! + if install_actions.len > 0 { + for install_action in install_actions { + mut p := install_action.params + cfg_play(p)! } } } + +// switch instance to be used for rclone +pub fn switch(name string) { + rclone_default = name +} diff --git a/lib/core/playcmds/play_dagu_test.v b/lib/core/playcmds/play_dagu_test.v index 5e45be25..65a0ec36 100644 --- a/lib/core/playcmds/play_dagu_test.v +++ b/lib/core/playcmds/play_dagu_test.v @@ -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') }