Started work on better repos cli

main
Jef Roosens 2022-04-06 22:41:19 +02:00
parent d7f6c87053
commit f44ce1c17f
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 41 additions and 33 deletions

View File

@ -3,6 +3,9 @@ module git
import cli import cli
import env import env
import net.http import net.http
import json
import git
import response
struct Config { struct Config {
address string [required] address string [required]
@ -28,13 +31,13 @@ pub fn cmd() cli.Command {
cli.Command{ cli.Command{
name: 'add' name: 'add'
required_args: 2 required_args: 2
usage: 'url branch' usage: 'url branch arch...'
description: 'Add a new repository.' description: 'Add a new repository.'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file') ? config_file := cmd.flags.get_string('config-file') ?
conf := env.load<Config>(config_file) ? conf := env.load<Config>(config_file) ?
add(conf, cmd.args[0], cmd.args[1]) ? add(conf, cmd.args[0], cmd.args[1], cmd.args[2..]) ?
} }
}, },
cli.Command{ cli.Command{
@ -58,12 +61,15 @@ fn list(conf Config) ? {
req.add_custom_header('X-API-Key', conf.api_key) ? req.add_custom_header('X-API-Key', conf.api_key) ?
res := req.do() ? res := req.do() ?
data := json.decode(response.Response<map[string]git.GitRepo>, res.text) ?
println(res.text) for id, details in data.data {
println("${id[..8]}\t$details.url\t$details.branch\t$details.arch")
}
} }
fn add(conf Config, url string, branch string) ? { fn add(conf Config, url string, branch string, arch []string) ? {
mut req := http.new_request(http.Method.post, '$conf.address/api/repos?url=$url&branch=$branch', mut req := http.new_request(http.Method.post, '$conf.address/api/repos?url=$url&branch=$branch&arch=${arch.join(',')}',
'') ? '') ?
req.add_custom_header('X-API-Key', conf.api_key) ? req.add_custom_header('X-API-Key', conf.api_key) ?

27
src/response.v 100644
View File

@ -0,0 +1,27 @@
module response
pub struct Response<T> {
message string
data T
}
pub fn new_response(message string) Response<string> {
return Response<string>{
message: message
data: ''
}
}
pub fn new_data_response<T>(data T) Response<T> {
return Response<T>{
message: ''
data: data
}
}
pub fn new_full_response<T>(message string, data T) Response<T> {
return Response<T>{
message: message
data: data
}
}

View File

@ -4,6 +4,7 @@ import web
import git import git
import net.http import net.http
import rand import rand
import response { new_response, new_data_response }
const repos_file = 'repos.json' const repos_file = 'repos.json'
@ -15,7 +16,7 @@ fn (mut app App) get_repos() web.Result {
repos := rlock app.git_mutex { repos := rlock app.git_mutex {
git.read_repos(app.conf.repos_file) or { git.read_repos(app.conf.repos_file) or {
app.lerror('Failed to read repos file.') app.lerror('Failed to read repos file: $err.msg')
return app.status(http.Status.internal_server_error) return app.status(http.Status.internal_server_error)
} }

View File

@ -1,27 +0,0 @@
module server
struct Response<T> {
message string
data T
}
fn new_response(message string) Response<string> {
return Response<string>{
message: message
data: ''
}
}
fn new_data_response<T>(data T) Response<T> {
return Response<T>{
message: ''
data: data
}
}
fn new_full_response<T>(message string, data T) Response<T> {
return Response<T>{
message: message
data: data
}
}

View File

@ -7,6 +7,7 @@ import time
import rand import rand
import util import util
import net.http import net.http
import response { new_response, new_data_response }
// healthcheck just returns a string, but can be used to quickly check if the // healthcheck just returns a string, but can be used to quickly check if the
// server is still responsive. // server is still responsive.