installers

This commit is contained in:
2024-12-25 20:53:37 +01:00
parent 7cbfb504d6
commit d458ccb7d0
52 changed files with 3416 additions and 35 deletions

View File

@@ -0,0 +1,7 @@
!!hero_code.generate_client
name: "postgresql_client"
classname: "PostgresClient"
hasconfig: false
singleton: true
default: true
title: ""

View File

@@ -0,0 +1,56 @@
module postgres
import freeflowuniverse.herolib.core.base
import db.pg
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.ui.console
// pub struct PostgresClient {
// base.BaseConfig
// pub mut:
// config Config
// db pg.DB
// }
// @[params]
// pub struct ClientArgs {
// pub mut:
// instance string @[required]
// // playargs ?play.PlayArgs
// }
// pub fn get(clientargs ClientArgs) !PostgresClient {
// // mut plargs := clientargs.playargs or {
// // // play.PlayArgs
// // // {
// // // }
// // }
// // mut cfg := configurator(clientargs.instance, plargs)!
// // mut args := cfg.get()!
// args.instance = texttools.name_fix(args.instance)
// if args.instance == '' {
// args.instance = 'default'
// }
// // console.print_debug(args)
// mut db := pg.connect(
// host: args.host
// user: args.user
// port: args.port
// password: args.password
// dbname: args.dbname
// )!
// // console.print_debug(postgres_client)
// return PostgresClient{
// instance: args.instance
// db: db
// config: args
// }
// }
struct LocalConfig {
name string
path string
passwd string
}

View File

@@ -0,0 +1,107 @@
module postgres
import db.pg
import freeflowuniverse.herolib.core.texttools
import freeflowuniverse.herolib.osal
import os
import freeflowuniverse.herolib.ui.console
pub fn (mut self PostgresClient[Config]) check() ! {
mut db := self.db
db.exec('SELECT version();') or { return error('can\t select version from database.\n${self}') }
}
pub fn (mut self PostgresClient[Config]) exec(c_ string) ![]pg.Row {
mut db := self.db
mut c := c_
if !(c.trim_space().ends_with(';')) {
c += ';'
}
config := self.config()!
return db.exec(c) or {
return error('can\t execute query on ${config.host}:${config.dbname}.\n${c}\n${err}')
}
}
pub fn (mut self PostgresClient[Config]) db_exists(name_ string) !bool {
mut db := self.db
r := db.exec("SELECT datname FROM pg_database WHERE datname='${name_}';")!
if r.len == 1 {
// console.print_header(' db exists: ${name_}')
return true
}
if r.len > 1 {
return error('should not have more than 1 db with name ${name_}')
}
return false
}
pub fn (mut self PostgresClient[Config]) db_create(name_ string) ! {
name := texttools.name_fix(name_)
mut db := self.db
db_exists := self.db_exists(name_)!
if !db_exists {
console.print_header(' db create: ${name}')
db.exec('CREATE DATABASE ${name};')!
}
db_exists2 := self.db_exists(name_)!
if !db_exists2 {
return error('Could not create db: ${name_}, could not find in DB.')
}
}
pub fn (mut self PostgresClient[Config]) db_delete(name_ string) ! {
mut db := self.db
name := texttools.name_fix(name_)
self.check()!
db_exists := self.db_exists(name_)!
if db_exists {
console.print_header(' db delete: ${name_}')
db.exec('DROP DATABASE ${name};')!
}
db_exists2 := self.db_exists(name_)!
if db_exists2 {
return error('Could not delete db: ${name_}, could not find in DB.')
}
}
pub fn (mut self PostgresClient[Config]) db_names() ![]string {
mut res := []string{}
sqlstr := "SELECT datname FROM pg_database WHERE datistemplate = false and datname != 'postgres' and datname != 'root';"
for row in self.exec(sqlstr)! {
v := row.vals[0] or { '' }
res << v or { '' }
}
return res
}
@[params]
pub struct BackupParams {
pub mut:
dbname string
dest string
}
pub fn (mut self PostgresClient[Config]) backup(args BackupParams) ! {
if args.dest == '' {
return error('specify the destination please')
}
if !os.exists(args.dest) {
os.mkdir_all(args.dest)!
}
if args.dbname == '' {
for dbname in self.db_names()! {
self.backup(dbname: dbname, dest: args.dest)!
}
} else {
config := self.config()!
cmd := '
export PGPASSWORD=\'${config.password}\'
pg_dump -h ${config.host} -p ${config.port} -U ${config.user} --dbname=${args.dbname} --format=c > "${args.dest}/${args.dbname}.bak"
' // console.print_debug(cmd)
osal.exec(cmd: cmd, stdout: true)!
}
}

View File

@@ -0,0 +1,91 @@
module postgres
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.ui
import freeflowuniverse.herolib.ui.console
@[params]
pub struct Config {
pub mut:
instance string = 'default'
user string = 'root'
port int = 5432
host string = 'localhost'
password string
dbname string = 'postgres'
heroscript string
reset bool
}
pub fn configure(instance string, cfg_ Config) !PostgresClient[Config] {
mut config := cfg_
mut server := PostgresClient[Config]{}
server.init('postgres', instance, .set, config)!
return get(instance)!
}
pub fn configure_interactive(args_ Config, mut session base.Session) ! {
mut args := args_
mut myui := ui.new()!
console.clear()
console.print_debug('\n## Configure Postgres Client')
console.print_debug('============================\n\n')
instance := myui.ask_question(
question: 'name for postgres client'
default: args.instance
)!
args.user = myui.ask_question(
question: 'user'
minlen: 3
default: args.user
)!
args.password = myui.ask_question(
question: 'password'
minlen: 3
default: args.password
)!
args.dbname = myui.ask_question(
question: 'dbname'
minlen: 3
default: args.dbname
)!
args.host = myui.ask_question(
question: 'host'
minlen: 3
default: args.host
)!
mut port := myui.ask_question(
question: 'port'
default: '${args.port}'
)!
args.port = port.int()
mut client := PostgresClient[Config]{}
client.init('postgres', instance, .set, args)!
}
// pub fn play_session(mut session base.Session) ! {
// for mut action in session.plbook.find(filter: 'postgresclient.define')! {
// mut p := action.params
// mut args := config()
// panic('implement')
// // args.instance = p.get_default('name','')!
// // if args.instance == ""{
// // args.instance = p.get_default('instance', 'default')!
// // }
// // args.mail_from = p.get('mail_from')!
// // args.smtp_addr = p.get('smtp_addr')!
// // args.smtp_login = p.get('smtp_login')!
// // args.smtp_passwd = p.get('smtp_passwd')!
// // args.smpt_port = p.get_int('smpt_port')!
// // mut c:=configurator(args.instance,session:session)!
// // c.set(args)!
// }
// }

View File

@@ -0,0 +1,29 @@
module postgres
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.ui as gui
import freeflowuniverse.herolib.ui.console
import db.pg
pub struct PostgresClient[T] {
base.BaseConfig[T]
pub mut:
db pg.DB
}
pub fn get(instance string) !PostgresClient[Config] {
mut self := PostgresClient[Config]{}
self.init('postgres', instance, .get)!
config := self.config()!
mut db := pg.connect(
host: config.host
user: config.user
port: config.port
password: config.password
dbname: config.dbname
)!
self.db = db
return self
}

View File

@@ -0,0 +1,32 @@
module postgresql_client
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
import freeflowuniverse.herolib.ui.console
__global (
postgresql_client_global map[string]&PostgresClient
postgresql_client_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet{
pub mut:
name string
}
pub fn get(args_ ArgsGet) !&PostgresClient {
return &PostgresClient{}
}
//switch instance to be used for postgresql_client
pub fn switch(name string) {
postgresql_client_default = name
}

View File

@@ -0,0 +1,33 @@
module postgresql_client
import freeflowuniverse.herolib.data.paramsparser
import os
pub const version = '0.0.0'
const singleton = true
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 PostgresClient {
pub mut:
name string = 'default'
mail_from string
mail_password string @[secret]
mail_port int
mail_server string
mail_username string
}
fn obj_init(obj_ PostgresClient)!PostgresClient{
//never call get here, only thing we can do here is work on object itself
mut obj:=obj_
panic("implement")
return obj
}

View File

@@ -0,0 +1,76 @@
# postgres client
## use hero to work with postgres
```bash
Usage: hero postgres [flags] [commands]
manage postgresql
Flags:
-help Prints help information.
-man Prints the auto-generated manpage.
Commands:
exec execute a query
check check the postgresql connection
configure configure a postgresl connection.
backup backup
print print configure info.
list list databases
```
## configure
the postgres configuration is stored on the filesystem for further use, can be configured as follows
```v
import freeflowuniverse.herolib.clients.postgres
postgres.configure(name:'default',
user :'root'
port : 5432
host : 'localhost'
password : 'ssss'
dbname :'postgres')!
mut db:=postgres.get(name:'default')!
```
## configure through heroscript
```v
import freeflowuniverse.herolib.clients.postgres
heroscript:='
!!postgresclient.define name:'default'
//TO IMPLEMENT
'
postgres.configure(heroscript:heroscript)!
//can also be done through get directly
mut cl:=postgres.get(reset:true,name:'default',heroscript:heroscript)
```
## some postgresql cmds
```v
import freeflowuniverse.herolib.clients.postgres
mut cl:=postgres.get()! //will default get postgres client with name 'default'
cl.db_exists("mydb")!
```
## use the good module of v
- [https://modules.vlang.io/db.pg.html#DB.exec](https://modules.vlang.io/db.pg.html#DB.exec)