Added patch support to repos cli

main
Jef Roosens 2022-04-07 15:08:27 +02:00
parent ab72c800c3
commit c6d176f426
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 84 additions and 21 deletions

View File

@ -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>(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)
}

View File

@ -50,3 +50,12 @@ pub fn remove_repo(address string, api_key string, id string) ?Response<string>
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<string> {
data := send_request<string>(http.Method.patch, address, '/api/repos/$id', api_key,
params) ?
return data
}