From c6d176f426738c7282757927b2a516f5fe136e26 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 7 Apr 2022 15:08:27 +0200 Subject: [PATCH] Added patch support to repos cli --- src/git/cli.v | 96 +++++++++++++++++++++++++++++++++++++----------- src/git/client.v | 9 +++++ 2 files changed, 84 insertions(+), 21 deletions(-) diff --git a/src/git/cli.v b/src/git/cli.v index 376592d8..a75f8a02 100644 --- a/src/git/cli.v +++ b/src/git/cli.v @@ -48,10 +48,76 @@ pub fn cmd() cli.Command { remove(conf, cmd.args[0]) ? } }, + cli.Command{ + name: 'edit' + required_args: 1 + usage: 'id' + description: 'Edit the repository that matches the given ID prefix.' + flags: [ + cli.Flag{ + name: 'url' + description: 'URL of the Git repository.' + flag: cli.FlagType.string + }, + cli.Flag{ + name: 'branch' + description: 'Branch of the Git repository.' + flag: cli.FlagType.string + }, + cli.Flag{ + name: 'repo' + description: 'Repo to publish builds to.' + flag: cli.FlagType.string + }, + cli.Flag{ + name: 'arch' + description: 'Comma-separated list of architectures to build on.' + flag: cli.FlagType.string + }, + ] + execute: fn (cmd cli.Command) ? { + config_file := cmd.flags.get_string('config-file') ? + conf := env.load(config_file) ? + + found := cmd.flags.get_all_found() + + mut params := map[string]string{} + + for f in found { + if f.name != 'config-file' { + params[f.name] = f.get_string() ? + } + } + + patch(conf, cmd.args[0], params) ? + } + }, ] } } +fn get_repo_id_by_prefix(conf Config, id_prefix string) ?string { + repos := get_repos(conf.address, conf.api_key) ? + + mut res := []string{} + + for id, _ in repos { + if id.starts_with(id_prefix) { + res << id + } + } + + if res.len == 0 { + eprintln('No repo found for given prefix.') + } + + if res.len > 1 { + eprintln('Multiple repos found for given prefix.') + } + + return res[0] +} + fn list(conf Config) ? { repos := get_repos(conf.address, conf.api_key) ? @@ -67,27 +133,15 @@ fn add(conf Config, url string, branch string, repo string, arch []string) ? { } fn remove(conf Config, id_prefix string) ? { - repos := get_repos(conf.address, conf.api_key) ? - - mut to_remove := []string{} - - for id, _ in repos { - if id.starts_with(id_prefix) { - to_remove << id - } - } - - if to_remove.len == 0 { - eprintln('No repo found for given prefix.') - exit(1) - } - - if to_remove.len > 1 { - eprintln('Multiple repos found for given prefix.') - exit(1) - } - - res := remove_repo(conf.address, conf.api_key, to_remove[0]) ? + id := get_repo_id_by_prefix(conf, id_prefix) ? + res := remove_repo(conf.address, conf.api_key, id) ? + + println(res.message) +} + +fn patch(conf Config, id_prefix string, params map[string]string) ? { + id := get_repo_id_by_prefix(conf, id_prefix) ? + res := patch_repo(conf.address, conf.api_key, id, params) ? println(res.message) } diff --git a/src/git/client.v b/src/git/client.v index 92d27eea..e4a39acd 100644 --- a/src/git/client.v +++ b/src/git/client.v @@ -50,3 +50,12 @@ pub fn remove_repo(address string, api_key string, id string) ?Response return data } + +// 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 { + data := send_request(http.Method.patch, address, '/api/repos/$id', api_key, + params) ? + + return data +}