feat: removed git.GitRepo type

feat(cli): updated to new GitRepo format
This commit is contained in:
Jef Roosens 2022-05-03 16:16:56 +02:00
parent 0a2488a4df
commit 7419144f97
Signed by untrusted user: Jef Roosens
GPG key ID: B75D4F293C7052DB
7 changed files with 179 additions and 172 deletions

View file

@ -3,6 +3,7 @@ module git
import cli
import env
import cron.expression { parse_expression }
import db { GitRepo, GitRepoArch }
struct Config {
address string [required]
@ -116,34 +117,13 @@ pub fn cmd() cli.Command {
// get_repo_by_prefix tries to find the repo with the given prefix in its
// ID. If multiple or none are found, an error is raised.
fn get_repo_by_prefix(conf Config, id_prefix string) ?(string, GitRepo) {
repos := get_repos(conf.address, conf.api_key) ?
mut res := map[string]GitRepo{}
for id, repo in repos {
if id.starts_with(id_prefix) {
res[id] = repo
}
}
if res.len == 0 {
return error('No repo found for given prefix.')
}
if res.len > 1 {
return error('Multiple repos found for given prefix.')
}
return res.keys()[0], res[res.keys()[0]]
}
// list prints out a list of all repositories.
fn list(conf Config) ? {
repos := get_repos(conf.address, conf.api_key) ?
for id, details in repos {
println('${id[..8]}\t$details.url\t$details.branch\t$details.repo')
for repo in repos {
println('${repo.id}\t$repo.url\t$repo.branch\t$repo.repo')
}
}
@ -155,15 +135,18 @@ fn add(conf Config, url string, branch string, repo string) ? {
}
// remove removes a repository from the server's list.
fn remove(conf Config, id_prefix string) ? {
id, _ := get_repo_by_prefix(conf, id_prefix) ?
res := remove_repo(conf.address, conf.api_key, id) ?
fn remove(conf Config, id string) ? {
// id, _ := get_repo_by_prefix(conf, id_prefix) ?
id_int := id.int()
println(res.message)
if id_int != 0 {
res := remove_repo(conf.address, conf.api_key, id_int) ?
println(res.message)
}
}
// patch patches a given repository with the provided params.
fn patch(conf Config, id_prefix string, params map[string]string) ? {
fn patch(conf Config, id string, params map[string]string) ? {
// We check the cron expression first because it's useless to send an
// invalid one to the server.
if 'schedule' in params && params['schedule'] != '' {
@ -172,20 +155,22 @@ fn patch(conf Config, id_prefix string, params map[string]string) ? {
}
}
id, _ := get_repo_by_prefix(conf, id_prefix) ?
res := patch_repo(conf.address, conf.api_key, id, params) ?
id_int := id.int()
if id_int != 0 {
res := patch_repo(conf.address, conf.api_key, id_int, params) ?
println(res.message)
println(res.message)
}
}
// info shows detailed information for a given repo.
fn info(conf Config, id_prefix string) ? {
id, repo := get_repo_by_prefix(conf, id_prefix) ?
fn info(conf Config, id string) ? {
id_int := id.int()
println('id: $id')
$for field in GitRepo.fields {
val := repo.$(field.name)
println('$field.name: $val')
if id_int == 0 {
return
}
repo := get_repo(conf.address, conf.api_key, id_int) ?
println(repo)
}

View file

@ -3,6 +3,7 @@ module git
import json
import response { Response }
import net.http
import db
// send_request<T> is a convenience method for sending requests to the repos
// API. It mostly does string manipulation to create a query string containing
@ -26,8 +27,15 @@ fn send_request<T>(method http.Method, address string, url string, api_key strin
}
// get_repos returns the current list of repos.
pub fn get_repos(address string, api_key string) ?map[string]GitRepo {
data := send_request<map[string]GitRepo>(http.Method.get, address, '/api/repos', api_key,
pub fn get_repos(address string, api_key string) ?[]db.GitRepo {
data := send_request<[]db.GitRepo>(http.Method.get, address, '/api/repos',
api_key, {}) ?
return data.data
}
pub fn get_repo(address string, api_key string, id int) ?db.GitRepo {
data := send_request<db.GitRepo>(http.Method.get, address, '/api/repos/$id', api_key,
{}) ?
return data.data
@ -51,7 +59,7 @@ pub fn add_repo(address string, api_key string, url string, branch string, repo
}
// remove_repo removes the repo with the given ID from the server.
pub fn remove_repo(address string, api_key string, id string) ?Response<string> {
pub fn remove_repo(address string, api_key string, id int) ?Response<string> {
data := send_request<string>(http.Method.delete, address, '/api/repos/$id', api_key,
{}) ?
@ -60,7 +68,7 @@ pub fn remove_repo(address string, api_key string, id string) ?Response<string>
// patch_repo sends a PATCH request to the given repo with the params as
// payload.
pub fn patch_repo(address string, api_key string, id string, params map[string]string) ?Response<string> {
pub fn patch_repo(address string, api_key string, id int, params map[string]string) ?Response<string> {
data := send_request<string>(http.Method.patch, address, '/api/repos/$id', api_key,
params) ?

View file

@ -1,84 +1,84 @@
module git
import os
import json
/* import os */
/* import json */
pub struct GitRepo {
pub mut:
// URL of the Git repository
url string
// Branch of the Git repository to use
branch string
// On which architectures the package is allowed to be built. In reality,
// this controls which builders will periodically build the image.
arch []string
// Which repo the builder should publish packages to
repo string
// Cron schedule describing how frequently to build the repo.
schedule string [optional]
}
/* pub struct GitRepo { */
/* pub mut: */
/* // URL of the Git repository */
/* url string */
/* // Branch of the Git repository to use */
/* branch string */
/* // On which architectures the package is allowed to be built. In reality, */
/* // this controls which builders will periodically build the image. */
/* arch []string */
/* // Which repo the builder should publish packages to */
/* repo string */
/* // Cron schedule describing how frequently to build the repo. */
/* schedule string [optional] */
/* } */
// patch_from_params patches a GitRepo from a map[string]string, usually
// provided from a web.App's params
pub fn (mut r GitRepo) patch_from_params(params map[string]string) {
$for field in GitRepo.fields {
if field.name in params {
$if field.typ is string {
r.$(field.name) = params[field.name]
// This specific type check is needed for the compiler to ensure
// our types are correct
} $else $if field.typ is []string {
r.$(field.name) = params[field.name].split(',')
}
}
}
}
/* // patch_from_params patches a GitRepo from a map[string]string, usually */
/* // provided from a web.App's params */
/* pub fn (mut r GitRepo) patch_from_params(params map[string]string) { */
/* $for field in GitRepo.fields { */
/* if field.name in params { */
/* $if field.typ is string { */
/* r.$(field.name) = params[field.name] */
/* // This specific type check is needed for the compiler to ensure */
/* // our types are correct */
/* } $else $if field.typ is []string { */
/* r.$(field.name) = params[field.name].split(',') */
/* } */
/* } */
/* } */
/* } */
// read_repos reads the provided path & parses it into a map of GitRepo's.
pub fn read_repos(path string) ?map[string]GitRepo {
if !os.exists(path) {
mut f := os.create(path) ?
/* // read_repos reads the provided path & parses it into a map of GitRepo's. */
/* pub fn read_repos(path string) ?map[string]GitRepo { */
/* if !os.exists(path) { */
/* mut f := os.create(path) ? */
defer {
f.close()
}
/* defer { */
/* f.close() */
/* } */
f.write_string('{}') ?
/* f.write_string('{}') ? */
return {}
}
/* return {} */
/* } */
content := os.read_file(path) ?
res := json.decode(map[string]GitRepo, content) ?
/* content := os.read_file(path) ? */
/* res := json.decode(map[string]GitRepo, content) ? */
return res
}
/* return res */
/* } */
// write_repos writes a map of GitRepo's back to disk given the provided path.
pub fn write_repos(path string, repos &map[string]GitRepo) ? {
mut f := os.create(path) ?
/* // write_repos writes a map of GitRepo's back to disk given the provided path. */
/* pub fn write_repos(path string, repos &map[string]GitRepo) ? { */
/* mut f := os.create(path) ? */
defer {
f.close()
}
/* defer { */
/* f.close() */
/* } */
value := json.encode(repos)
f.write_string(value) ?
}
/* value := json.encode(repos) */
/* f.write_string(value) ? */
/* } */
// repo_from_params creates a GitRepo from a map[string]string, usually
// provided from a web.App's params
pub fn repo_from_params(params map[string]string) ?GitRepo {
mut repo := GitRepo{}
/* // repo_from_params creates a GitRepo from a map[string]string, usually */
/* // provided from a web.App's params */
/* pub fn repo_from_params(params map[string]string) ?GitRepo { */
/* mut repo := GitRepo{} */
// If we're creating a new GitRepo, we want all fields to be present before
// "patching".
$for field in GitRepo.fields {
if field.name !in params && !field.attrs.contains('optional') {
return error('Missing parameter: ${field.name}.')
}
}
repo.patch_from_params(params)
/* // If we're creating a new GitRepo, we want all fields to be present before */
/* // "patching". */
/* $for field in GitRepo.fields { */
/* if field.name !in params && !field.attrs.contains('optional') { */
/* return error('Missing parameter: ${field.name}.') */
/* } */
/* } */
/* repo.patch_from_params(params) */
return repo
}
/* return repo */
/* } */