From b1ac39e2347406600bfed8929b63d89190fffebc Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 1 May 2022 12:44:54 +0200 Subject: [PATCH 1/3] feat: made arch param optional when adding Git repo --- src/git/cli.v | 10 +++++----- src/git/client.v | 8 ++++++-- src/server/git.v | 10 +++++++++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/git/cli.v b/src/git/cli.v index 53527d52..f7f125c9 100644 --- a/src/git/cli.v +++ b/src/git/cli.v @@ -26,14 +26,14 @@ pub fn cmd() cli.Command { }, cli.Command{ name: 'add' - required_args: 4 - usage: 'url branch repo arch...' + required_args: 3 + usage: 'url branch repo' 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], cmd.args[2], cmd.args[3..]) ? + add(conf, cmd.args[0], cmd.args[1], cmd.args[2]) ? } }, cli.Command{ @@ -130,8 +130,8 @@ fn list(conf Config) ? { } // add adds a new repository to the server's list. -fn add(conf Config, url string, branch string, repo string, arch []string) ? { - res := add_repo(conf.address, conf.api_key, url, branch, repo, arch) ? +fn add(conf Config, url string, branch string, repo string) ? { + res := add_repo(conf.address, conf.api_key, url, branch, repo, []) ? println(res.message) } diff --git a/src/git/client.v b/src/git/client.v index a43c9ca0..0ed19b57 100644 --- a/src/git/client.v +++ b/src/git/client.v @@ -35,12 +35,16 @@ pub fn get_repos(address string, api_key string) ?map[string]GitRepo { // add_repo adds a new repo to the server. pub fn add_repo(address string, api_key string, url string, branch string, repo string, arch []string) ?Response { - params := { + mut params := { 'url': url 'branch': branch 'repo': repo - 'arch': arch.join(',') } + + if arch.len > 0 { + params['arch'] = arch.join(',') + } + data := send_request(http.Method.post, address, '/api/repos', api_key, params) ? return data diff --git a/src/server/git.v b/src/server/git.v index 0cba17cb..c136d986 100644 --- a/src/server/git.v +++ b/src/server/git.v @@ -57,7 +57,15 @@ fn (mut app App) post_repo() web.Result { return app.json(http.Status.unauthorized, new_response('Unauthorized.')) } - new_repo := git.repo_from_params(app.query) or { + mut params := app.query.clone() + + // If a repo is created without specifying the arch, we assume it's meant + // for the default architecture. + if 'arch' !in params { + params['arch'] = app.conf.default_arch + } + + new_repo := git.repo_from_params(params) or { return app.json(http.Status.bad_request, new_response(err.msg())) } From 92b8f1fb9321d38498832f4d566a2c1c3803de31 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 1 May 2022 12:52:05 +0200 Subject: [PATCH 2/3] feat(cli): added management of cron schedules --- src/git/cli.v | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/git/cli.v b/src/git/cli.v index f7f125c9..9ad2dc36 100644 --- a/src/git/cli.v +++ b/src/git/cli.v @@ -2,6 +2,7 @@ module git import cli import env +import cron.expression { parse_expression } struct Config { address string [required] @@ -74,6 +75,11 @@ pub fn cmd() cli.Command { description: 'Comma-separated list of architectures to build on.' flag: cli.FlagType.string }, + cli.Flag{ + name: 'schedule' + description: 'Cron schedule for repository.' + flag: cli.FlagType.string + }, ] execute: fn (cmd cli.Command) ? { config_file := cmd.flags.get_string('config-file') ? @@ -125,7 +131,7 @@ 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\t$details.arch') + println('${id[..8]}\t$details.url\t$details.branch\t$details.repo\t$details.arch\t$details.schedule') } } @@ -146,6 +152,14 @@ fn remove(conf Config, id_prefix string) ? { // patch patches a given repository with the provided params. fn patch(conf Config, id_prefix 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 { + parse_expression(params['schedule']) or { + return error('Invalid cron expression: $err.msg()') + } + } + id := get_repo_id_by_prefix(conf, id_prefix) ? res := patch_repo(conf.address, conf.api_key, id, params) ? From d313c5b786d0cab578aacb158eb7a196d39a9db1 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 1 May 2022 13:06:57 +0200 Subject: [PATCH 3/3] feat(cli): added command to show detailed repo info --- src/git/cli.v | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/git/cli.v b/src/git/cli.v index 9ad2dc36..3a1f2e3a 100644 --- a/src/git/cli.v +++ b/src/git/cli.v @@ -49,6 +49,18 @@ pub fn cmd() cli.Command { remove(conf, cmd.args[0]) ? } }, + cli.Command{ + name: 'info' + required_args: 1 + usage: 'id' + description: 'Show detailed information for the repo matching the ID prefix.' + execute: fn (cmd cli.Command) ? { + config_file := cmd.flags.get_string('config-file') ? + conf := env.load(config_file) ? + + info(conf, cmd.args[0]) ? + } + }, cli.Command{ name: 'edit' required_args: 1 @@ -102,16 +114,16 @@ pub fn cmd() cli.Command { } } -// get_repo_id_by_prefix tries to find the repo with the given prefix in its +// 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_id_by_prefix(conf Config, id_prefix string) ?string { +fn get_repo_by_prefix(conf Config, id_prefix string) ?(string, GitRepo) { repos := get_repos(conf.address, conf.api_key) ? - mut res := []string{} + mut res := map[string]GitRepo{} - for id, _ in repos { + for id, repo in repos { if id.starts_with(id_prefix) { - res << id + res[id] = repo } } @@ -123,7 +135,7 @@ fn get_repo_id_by_prefix(conf Config, id_prefix string) ?string { return error('Multiple repos found for given prefix.') } - return res[0] + return res.keys()[0], res[res.keys()[0]] } // list prints out a list of all repositories. @@ -131,7 +143,7 @@ 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\t$details.arch\t$details.schedule') + println('${id[..8]}\t$details.url\t$details.branch\t$details.repo') } } @@ -144,7 +156,7 @@ 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_id_by_prefix(conf, id_prefix) ? + id, _ := get_repo_by_prefix(conf, id_prefix) ? res := remove_repo(conf.address, conf.api_key, id) ? println(res.message) @@ -160,8 +172,20 @@ fn patch(conf Config, id_prefix string, params map[string]string) ? { } } - id := get_repo_id_by_prefix(conf, id_prefix) ? + id, _ := get_repo_by_prefix(conf, id_prefix) ? res := patch_repo(conf.address, conf.api_key, id, params) ? 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) ? + + println('id: $id') + + $for field in GitRepo.fields { + val := repo.$(field.name) + println('$field.name: $val') + } +}