forked from vieter-v/vieter
Compare commits
No commits in common. "0b050a81db25fb0d7437fa9a2b32d324e16bc9b3" and "d9d7272b4442393a1dcc5973227e00a13eb20fe2" have entirely different histories.
0b050a81db
...
d9d7272b44
|
|
@ -2,7 +2,6 @@ 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]
|
||||||
|
|
@ -27,14 +26,14 @@ pub fn cmd() cli.Command {
|
||||||
},
|
},
|
||||||
cli.Command{
|
cli.Command{
|
||||||
name: 'add'
|
name: 'add'
|
||||||
required_args: 3
|
required_args: 4
|
||||||
usage: 'url branch repo'
|
usage: 'url branch repo arch...'
|
||||||
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]) ?
|
add(conf, cmd.args[0], cmd.args[1], cmd.args[2], cmd.args[3..]) ?
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
cli.Command{
|
cli.Command{
|
||||||
|
|
@ -49,18 +48,6 @@ 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
|
||||||
|
|
@ -87,11 +74,6 @@ 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') ?
|
||||||
|
|
@ -114,16 +96,16 @@ pub fn cmd() cli.Command {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_repo_by_prefix tries to find the repo with the given prefix in its
|
// get_repo_id_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_by_prefix(conf Config, id_prefix string) ?(string, GitRepo) {
|
fn get_repo_id_by_prefix(conf Config, id_prefix string) ?string {
|
||||||
repos := get_repos(conf.address, conf.api_key) ?
|
repos := get_repos(conf.address, conf.api_key) ?
|
||||||
|
|
||||||
mut res := map[string]GitRepo{}
|
mut res := []string{}
|
||||||
|
|
||||||
for id, repo in repos {
|
for id, _ in repos {
|
||||||
if id.starts_with(id_prefix) {
|
if id.starts_with(id_prefix) {
|
||||||
res[id] = repo
|
res << id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -135,7 +117,7 @@ fn get_repo_by_prefix(conf Config, id_prefix string) ?(string, GitRepo) {
|
||||||
return error('Multiple repos found for given prefix.')
|
return error('Multiple repos found for given prefix.')
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.keys()[0], res[res.keys()[0]]
|
return res[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// list prints out a list of all repositories.
|
// list prints out a list of all repositories.
|
||||||
|
|
@ -143,20 +125,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')
|
println('${id[..8]}\t$details.url\t$details.branch\t$details.repo\t$details.arch')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) ? {
|
fn add(conf Config, url string, branch string, repo string, arch []string) ? {
|
||||||
res := add_repo(conf.address, conf.api_key, url, branch, repo, []) ?
|
res := add_repo(conf.address, conf.api_key, url, branch, repo, arch) ?
|
||||||
|
|
||||||
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_by_prefix(conf, id_prefix) ?
|
id := get_repo_id_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)
|
||||||
|
|
@ -164,28 +146,8 @@ 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) ? {
|
||||||
// We check the cron expression first because it's useless to send an
|
id := get_repo_id_by_prefix(conf, id_prefix) ?
|
||||||
// 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')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -35,16 +35,12 @@ 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> {
|
||||||
mut params := {
|
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
|
||||||
|
|
|
||||||
|
|
@ -57,15 +57,7 @@ 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.'))
|
||||||
}
|
}
|
||||||
|
|
||||||
mut params := app.query.clone()
|
new_repo := git.repo_from_params(app.query) or {
|
||||||
|
|
||||||
// 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()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue