This commit is contained in:
2025-08-13 09:48:56 +02:00
parent 55f0621983
commit 0845feffac
9 changed files with 182 additions and 89 deletions

View File

@@ -0,0 +1,8 @@
!!hero_code.generate_client
name:'gitea_client'
classname:'GiteaClient'
singleton:0
default:1
hasconfig:1
reset:0

View File

@@ -0,0 +1,102 @@
module gitea_client
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook { PlayBook }
import freeflowuniverse.herolib.ui.console
__global (
gitea_client_global map[string]&GiteaClient
gitea_client_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string
}
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_)
mut obj := GiteaClient{
name: args.name
}
if args.name !in gitea_client_global {
if !exists(args)! {
set(obj)!
} else {
heroscript := context.hero_config_get('gitea_client', args.name)!
mut obj_ := heroscript_loads(heroscript)!
set_in_mem(obj_)!
}
}
return gitea_client_global[args.name] or {
println(gitea_client_global)
// bug if we get here because should be in globals
panic('could not get config for gitea_client with name, is bug:${args.name}')
}
}
// register the config for the future
pub fn set(o GiteaClient) ! {
set_in_mem(o)!
mut context := base.context()!
heroscript := heroscript_dumps(o)!
context.hero_config_set('gitea_client', 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('gitea_client', args.name)
}
pub fn delete(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_delete('gitea_client', args.name)!
if args.name in gitea_client_global {
// del gitea_client_global[args.name]
}
}
// only sets in mem, does not set as config
fn set_in_mem(o GiteaClient) ! {
mut o2 := obj_init(o)!
gitea_client_global[o.name] = &o2
gitea_client_default = o.name
}
pub fn play(mut plbook PlayBook) ! {
mut install_actions := plbook.find(filter: 'gitea_client.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
heroscript := install_action.heroscript()
mut obj2 := heroscript_loads(heroscript)!
set(obj2)!
}
}
}
// switch instance to be used for gitea_client
pub fn switch(name string) {
gitea_client_default = name
}
// helpers
@[params]
pub struct DefaultConfigArgs {
instance string = 'default'
}

View File

@@ -0,0 +1,42 @@
module gitea_client
import freeflowuniverse.herolib.data.paramsparser
import freeflowuniverse.herolib.data.encoderhero
import os
pub const version = '0.0.0'
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 GiteaClient {
pub mut:
name string = 'default'
url string = "https://git.ourworld.tf"
key string
}
// your checking & initialization code if needed
fn obj_init(mycfg_ GiteaClient) !GiteaClient {
mut mycfg := mycfg_
if mycfg.url == '' {
return error('url needs to be filled in for ${mycfg.name}')
}
if mycfg.key == '' {
return error('key needs to be filled in for ${mycfg.name}')
}
return mycfg
}
/////////////NORMALLY NO NEED TO TOUCH
pub fn heroscript_dumps(obj GiteaClient) !string {
return encoderhero.encode[GiteaClient](obj)!
}
pub fn heroscript_loads(heroscript string) !GiteaClient {
mut obj := encoderhero.decode[GiteaClient](heroscript)!
return obj
}

View File

@@ -0,0 +1,69 @@
module gitea_client
import json
import net.http
// Repository operations
pub fn (mut client GiteaClient) create_repo(name string, description string, private bool) !string {
data := {
'name': name
'description': description
'private': private.str()
}
resp := client.connection.post('/api/v1/user/repos', json.encode(data))!
return resp
}
pub fn (mut client GiteaClient) get_repo(owner string, repo string) !string {
resp := client.connection.get('/api/v1/repos/${owner}/${repo}')!
return resp
}
pub fn (mut client GiteaClient) list_repos() !string {
resp := client.connection.get('/api/v1/user/repos')!
return resp
}
// User operations
pub fn (mut client GiteaClient) get_user() !string {
resp := client.connection.get('/api/v1/user')!
return resp
}
pub fn (mut client GiteaClient) list_users() !string {
resp := client.connection.get('/api/v1/admin/users')!
return resp
}
// Organization operations
pub fn (mut client GiteaClient) create_org(name string, description string) !string {
data := {
'username': name
'description': description
}
resp := client.connection.post('/api/v1/orgs', json.encode(data))!
return resp
}
pub fn (mut client GiteaClient) list_orgs() !string {
resp := client.connection.get('/api/v1/orgs')!
return resp
}
// Issue operations
pub fn (mut client GiteaClient) create_issue(owner string, repo string, title string, body string) !string {
data := {
'title': title
'body': body
}
resp := client.connection.post('/api/v1/repos/${owner}/${repo}/issues', json.encode(data))!
return resp
}
pub fn (mut client GiteaClient) list_issues(owner string, repo string) !string {
resp := client.connection.get('/api/v1/repos/${owner}/${repo}/issues')!
return resp
}

View File

@@ -0,0 +1,29 @@
# gitea_client
To get started
```vlang
import freeflowuniverse.herolib.clients.gitea_client
mut client:= gitea_client.get()!
client...
```
## example heroscript
```hero
!!gitea_client.configure
url: 'https://git.ourworld.tf/'
key: '...'
```