Merge pull request 'Repo CLI Improvements' (#146) from better-repos-cli into dev

Reviewed-on: Chewing_Bever/vieter#146
cli-docs
Jef Roosens 2022-05-01 13:17:21 +02:00
commit 0b050a81db
3 changed files with 67 additions and 17 deletions

View File

@ -2,6 +2,7 @@ module git
import cli import cli
import env import env
import cron.expression { parse_expression }
struct Config { struct Config {
address string [required] address string [required]
@ -26,14 +27,14 @@ pub fn cmd() cli.Command {
}, },
cli.Command{ cli.Command{
name: 'add' name: 'add'
required_args: 4 required_args: 3
usage: 'url branch repo arch...' usage: 'url branch repo'
description: 'Add a new repository.' description: 'Add a new repository.'
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file') ? config_file := cmd.flags.get_string('config-file') ?
conf := env.load<Config>(config_file) ? conf := env.load<Config>(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{ cli.Command{
@ -48,6 +49,18 @@ pub fn cmd() cli.Command {
remove(conf, cmd.args[0]) ? 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>(config_file) ?
info(conf, cmd.args[0]) ?
}
},
cli.Command{ cli.Command{
name: 'edit' name: 'edit'
required_args: 1 required_args: 1
@ -74,6 +87,11 @@ pub fn cmd() cli.Command {
description: 'Comma-separated list of architectures to build on.' description: 'Comma-separated list of architectures to build on.'
flag: cli.FlagType.string flag: cli.FlagType.string
}, },
cli.Flag{
name: 'schedule'
description: 'Cron schedule for repository.'
flag: cli.FlagType.string
},
] ]
execute: fn (cmd cli.Command) ? { execute: fn (cmd cli.Command) ? {
config_file := cmd.flags.get_string('config-file') ? config_file := cmd.flags.get_string('config-file') ?
@ -96,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. // 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) ? 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) { if id.starts_with(id_prefix) {
res << id res[id] = repo
} }
} }
@ -117,7 +135,7 @@ fn get_repo_id_by_prefix(conf Config, id_prefix string) ?string {
return error('Multiple repos found for given prefix.') 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. // list prints out a list of all repositories.
@ -125,20 +143,20 @@ fn list(conf Config) ? {
repos := get_repos(conf.address, conf.api_key) ? repos := get_repos(conf.address, conf.api_key) ?
for id, details in repos { 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')
} }
} }
// add adds a new repository to the server's list. // add adds a new repository to the server's list.
fn add(conf Config, url string, branch string, repo string, arch []string) ? { fn add(conf Config, url string, branch string, repo string) ? {
res := add_repo(conf.address, conf.api_key, url, branch, repo, arch) ? res := add_repo(conf.address, conf.api_key, url, branch, repo, []) ?
println(res.message) println(res.message)
} }
// remove removes a repository from the server's list. // remove removes a repository from the server's list.
fn remove(conf Config, id_prefix string) ? { 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) ? res := remove_repo(conf.address, conf.api_key, id) ?
println(res.message) println(res.message)
@ -146,8 +164,28 @@ fn remove(conf Config, id_prefix string) ? {
// patch patches a given repository with the provided params. // 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_prefix string, params map[string]string) ? {
id := get_repo_id_by_prefix(conf, id_prefix) ? // 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_by_prefix(conf, id_prefix) ?
res := patch_repo(conf.address, conf.api_key, id, params) ? res := patch_repo(conf.address, conf.api_key, id, 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) ?
println('id: $id')
$for field in GitRepo.fields {
val := repo.$(field.name)
println('$field.name: $val')
}
}

View File

@ -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. // 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<string> { pub fn add_repo(address string, api_key string, url string, branch string, repo string, arch []string) ?Response<string> {
params := { mut params := {
'url': url 'url': url
'branch': branch 'branch': branch
'repo': repo 'repo': repo
'arch': arch.join(',')
} }
if arch.len > 0 {
params['arch'] = arch.join(',')
}
data := send_request<string>(http.Method.post, address, '/api/repos', api_key, params) ? data := send_request<string>(http.Method.post, address, '/api/repos', api_key, params) ?
return data return data

View File

@ -57,7 +57,15 @@ fn (mut app App) post_repo() web.Result {
return app.json(http.Status.unauthorized, new_response('Unauthorized.')) 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())) return app.json(http.Status.bad_request, new_response(err.msg()))
} }