From fc1d4480dc220e6f6245b6d6add8a24550f39493 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 7 Apr 2022 12:07:56 +0200 Subject: [PATCH 1/2] Split Git client code into separate module --- src/git/cli.v | 33 ++++++--------------------------- src/git/client.v | 36 ++++++++++++++++++++++++++++++++++++ src/response.v | 1 + 3 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 src/git/client.v diff --git a/src/git/cli.v b/src/git/cli.v index 7e41b1c..4a066d5 100644 --- a/src/git/cli.v +++ b/src/git/cli.v @@ -2,9 +2,6 @@ module git import cli import env -import net.http -import json -import response struct Config { address string [required] @@ -55,18 +52,8 @@ pub fn cmd() cli.Command { } } -fn get_repos(conf Config) ?map[string]GitRepo { - mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ? - req.add_custom_header('X-API-Key', conf.api_key) ? - - res := req.do() ? - data := json.decode(response.Response, res.text) ? - - return data.data -} - fn list(conf Config) ? { - repos := get_repos(conf) ? + repos := get_repos(conf.address, conf.api_key) ? for id, details in repos { println('${id[..8]}\t$details.url\t$details.branch\t$details.arch') @@ -74,17 +61,13 @@ fn list(conf Config) ? { } 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) ? + res := add_repo(conf.address, conf.api_key, url, branch, arch) ? - res := req.do() ? - - println(res.text) + println(res.message) } fn remove(conf Config, id_prefix string) ? { - repos := get_repos(conf) ? + repos := get_repos(conf.address, conf.api_key) ? mut to_remove := []string{} @@ -104,11 +87,7 @@ fn remove(conf Config, id_prefix string) ? { exit(1) } - mut req := http.new_request(http.Method.delete, '$conf.address/api/repos/${to_remove[0]}', - '') ? - req.add_custom_header('X-API-Key', conf.api_key) ? + res := remove_repo(conf.address, conf.api_key, to_remove[0]) ? - res := req.do() ? - - println(res.text) + println(res.message) } diff --git a/src/git/client.v b/src/git/client.v new file mode 100644 index 0000000..3da1a61 --- /dev/null +++ b/src/git/client.v @@ -0,0 +1,36 @@ +module git + +import json +import response { Response } +import net.http + +fn get_repos(address string, api_key string) ?map[string]GitRepo { + mut req := http.new_request(http.Method.get, '$address/api/repos', '') ? + req.add_custom_header('X-API-Key', api_key) ? + + res := req.do() ? + data := json.decode(Response, res.text) ? + + return data.data +} + +fn add_repo(address string, api_key string, url string, branch string, arch []string) ?Response { + mut req := http.new_request(http.Method.post, '$address/api/repos?url=$url&branch=$branch&arch=${arch.join(',')}', + '') ? + req.add_custom_header('X-API-Key', api_key) ? + + res := req.do() ? + data := json.decode(Response, res.text) ? + + return data +} + +fn remove_repo(address string, api_key string, id string) ?Response { + mut req := http.new_request(http.Method.delete, '$address/api/repos/$id', '') ? + req.add_custom_header('X-API-Key', api_key) ? + + res := req.do() ? + data := json.decode(Response, res.text) ? + + return data +} diff --git a/src/response.v b/src/response.v index 7e268b1..a06a589 100644 --- a/src/response.v +++ b/src/response.v @@ -1,6 +1,7 @@ module response pub struct Response { +pub: message string data T } From 78310fa1e425652fb0f2f2e29865a6f2b9310cf0 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 7 Apr 2022 12:10:37 +0200 Subject: [PATCH 2/2] Builder now uses new Git repos API --- src/build/build.v | 10 ++-------- src/git/client.v | 9 ++++++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/build/build.v b/src/build/build.v index 934627f..c42c98d 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -3,9 +3,7 @@ module build import docker import encoding.base64 import time -import net.http import git -import json const container_build_dir = '/build' @@ -63,11 +61,7 @@ fn create_build_image() ?string { fn build(conf Config) ? { // We get the repos list from the Vieter instance - mut req := http.new_request(http.Method.get, '$conf.address/api/repos', '') ? - req.add_custom_header('X-Api-Key', conf.api_key) ? - - res := req.do() ? - repos := json.decode([]git.GitRepo, res.text) ? + repos := git.get_repos(conf.address, conf.api_key) ? // No point in doing work if there's no repos present if repos.len == 0 { @@ -77,7 +71,7 @@ fn build(conf Config) ? { // First, we create a base image which has updated repos n stuff image_id := create_build_image() ? - for repo in repos { + for _, repo in repos { // TODO what to do with PKGBUILDs that build multiple packages? commands := [ 'git clone --single-branch --depth 1 --branch $repo.branch $repo.url repo', diff --git a/src/git/client.v b/src/git/client.v index 3da1a61..97fe9fb 100644 --- a/src/git/client.v +++ b/src/git/client.v @@ -4,7 +4,8 @@ import json import response { Response } import net.http -fn get_repos(address string, api_key string) ?map[string]GitRepo { +// get_repos returns the current list of repos. +pub fn get_repos(address string, api_key string) ?map[string]GitRepo { mut req := http.new_request(http.Method.get, '$address/api/repos', '') ? req.add_custom_header('X-API-Key', api_key) ? @@ -14,7 +15,8 @@ fn get_repos(address string, api_key string) ?map[string]GitRepo { return data.data } -fn add_repo(address string, api_key string, url string, branch string, arch []string) ?Response { +// add_repo adds a new repo to the server. +pub fn add_repo(address string, api_key string, url string, branch string, arch []string) ?Response { mut req := http.new_request(http.Method.post, '$address/api/repos?url=$url&branch=$branch&arch=${arch.join(',')}', '') ? req.add_custom_header('X-API-Key', api_key) ? @@ -25,7 +27,8 @@ fn add_repo(address string, api_key string, url string, branch string, arch []st return data } -fn remove_repo(address string, api_key string, id string) ?Response { +// remove_repo removes the repo with the given ID from the server. +pub fn remove_repo(address string, api_key string, id string) ?Response { mut req := http.new_request(http.Method.delete, '$address/api/repos/$id', '') ? req.add_custom_header('X-API-Key', api_key) ?