From f44ce1c17f572bb788e2002c877fc0678d71344c Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Wed, 6 Apr 2022 22:41:19 +0200 Subject: [PATCH] Started work on better repos cli --- src/git/cli.v | 16 +++++++++++----- src/response.v | 27 +++++++++++++++++++++++++++ src/server/git.v | 3 ++- src/server/response.v | 27 --------------------------- src/server/routes.v | 1 + 5 files changed, 41 insertions(+), 33 deletions(-) create mode 100644 src/response.v delete mode 100644 src/server/response.v diff --git a/src/git/cli.v b/src/git/cli.v index 17fa984..586b5ba 100644 --- a/src/git/cli.v +++ b/src/git/cli.v @@ -3,6 +3,9 @@ module git import cli import env import net.http +import json +import git +import response struct Config { address string [required] @@ -28,13 +31,13 @@ pub fn cmd() cli.Command { cli.Command{ name: 'add' required_args: 2 - usage: 'url branch' + usage: 'url branch arch...' description: 'Add a new repository.' execute: fn (cmd cli.Command) ? { config_file := cmd.flags.get_string('config-file') ? conf := env.load(config_file) ? - add(conf, cmd.args[0], cmd.args[1]) ? + add(conf, cmd.args[0], cmd.args[1], cmd.args[2..]) ? } }, cli.Command{ @@ -58,12 +61,15 @@ fn list(conf Config) ? { req.add_custom_header('X-API-Key', conf.api_key) ? res := req.do() ? + data := json.decode(response.Response, 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) ? { - mut req := http.new_request(http.Method.post, '$conf.address/api/repos?url=$url&branch=$branch', +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&arch=${arch.join(',')}', '') ? req.add_custom_header('X-API-Key', conf.api_key) ? diff --git a/src/response.v b/src/response.v new file mode 100644 index 0000000..1618fcf --- /dev/null +++ b/src/response.v @@ -0,0 +1,27 @@ +module response + +pub struct Response { + message string + data T +} + +pub fn new_response(message string) Response { + return Response{ + message: message + data: '' + } +} + +pub fn new_data_response(data T) Response { + return Response{ + message: '' + data: data + } +} + +pub fn new_full_response(message string, data T) Response { + return Response{ + message: message + data: data + } +} diff --git a/src/server/git.v b/src/server/git.v index 7d177d7..359d6ce 100644 --- a/src/server/git.v +++ b/src/server/git.v @@ -4,6 +4,7 @@ import web import git import net.http import rand +import response { new_response, new_data_response } const repos_file = 'repos.json' @@ -15,7 +16,7 @@ fn (mut app App) get_repos() web.Result { repos := rlock app.git_mutex { 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) } diff --git a/src/server/response.v b/src/server/response.v deleted file mode 100644 index 354e7da..0000000 --- a/src/server/response.v +++ /dev/null @@ -1,27 +0,0 @@ -module server - -struct Response { - message string - data T -} - -fn new_response(message string) Response { - return Response{ - message: message - data: '' - } -} - -fn new_data_response(data T) Response { - return Response{ - message: '' - data: data - } -} - -fn new_full_response(message string, data T) Response { - return Response{ - message: message - data: data - } -} diff --git a/src/server/routes.v b/src/server/routes.v index 0e7d72c..71da02a 100644 --- a/src/server/routes.v +++ b/src/server/routes.v @@ -7,6 +7,7 @@ import time import rand import util import net.http +import response { new_response, new_data_response } // healthcheck just returns a string, but can be used to quickly check if the // server is still responsive.