2022-04-06 16:52:31 +02:00
|
|
|
module git
|
|
|
|
|
|
|
|
import cli
|
|
|
|
import env
|
2022-05-01 12:52:05 +02:00
|
|
|
import cron.expression { parse_expression }
|
2022-04-06 16:52:31 +02:00
|
|
|
|
|
|
|
struct Config {
|
|
|
|
address string [required]
|
|
|
|
api_key string [required]
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:57:05 +02:00
|
|
|
// cmd returns the cli submodule that handles the repos API interaction
|
2022-04-06 16:52:31 +02:00
|
|
|
pub fn cmd() cli.Command {
|
|
|
|
return cli.Command{
|
|
|
|
name: 'repos'
|
|
|
|
description: 'Interact with the repos API.'
|
|
|
|
commands: [
|
2022-04-06 16:57:27 +02:00
|
|
|
cli.Command{
|
|
|
|
name: 'list'
|
|
|
|
description: 'List the current repos.'
|
|
|
|
execute: fn (cmd cli.Command) ? {
|
2022-04-06 17:51:06 +02:00
|
|
|
config_file := cmd.flags.get_string('config-file') ?
|
|
|
|
conf := env.load<Config>(config_file) ?
|
2022-04-06 16:52:31 +02:00
|
|
|
|
2022-04-06 16:57:27 +02:00
|
|
|
list(conf) ?
|
|
|
|
}
|
|
|
|
},
|
2022-04-06 17:16:27 +02:00
|
|
|
cli.Command{
|
|
|
|
name: 'add'
|
2022-05-01 12:44:54 +02:00
|
|
|
required_args: 3
|
|
|
|
usage: 'url branch repo'
|
2022-04-06 17:16:27 +02:00
|
|
|
description: 'Add a new repository.'
|
|
|
|
execute: fn (cmd cli.Command) ? {
|
2022-04-06 17:51:06 +02:00
|
|
|
config_file := cmd.flags.get_string('config-file') ?
|
|
|
|
conf := env.load<Config>(config_file) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-05-01 12:44:54 +02:00
|
|
|
add(conf, cmd.args[0], cmd.args[1], cmd.args[2]) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
cli.Command{
|
|
|
|
name: 'remove'
|
2022-04-07 11:54:20 +02:00
|
|
|
required_args: 1
|
|
|
|
usage: 'id'
|
|
|
|
description: 'Remove a repository that matches the given ID prefix.'
|
2022-04-06 17:16:27 +02:00
|
|
|
execute: fn (cmd cli.Command) ? {
|
2022-04-06 17:51:06 +02:00
|
|
|
config_file := cmd.flags.get_string('config-file') ?
|
|
|
|
conf := env.load<Config>(config_file) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-04-07 11:54:20 +02:00
|
|
|
remove(conf, cmd.args[0]) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
}
|
|
|
|
},
|
2022-05-01 13:06:57 +02:00
|
|
|
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]) ?
|
|
|
|
}
|
|
|
|
},
|
2022-04-07 15:08:27 +02:00
|
|
|
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
|
|
|
|
},
|
2022-05-01 12:52:05 +02:00
|
|
|
cli.Flag{
|
|
|
|
name: 'schedule'
|
|
|
|
description: 'Cron schedule for repository.'
|
|
|
|
flag: cli.FlagType.string
|
|
|
|
},
|
2022-04-07 15:08:27 +02:00
|
|
|
]
|
|
|
|
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) ?
|
|
|
|
}
|
|
|
|
},
|
2022-04-06 16:52:31 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-01 13:06:57 +02:00
|
|
|
// get_repo_by_prefix tries to find the repo with the given prefix in its
|
2022-04-30 20:22:03 +02:00
|
|
|
// ID. If multiple or none are found, an error is raised.
|
2022-04-07 15:08:27 +02:00
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// list prints out a list of all repositories.
|
2022-04-07 11:54:20 +02:00
|
|
|
fn list(conf Config) ? {
|
2022-04-07 12:07:56 +02:00
|
|
|
repos := get_repos(conf.address, conf.api_key) ?
|
2022-04-07 11:54:20 +02:00
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
for repo in repos {
|
2022-05-03 16:55:50 +02:00
|
|
|
println('$repo.id\t$repo.url\t$repo.branch\t$repo.repo')
|
2022-04-06 22:41:19 +02:00
|
|
|
}
|
2022-04-06 16:52:31 +02:00
|
|
|
}
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// add adds a new repository to the server's list.
|
2022-05-01 12:44:54 +02:00
|
|
|
fn add(conf Config, url string, branch string, repo string) ? {
|
|
|
|
res := add_repo(conf.address, conf.api_key, url, branch, repo, []) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-04-07 12:07:56 +02:00
|
|
|
println(res.message)
|
2022-04-06 17:16:27 +02:00
|
|
|
}
|
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// remove removes a repository from the server's list.
|
2022-05-03 16:16:56 +02:00
|
|
|
fn remove(conf Config, id string) ? {
|
|
|
|
// id, _ := get_repo_by_prefix(conf, id_prefix) ?
|
|
|
|
id_int := id.int()
|
2022-04-07 11:54:20 +02:00
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
if id_int != 0 {
|
|
|
|
res := remove_repo(conf.address, conf.api_key, id_int) ?
|
|
|
|
println(res.message)
|
|
|
|
}
|
2022-04-07 15:08:27 +02:00
|
|
|
}
|
2022-04-07 11:54:20 +02:00
|
|
|
|
2022-04-30 20:22:03 +02:00
|
|
|
// patch patches a given repository with the provided params.
|
2022-05-03 16:16:56 +02:00
|
|
|
fn patch(conf Config, id string, params map[string]string) ? {
|
2022-05-01 12:52:05 +02:00
|
|
|
// We check the cron expression first because it's useless to send an
|
|
|
|
// invalid one to the server.
|
2022-05-01 14:51:52 +02:00
|
|
|
if 'schedule' in params && params['schedule'] != '' {
|
2022-05-01 12:52:05 +02:00
|
|
|
parse_expression(params['schedule']) or {
|
|
|
|
return error('Invalid cron expression: $err.msg()')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
id_int := id.int()
|
|
|
|
if id_int != 0 {
|
|
|
|
res := patch_repo(conf.address, conf.api_key, id_int, params) ?
|
2022-04-06 17:16:27 +02:00
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
println(res.message)
|
|
|
|
}
|
2022-04-06 17:16:27 +02:00
|
|
|
}
|
2022-05-01 13:06:57 +02:00
|
|
|
|
|
|
|
// info shows detailed information for a given repo.
|
2022-05-03 16:16:56 +02:00
|
|
|
fn info(conf Config, id string) ? {
|
|
|
|
id_int := id.int()
|
2022-05-01 13:06:57 +02:00
|
|
|
|
2022-05-03 16:16:56 +02:00
|
|
|
if id_int == 0 {
|
|
|
|
return
|
2022-05-01 13:06:57 +02:00
|
|
|
}
|
2022-05-03 16:16:56 +02:00
|
|
|
|
|
|
|
repo := get_repo(conf.address, conf.api_key, id_int) ?
|
|
|
|
println(repo)
|
2022-05-01 13:06:57 +02:00
|
|
|
}
|